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
2929using namespace G42Cloud ::Sdk::Core;
3030using namespace G42Cloud ::Sdk::Core::Auth;
31+ using namespace G42Cloud ::Sdk::Core::Exception;
3132
3233Client::Client () = default;
3334
34- Client::~Client () {}
35+ Client::~Client () = default ;
3536
3637void 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
4446std::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
88100std::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-
146153void 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-
170173Region 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+
181192void 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+ }
0 commit comments