ServiceRequest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/ServiceRequest.h>
  18. #include <alibabacloud/core/Utils.h>
  19. namespace AlibabaCloud {
  20. ServiceRequest::ServiceRequest(const std::string &product,
  21. const std::string &version)
  22. : content_(nullptr), contentSize_(0), params_(), product_(product),
  23. resourcePath_("/"), version_(version), scheme_("https"),
  24. connectTimeout_(kInvalidTimeout), readTimeout_(kInvalidTimeout),
  25. method_(HttpRequest::Method::Get) {
  26. setHeader(std::string("x-acs-version"), version);
  27. }
  28. ServiceRequest::ServiceRequest(const ServiceRequest &other)
  29. : content_(nullptr), contentSize_(other.contentSize_),
  30. params_(other.params_), product_(other.product_),
  31. resourcePath_(other.resourcePath_), version_(other.version_),
  32. scheme_(other.scheme_), connectTimeout_(other.connectTimeout_),
  33. readTimeout_(other.readTimeout_) {
  34. setContent(other.content_, other.contentSize_);
  35. }
  36. ServiceRequest::ServiceRequest(ServiceRequest &&other) {
  37. *this = std::move(other);
  38. }
  39. ServiceRequest &ServiceRequest::operator=(const ServiceRequest &other) {
  40. if (this != &other) {
  41. content_ = nullptr;
  42. contentSize_ = 0;
  43. params_ = other.params_;
  44. connectTimeout_ = other.connectTimeout_;
  45. readTimeout_ = other.readTimeout_;
  46. setContent(other.content_, other.contentSize_);
  47. }
  48. return *this;
  49. }
  50. ServiceRequest &ServiceRequest::operator=(ServiceRequest &&other) {
  51. if (this != &other)
  52. *this = std::move(other);
  53. return *this;
  54. }
  55. ServiceRequest::~ServiceRequest() {
  56. if (content_)
  57. delete content_;
  58. }
  59. const char *ServiceRequest::content() const { return content_; }
  60. size_t ServiceRequest::contentSize() const { return contentSize_; }
  61. bool ServiceRequest::hasContent() const { return (contentSize_ != 0); }
  62. void ServiceRequest::setContent(const char *data, size_t size) {
  63. if (content_)
  64. delete content_;
  65. content_ = nullptr;
  66. contentSize_ = 0;
  67. if (size) {
  68. contentSize_ = size;
  69. content_ = new char[size];
  70. std::copy(data, data + size, content_);
  71. }
  72. }
  73. void ServiceRequest::addParameter(const ParameterNameType &name,
  74. const ParameterValueType &value) {
  75. setParameter(name, value);
  76. }
  77. ServiceRequest::ParameterValueType
  78. ServiceRequest::parameter(const ParameterNameType &name) const {
  79. ParameterCollection::const_iterator it = params_.find(name);
  80. if (it == params_.end()) {
  81. return ParameterValueType("");
  82. }
  83. return it->second;
  84. }
  85. ServiceRequest::ParameterValueType
  86. ServiceRequest::coreParameter(const ParameterNameType &name) const {
  87. return parameter(name);
  88. }
  89. ServiceRequest::ParameterCollection ServiceRequest::parameters() const {
  90. return params_;
  91. }
  92. ServiceRequest::ParameterCollection ServiceRequest::bodyParameters() const {
  93. return body_params_;
  94. }
  95. void ServiceRequest::removeParameter(const ParameterNameType &name) {
  96. params_.erase(name);
  97. }
  98. void ServiceRequest::setParameter(const ParameterNameType &name,
  99. const ParameterValueType &value) {
  100. params_[name] = value;
  101. }
  102. void ServiceRequest::setCoreParameter(const ParameterNameType &name,
  103. const ParameterValueType &value) {
  104. setParameter(name, value);
  105. }
  106. void ServiceRequest::setBodyParameter(const ParameterNameType &name,
  107. const ParameterValueType &value) {
  108. body_params_[name] = value;
  109. }
  110. void ServiceRequest::setParameters(const ParameterCollection &params) {
  111. params_ = params;
  112. }
  113. void ServiceRequest::setJsonParameters(const ParameterNameType &name,
  114. const ParameterCollection &params) {
  115. params_ = params;
  116. params_ = params;
  117. setParameter(name, AlibabaCloud::MapToJson(params));
  118. }
  119. std::string ServiceRequest::version() const { return version_; }
  120. HttpRequest::Method ServiceRequest::method() const { return method_; }
  121. void ServiceRequest::setVersion(const std::string &version) {
  122. version_ = version;
  123. }
  124. std::string ServiceRequest::product() const { return product_; }
  125. void ServiceRequest::setProduct(const std::string &product) {
  126. product_ = product;
  127. }
  128. std::string ServiceRequest::resourcePath() const { return resourcePath_; }
  129. void ServiceRequest::setResourcePath(const std::string &path) {
  130. resourcePath_ = path;
  131. }
  132. void ServiceRequest::setScheme(const std::string scheme) { scheme_ = scheme; }
  133. std::string ServiceRequest::scheme() const { return scheme_; }
  134. long ServiceRequest::connectTimeout() const { return connectTimeout_; }
  135. long ServiceRequest::readTimeout() const { return readTimeout_; }
  136. void ServiceRequest::setConnectTimeout(const long connectTimeout) {
  137. connectTimeout_ = connectTimeout;
  138. }
  139. void ServiceRequest::setReadTimeout(const long readTimeout) {
  140. readTimeout_ = readTimeout;
  141. }
  142. void ServiceRequest::setMethod(const HttpRequest::Method method) {
  143. method_ = method;
  144. }
  145. void ServiceRequest::setHeader(
  146. const ServiceRequest::ParameterNameType &name,
  147. const ServiceRequest::ParameterValueType &value) {
  148. headers_[name] = value;
  149. }
  150. ServiceRequest::ParameterValueType
  151. ServiceRequest::getHeader(const ServiceRequest::ParameterNameType &name) {
  152. return headers_[name];
  153. }
  154. void ServiceRequest::removeHeader(
  155. const ServiceRequest::ParameterNameType &name) {
  156. headers_.erase(name);
  157. }
  158. ServiceRequest::ParameterCollection ServiceRequest::headers() const {
  159. return headers_;
  160. }
  161. } // namespace AlibabaCloud