Skip to content

Commit bb65b40

Browse files
committed
release 0.0.4-beta source code for cpp
1 parent fd89107 commit bb65b40

File tree

13 files changed

+143
-74
lines changed

13 files changed

+143
-74
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 0.0.4-beta # 0.0.4-beta 2023-02-20
2+
3+
### G42Cloud SDK IMS
4+
5+
- _Features_
6+
- None
7+
- _Bug Fix_
8+
- None
9+
- _Change_
10+
- Add the enum values `IsoImage` to the request parameter `type` to the interface `CreateImage`
11+
112
# 0.0.3-beta 2023-01-06
213

314
### G42Cloud SDK IMS

core/include/g42cloud/core/Client.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <memory>
2323
#include <curl/curl.h>
2424
#include <map>
25+
#include <atomic>
2526

2627
#include <g42cloud/core/RequestParams.h>
2728
#include <g42cloud/core/utils/Header.h>
@@ -55,17 +56,16 @@ class G42CLOUD_CORE_EXPORT Client {
5556

5657
void setHttpConfig(const HttpConfig &httpConfig);
5758
void setCredentials(std::unique_ptr<Credentials> credentials);
58-
void setEndPoint(std::string endPoint);
5959
void setStreamLog(bool streamLog);
6060
void setFileLog(std::string filePath, bool fileLog);
6161
void setRegion(Region region);
6262
void setHttpClient(const HttpClient& httpClient);
63+
void setEndpoints(const std::vector<std::string> &endpoints);
6364

64-
std::string getEndpoint();
6565
Region getRegion();
6666
bool getStreamLog();
6767
bool getFileLog();
68-
68+
const std::vector<std::string> &getEndpoints() const;
6969
bool isCredentialsEmpty();
7070

7171
void processRegionAuth();
@@ -84,14 +84,16 @@ class G42CLOUD_CORE_EXPORT Client {
8484
const std::map<std::string, std::string> &updatePathParams);
8585
std::string getQueryParams(const std::map<std::string, std::string> &map);
8686

87-
std::string endpoint_;
87+
std::vector<std::string> endpoints_;
8888
std::unique_ptr<Credentials> credentials_;
8989
HttpConfig httpConfig_;
9090
bool streamLog_ = false;
9191
bool fileLog_ = false;
9292
std::string filePath_;
9393
Region region_;
9494
HttpClient httpClient_;
95+
96+
std::atomic<int> endpointIndex = 0;
9597
};
9698
}
9799
}

core/include/g42cloud/core/ClientBuilder.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ template <typename T> class G42CLOUD_CORE_EXPORT ClientBuilder {
135135

136136
ClientBuilder &withEndPoint(std::string endPoint)
137137
{
138-
endPoint_ = std::move(endPoint);
138+
if (endPoints_.empty()) {
139+
endPoints_.emplace_back(endPoint);
140+
} else {
141+
endPoints_[0] = endPoint;
142+
}
139143
return *this;
140144
}
141145

@@ -177,7 +181,7 @@ template <typename T> class G42CLOUD_CORE_EXPORT ClientBuilder {
177181
}
178182
client->setFileLog(std::move(filePath_), fileLog_);
179183
client->setStreamLog(streamLog_);
180-
client->setEndPoint(endPoint_);
184+
client->setEndpoints(endPoints_);
181185
if (region_.getRegionId() != "" && region_.getEndpoint() != "") {
182186
client->setRegion(region_);
183187
client->processRegionAuth();
@@ -192,7 +196,7 @@ template <typename T> class G42CLOUD_CORE_EXPORT ClientBuilder {
192196
std::unique_ptr<HttpConfig> httpConfig_;
193197
Region region_;
194198
bool streamLog_ = false;
195-
std::string endPoint_;
199+
std::vector<std::string> endPoints_;
196200
std::string filePath_;
197201
bool fileLog_ = false;
198202
std::string defaultType_;

core/include/g42cloud/core/auth/Region.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define G42CLOUD_SDK_CORE_AUTH_REGION_H
2121

2222
#include <string>
23+
#include <vector>
2324
#include <g42cloud/core/CoreExport.h>
2425

2526
namespace G42Cloud {
@@ -33,15 +34,19 @@ class G42CLOUD_CORE_EXPORT Region {
3334

3435
Region(const Region& region);
3536
Region(std::string regionId, std::string endpoint);
37+
Region(std::string regionId, std::initializer_list<std::string> endpoints);
3638

3739
Region& withRegionId(std::string regionId);
3840
Region& withEndpoint(std::string endpoint);
41+
Region& withEndpoints(std::vector<std::string> endpoints);
3942

4043
const std::string getEndpoint() const;
4144
const std::string getRegionId() const;
45+
46+
std::vector<std::string> getEndpoints() const;
4247
private:
4348
std::string regionId_;
44-
std::string endpoint_;
49+
std::vector<std::string> endpoints_;
4550
};
4651
}
4752
}

core/include/g42cloud/core/http/HttpConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class G42CLOUD_CORE_EXPORT HttpConfig {
8080
int getPoolMaxsize() const;
8181

8282
void setPoolMaxsize(int poolMaxsize);
83-
8483
private:
8584
std::string proxyProtocol_;
8685
std::string proxyHost_;
@@ -89,6 +88,7 @@ class G42CLOUD_CORE_EXPORT HttpConfig {
8988
std::string proxyPassword_;
9089

9190
bool ignoreSslVerification_ = false;
91+
9292
std::string sslCaCert_;
9393
std::string certFile_;
9494

core/src/Client.cpp

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,68 +21,80 @@
2121
#include <g42cloud/core/auth/GlobalCredentials.h>
2222
#include <g42cloud/core/auth/BasicCredentials.h>
2323
#include <g42cloud/core/http/HttpRequest.h>
24+
#include <g42cloud/core/utils/ModelBase.h>
25+
2426
#include <boost/algorithm/string/replace.hpp>
2527
#include <utility>
26-
#include <g42cloud/core/utils/MultipartFormData.h>
27-
#include <g42cloud/core/utils/ModelBase.h>
2828

2929
using namespace G42Cloud::Sdk::Core;
3030
using namespace G42Cloud::Sdk::Core::Auth;
31+
using namespace G42Cloud::Sdk::Core::Exception;
3132

3233
Client::Client() = default;
3334

34-
Client::~Client() {}
35+
Client::~Client() = default;
3536

3637
void Client::processRegionAuth() {
38+
spdlog::info("[Client]begin execute region auth...");
3739
const std::string regionId = region_.getRegionId();
3840
const std::string endPoint = region_.getEndpoint();
39-
if (!endPoint.empty()) {
40-
this->endpoint_ = endPoint;
41-
}
41+
const std::vector<std::string>& regionEndpoints = this->region_.getEndpoints();
42+
this->endpoints_.insert(this->endpoints_.end(), regionEndpoints.begin(), regionEndpoints.end());
43+
spdlog::info("[Client]execute region auth end...");
4244
}
4345

4446
std::unique_ptr<HttpResponse> Client::callApi(const std::string &method, const std::string &resourcePath,
4547
const std::map<std::string, std::string> &pathParams, const std::map<std::string, std::string> &queryParams,
46-
const std::map<std::string, std::string> &headerParams, const std::string &body)
47-
{
48+
const std::map<std::string, std::string> &headerParams, const std::string &body) {
4849
std::string scheme;
4950
std::string host;
5051

5152
spdlog::info("client:call service api {}, resourcePath:{}", method, resourcePath);
5253
const std::string regionId = this->region_.getRegionId();
5354
if (!regionId.empty()) {
54-
spdlog::info("use region auth, processing...");
55+
spdlog::info("begin execute region auth for region:{}", regionId);
5556
credentials_->regionInit();
5657
credentials_->processAuthParams(regionId);
57-
spdlog::info("region auth sucessfully!");
58+
spdlog::info("region auth for region:{} successfully!", regionId);
5859
}
59-
60-
parseEndPoint(endpoint_, scheme, host);
61-
std::string uriHttp = getResourcePath(resourcePath, pathParams, credentials_->getUpdatePathParams());
62-
std::string queryParamsHttp = getQueryParams(queryParams);
63-
RequestParams requestParams(method, scheme, host, uriHttp, queryParamsHttp, false, body);
64-
65-
requestParams.addHeader(Header("User-Agent", "g42cloud-usdk-cpp/3.0"));
66-
addHeaderParams(requestParams, headerParams);
67-
credentials_->processAuthRequest(requestParams);
68-
69-
if (handler_request) {
70-
handler_request(requestParams);
60+
while (true) {
61+
parseEndPoint(this->endpoints_[endpointIndex], scheme, host);
62+
std::string uriHttp = getResourcePath(resourcePath, pathParams, credentials_->getUpdatePathParams());
63+
std::string queryParamsHttp = getQueryParams(queryParams);
64+
RequestParams requestParams(method, scheme, host, uriHttp, queryParamsHttp, false, body);
65+
66+
requestParams.addHeader(Header("User-Agent", "g42cloud-usdk-cpp/3.0"));
67+
addHeaderParams(requestParams, headerParams);
68+
credentials_->processAuthRequest(requestParams);
69+
70+
if (handler_request) {
71+
handler_request(requestParams);
72+
}
73+
spdlog::info("begin execute http request for the api....");
74+
HttpRequest httpRequest;
75+
httpRequest.setUrl(parseUrl(requestParams));
76+
httpRequest.setMethod(requestParams.getMethod());
77+
httpRequest.setRequestBody(requestParams.getBody());
78+
httpRequest.setHeaders(requestParams.getHeaders());
79+
httpRequest.setStreamLog(streamLog_);
80+
httpRequest.setFileLog(fileLog_);
81+
httpRequest.setFilePath(filePath_);
82+
try {
83+
std::unique_ptr<HttpResponse> httpResponse =
84+
httpClient_.doHttpRequestSync(httpRequest, httpConfig_, handler_response);
85+
spdlog::info("execute http request for the api successfully, get the response....");
86+
return httpResponse;
87+
} catch (HostUnreachableException ex) {
88+
if (!this->endpoints_.empty() && endpointIndex < this->endpoints_.size() - 1) {
89+
spdlog::error("can not resolve host for service,region:{}, error:{}", regionId, ex.what());
90+
endpointIndex++;
91+
} else {
92+
endpointIndex = 0;
93+
std::string errorMsg = "can not resolve all endpoints for service in region:!" + regionId;
94+
throw HostUnreachableException(errorMsg.c_str());
95+
}
96+
}
7197
}
72-
spdlog::info("begin execute http request for the api....");
73-
HttpRequest httpRequest;
74-
httpRequest.setUrl(parseUrl(requestParams));
75-
httpRequest.setMethod(requestParams.getMethod());
76-
httpRequest.setRequestBody(requestParams.getBody());
77-
httpRequest.setHeaders(requestParams.getHeaders());
78-
httpRequest.setStreamLog(streamLog_);
79-
httpRequest.setFileLog(fileLog_);
80-
httpRequest.setFilePath(filePath_);
81-
82-
std::unique_ptr<HttpResponse> httpResponse =
83-
httpClient_.doHttpRequestSync(httpRequest, httpConfig_, handler_response);
84-
spdlog::info("execute http request for the api successfully, get the response....");
85-
return httpResponse;
8698
}
8799

88100
std::string Client::parseUrl(const RequestParams &requestParams)
@@ -138,11 +150,6 @@ bool Client::isCredentialsEmpty() {
138150
return credentials_ == nullptr;
139151
}
140152

141-
void Client::setEndPoint(std::string endPoint)
142-
{
143-
endpoint_ = std::move(endPoint);
144-
}
145-
146153
void Client::setStreamLog(bool streamLog)
147154
{
148155
streamLog_ = streamLog;
@@ -163,10 +170,6 @@ void Client::setHttpClient(const HttpClient& httpClient) {
163170
httpClient_ = httpClient;
164171
}
165172

166-
std::string Client::getEndpoint() {
167-
return this->endpoint_;
168-
}
169-
170173
Region Client::getRegion() {
171174
return this->region_;
172175
}
@@ -178,6 +181,14 @@ bool Client::getStreamLog() {
178181
return this->streamLog_;
179182
}
180183

184+
const std::vector<std::string> &Client::getEndpoints() const {
185+
return endpoints_;
186+
}
187+
188+
void Client::setEndpoints(const std::vector<std::string> &endpoints) {
189+
endpoints_ = endpoints;
190+
}
191+
181192
void Client::parseEndPoint(const std::string &str, std::string &scheme, std::string &host)
182193
{
183194
std::vector<std::string> res;
@@ -200,4 +211,4 @@ void Client::parseEndPoint(const std::string &str, std::string &scheme, std::str
200211
}
201212
scheme = res.at(0);
202213
host = res.at(1);
203-
}
214+
}

core/src/auth/Region.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,47 @@
2020
#include <g42cloud/core/auth/Region.h>
2121
using namespace G42Cloud::Sdk::Core::Auth;
2222

23-
Region::Region(std::string regionId, std::string endpoint) :regionId_(regionId), endpoint_(endpoint)
24-
{}
23+
Region::Region(std::string regionId, std::string endpoint) :regionId_(regionId) {
24+
this->endpoints_.emplace_back(endpoint);
25+
}
2526

26-
Region::Region(const Region& region) :regionId_(region.getRegionId()), endpoint_(region.getEndpoint())
27+
Region::Region(const Region& region) :regionId_(region.getRegionId()), endpoints_(region.getEndpoints())
2728
{}
2829

30+
Region::Region(std::string regionId, std::initializer_list<std::string> endpoints) {
31+
this->regionId_ = regionId;
32+
for (auto iter = endpoints.begin(); iter != endpoints.end(); iter++) {
33+
endpoints_.emplace_back(*iter);
34+
}
35+
}
36+
2937
Region& Region::withRegionId(const std::string regionId) {
3038
this->regionId_ = regionId;
3139
return *this;
3240
}
3341

3442
Region& Region::withEndpoint(const std::string endpoint) {
35-
this->endpoint_ = endpoint;
43+
if (this->endpoints_.empty()) {
44+
this->endpoints_.emplace_back(endpoint);
45+
} else {
46+
this->endpoints_[0] = endpoint;
47+
}
48+
return *this;
49+
}
50+
51+
Region& Region::withEndpoints(std::vector<std::string> endpoints) {
52+
this->endpoints_ = endpoints;
3653
return *this;
3754
}
3855

3956
const std::string Region::getRegionId() const {
4057
return this->regionId_;
4158
}
59+
4260
const std::string Region::getEndpoint() const {
43-
return this->endpoint_;
61+
return this->endpoints_.empty() ? "" : this->endpoints_.front();
62+
}
63+
64+
std::vector<std::string> Region::getEndpoints() const {
65+
return this->endpoints_;
4466
}

0 commit comments

Comments
 (0)