ServiceRequest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/ServiceRequest.h>
  17. #include <alibabacloud/core/AlibabaCloud.h>
  18. #include <alibabacloud/core/Utils.h>
  19. namespace AlibabaCloud
  20. {
  21. ServiceRequest::ServiceRequest(const std::string &product,
  22. const std::string &version) : content_(nullptr),
  23. contentSize_(0),
  24. params_(),
  25. product_(product),
  26. resourcePath_("/"),
  27. version_(version),
  28. scheme_("https"),
  29. connectTimeout_(kInvalidTimeout),
  30. readTimeout_(kInvalidTimeout),
  31. method_(HttpRequest::Method::Get)
  32. {
  33. }
  34. ServiceRequest::ServiceRequest(const ServiceRequest &other) : content_(nullptr),
  35. contentSize_(other.contentSize_),
  36. params_(other.params_),
  37. product_(other.product_),
  38. resourcePath_(other.resourcePath_),
  39. version_(other.version_),
  40. scheme_(other.scheme_),
  41. connectTimeout_(other.connectTimeout_),
  42. readTimeout_(other.readTimeout_)
  43. {
  44. setContent(other.content_, other.contentSize_);
  45. }
  46. ServiceRequest::ServiceRequest(ServiceRequest &&other)
  47. {
  48. *this = std::move(other);
  49. }
  50. ServiceRequest &ServiceRequest::operator=(const ServiceRequest &other)
  51. {
  52. if (this != &other)
  53. {
  54. content_ = nullptr;
  55. contentSize_ = 0;
  56. params_ = other.params_;
  57. connectTimeout_ = other.connectTimeout_;
  58. readTimeout_ = other.readTimeout_;
  59. setContent(other.content_, other.contentSize_);
  60. }
  61. return *this;
  62. }
  63. ServiceRequest &ServiceRequest::operator=(ServiceRequest &&other)
  64. {
  65. if (this != &other)
  66. *this = std::move(other);
  67. return *this;
  68. }
  69. ServiceRequest::~ServiceRequest()
  70. {
  71. if (content_)
  72. delete content_;
  73. }
  74. const char *ServiceRequest::content() const
  75. {
  76. return content_;
  77. }
  78. size_t ServiceRequest::contentSize() const
  79. {
  80. return contentSize_;
  81. }
  82. bool ServiceRequest::hasContent() const
  83. {
  84. return (contentSize_ != 0);
  85. }
  86. void ServiceRequest::setContent(const char *data, size_t size)
  87. {
  88. if (content_)
  89. delete content_;
  90. content_ = nullptr;
  91. contentSize_ = 0;
  92. if (size)
  93. {
  94. contentSize_ = size;
  95. content_ = new char[size];
  96. std::copy(data, data + size, content_);
  97. }
  98. }
  99. void ServiceRequest::addParameter(const ParameterNameType &name,
  100. const ParameterValueType &value)
  101. {
  102. setParameter(name, value);
  103. }
  104. ServiceRequest::ParameterValueType ServiceRequest::parameter(
  105. const ParameterNameType &name) const
  106. {
  107. ParameterCollection::const_iterator it = params_.find(name);
  108. if (it == params_.end())
  109. {
  110. return ParameterValueType("");
  111. }
  112. return it->second;
  113. }
  114. ServiceRequest::ParameterValueType ServiceRequest::coreParameter(
  115. const ParameterNameType &name) const
  116. {
  117. return parameter(name);
  118. }
  119. ServiceRequest::ParameterCollection ServiceRequest::parameters() const
  120. {
  121. return params_;
  122. }
  123. ServiceRequest::ParameterCollection ServiceRequest::bodyParameters() const
  124. {
  125. return body_params_;
  126. }
  127. void ServiceRequest::removeParameter(const ParameterNameType &name)
  128. {
  129. params_.erase(name);
  130. }
  131. void ServiceRequest::setParameter(const ParameterNameType &name,
  132. const ParameterValueType &value)
  133. {
  134. params_[name] = value;
  135. }
  136. void ServiceRequest::setCoreParameter(const ParameterNameType &name, const ParameterValueType &value)
  137. {
  138. setParameter(name, value);
  139. }
  140. void ServiceRequest::setBodyParameter(const ParameterNameType &name, const ParameterValueType &value)
  141. {
  142. body_params_[name] = value;
  143. }
  144. void ServiceRequest::setParameters(const ParameterCollection &params)
  145. {
  146. params_ = params;
  147. }
  148. void ServiceRequest::setJsonParameters(const ParameterNameType &name, const ParameterCollection &params)
  149. {
  150. params_ = params;
  151. params_ = params;
  152. setParameter(name, AlibabaCloud::MapToJson(params));
  153. }
  154. std::string ServiceRequest::version() const
  155. {
  156. return version_;
  157. }
  158. HttpRequest::Method ServiceRequest::method() const
  159. {
  160. return method_;
  161. }
  162. void ServiceRequest::setVersion(const std::string &version)
  163. {
  164. version_ = version;
  165. }
  166. std::string ServiceRequest::product() const
  167. {
  168. return product_;
  169. }
  170. void ServiceRequest::setProduct(const std::string &product)
  171. {
  172. product_ = product;
  173. }
  174. std::string ServiceRequest::resourcePath() const
  175. {
  176. return resourcePath_;
  177. }
  178. void ServiceRequest::setResourcePath(const std::string &path)
  179. {
  180. resourcePath_ = path;
  181. }
  182. void ServiceRequest::setScheme(const std::string scheme)
  183. {
  184. scheme_ = scheme;
  185. }
  186. std::string ServiceRequest::scheme() const
  187. {
  188. return scheme_;
  189. }
  190. long ServiceRequest::connectTimeout() const
  191. {
  192. return connectTimeout_;
  193. }
  194. long ServiceRequest::readTimeout() const
  195. {
  196. return readTimeout_;
  197. }
  198. void ServiceRequest::setConnectTimeout(const long connectTimeout)
  199. {
  200. connectTimeout_ = connectTimeout;
  201. }
  202. void ServiceRequest::setReadTimeout(const long readTimeout)
  203. {
  204. readTimeout_ = readTimeout;
  205. }
  206. void ServiceRequest::setMethod(const HttpRequest::Method method)
  207. {
  208. method_ = method;
  209. }
  210. void ServiceRequest::setHeader(const ServiceRequest::ParameterNameType &name,
  211. const ServiceRequest::ParameterValueType &value)
  212. {
  213. headers_[name] = value;
  214. }
  215. ServiceRequest::ParameterValueType ServiceRequest::getHeader(const ServiceRequest::ParameterNameType &name)
  216. {
  217. return headers_[name];
  218. }
  219. void ServiceRequest::removeHeader(const ServiceRequest::ParameterNameType &name)
  220. {
  221. headers_.erase(name);
  222. }
  223. ServiceRequest::ParameterCollection ServiceRequest::headers() const
  224. {
  225. return headers_;
  226. }
  227. } // namespace AlibabaCloud