RpcServiceClient.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 1999-2019 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <alibabacloud/core/AlibabaCloud.h>
  17. #include <alibabacloud/core/RpcServiceClient.h>
  18. #include <alibabacloud/core/HmacSha1Signer.h>
  19. #include <algorithm>
  20. #include <iomanip>
  21. #include <sstream>
  22. #include "Utils.h"
  23. namespace AlibabaCloud {
  24. RpcServiceClient::RpcServiceClient(const std::string & servicename,
  25. const std::shared_ptr<CredentialsProvider> &credentialsProvider,
  26. const ClientConfiguration &configuration,
  27. const std::shared_ptr<Signer> &signer) :
  28. CoreClient(servicename, configuration),
  29. credentialsProvider_(credentialsProvider),
  30. signer_(signer) {
  31. }
  32. RpcServiceClient::~RpcServiceClient() {
  33. }
  34. RpcServiceClient::JsonOutcome RpcServiceClient::makeRequest(
  35. const std::string &endpoint,
  36. const RpcServiceRequest &msg, HttpRequest::Method method)const {
  37. auto outcome = AttemptRequest(endpoint, msg, method);
  38. if (outcome.isSuccess())
  39. return JsonOutcome(std::string(outcome.result().body(),
  40. outcome.result().bodySize()));
  41. else
  42. return JsonOutcome(outcome.error());
  43. }
  44. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint,
  45. const ServiceRequest &msg, HttpRequest::Method method)const {
  46. return buildHttpRequest(endpoint,
  47. dynamic_cast<const RpcServiceRequest& >(msg), method);
  48. }
  49. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint,
  50. const RpcServiceRequest &msg, HttpRequest::Method method) const {
  51. const Credentials credentials = credentialsProvider_->getCredentials();
  52. Url url;
  53. if (msg.scheme().empty()) {
  54. url.setScheme("https");
  55. } else {
  56. url.setScheme(msg.scheme());
  57. }
  58. url.setHost(endpoint);
  59. url.setPath(msg.resourcePath());
  60. auto params = msg.parameters();
  61. std::map <std::string, std::string> queryParams;
  62. for (const auto &p : params) {
  63. if (!p.second.empty())
  64. queryParams[p.first] = p.second;
  65. }
  66. queryParams["AccessKeyId"] = credentials.accessKeyId();
  67. queryParams["Format"] = "JSON";
  68. queryParams["RegionId"] = configuration().regionId();
  69. queryParams["SecurityToken"] = credentials.sessionToken();
  70. queryParams["SignatureMethod"] = signer_->name();
  71. queryParams["SignatureNonce"] = GenerateUuid();
  72. queryParams["SignatureVersion"] = signer_->version();
  73. std::time_t t = std::time(nullptr);
  74. std::stringstream ss;
  75. #if defined(__GNUG__) && __GNUC__ < 5
  76. char tmbuff[26];
  77. strftime(tmbuff, 26, "%FT%TZ", std::gmtime(&t));
  78. ss << tmbuff;
  79. #else
  80. ss << std::put_time(std::gmtime(&t), "%FT%TZ");
  81. #endif
  82. queryParams["Timestamp"] = ss.str();
  83. queryParams["Version"] = msg.version();
  84. std::stringstream plaintext;
  85. plaintext << HttpMethodToString(method)
  86. << "&"
  87. << UrlEncode(url.path())
  88. << "&"
  89. << UrlEncode(canonicalizedQuery(queryParams));
  90. queryParams["Signature"] = signer_->generate(plaintext.str(),
  91. credentials.accessKeySecret() + "&");
  92. std::stringstream queryString;
  93. for (const auto &p : queryParams)
  94. queryString << "&" << p.first << "=" << UrlEncode(p.second);
  95. url.setQuery(queryString.str().substr(1));
  96. HttpRequest request(url);
  97. if (msg.connectTimeout() != kInvalidTimeout) {
  98. request.setConnectTimeout(msg.connectTimeout());
  99. } else {
  100. request.setConnectTimeout(configuration().connectTimeout());
  101. }
  102. if (msg.readTimeout() != kInvalidTimeout) {
  103. request.setReadTimeout(msg.readTimeout());
  104. } else {
  105. request.setReadTimeout(configuration().readTimeout());
  106. }
  107. request.setMethod(method);
  108. request.setHeader("Host", url.host());
  109. request.setHeader("x-sdk-client",
  110. std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  111. return request;
  112. }
  113. } // namespace AlibabaCloud