RpcServiceClient.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/RpcServiceClient.h>
  17. #include <alibabacloud/core/HmacSha1Signer.h>
  18. #include <algorithm>
  19. #include <iomanip>
  20. #include <sstream>
  21. #include "Utils.h"
  22. namespace AlibabaCloud {
  23. RpcServiceClient::RpcServiceClient(const std::string & servicename,
  24. const std::shared_ptr<CredentialsProvider> &credentialsProvider,
  25. const ClientConfiguration &configuration,
  26. const std::shared_ptr<Signer> &signer) :
  27. CoreClient(servicename, configuration),
  28. credentialsProvider_(credentialsProvider),
  29. signer_(signer) {
  30. }
  31. RpcServiceClient::~RpcServiceClient() {
  32. }
  33. RpcServiceClient::JsonOutcome RpcServiceClient::makeRequest(
  34. const std::string &endpoint,
  35. const RpcServiceRequest &msg, HttpRequest::Method method)const {
  36. auto outcome = AttemptRequest(endpoint, msg, method);
  37. if (outcome.isSuccess())
  38. return JsonOutcome(std::string(outcome.result().body(),
  39. outcome.result().bodySize()));
  40. else
  41. return JsonOutcome(outcome.error());
  42. }
  43. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint,
  44. const ServiceRequest &msg, HttpRequest::Method method)const {
  45. return buildHttpRequest(endpoint,
  46. dynamic_cast<const RpcServiceRequest& >(msg), method);
  47. }
  48. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint,
  49. const RpcServiceRequest &msg, HttpRequest::Method method) const {
  50. const Credentials credentials = credentialsProvider_->getCredentials();
  51. Url url;
  52. if (msg.scheme().empty()) {
  53. url.setScheme("https");
  54. } else {
  55. url.setScheme(msg.scheme());
  56. }
  57. url.setHost(endpoint);
  58. url.setPath(msg.resourcePath());
  59. auto params = msg.parameters();
  60. std::map <std::string, std::string> queryParams;
  61. for (const auto &p : params) {
  62. if (!p.second.empty())
  63. queryParams[p.first] = p.second;
  64. }
  65. queryParams["AccessKeyId"] = credentials.accessKeyId();
  66. queryParams["Format"] = "JSON";
  67. queryParams["RegionId"] = configuration().regionId();
  68. queryParams["SecurityToken"] = credentials.sessionToken();
  69. queryParams["SignatureMethod"] = signer_->name();
  70. queryParams["SignatureNonce"] = GenerateUuid();
  71. queryParams["SignatureVersion"] = signer_->version();
  72. std::time_t t = std::time(nullptr);
  73. std::stringstream ss;
  74. #if defined(__GNUG__) && __GNUC__ < 5
  75. char tmbuff[26];
  76. strftime(tmbuff, 26, "%FT%TZ", std::gmtime(&t));
  77. ss << tmbuff;
  78. #else
  79. ss << std::put_time(std::gmtime(&t), "%FT%TZ");
  80. #endif
  81. queryParams["Timestamp"] = ss.str();
  82. queryParams["Version"] = msg.version();
  83. std::stringstream plaintext;
  84. plaintext << HttpMethodToString(method)
  85. << "&"
  86. << UrlEncode(url.path())
  87. << "&"
  88. << UrlEncode(canonicalizedQuery(queryParams));
  89. queryParams["Signature"] = signer_->generate(plaintext.str(),
  90. credentials.accessKeySecret() + "&");
  91. std::stringstream queryString;
  92. for (const auto &p : queryParams)
  93. queryString << "&" << p.first << "=" << UrlEncode(p.second);
  94. url.setQuery(queryString.str().substr(1));
  95. HttpRequest request(url);
  96. request.setMethod(method);
  97. request.setHeader("Host", url.host());
  98. request.setHeader("x-sdk-client",
  99. std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  100. return request;
  101. }
  102. } // namespace AlibabaCloud