ServiceRequest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. {
  32. }
  33. ServiceRequest::ServiceRequest(const ServiceRequest &other) : content_(nullptr),
  34. contentSize_(other.contentSize_),
  35. params_(other.params_),
  36. product_(other.product_),
  37. resourcePath_(other.resourcePath_),
  38. version_(other.version_),
  39. scheme_(other.scheme_),
  40. connectTimeout_(other.connectTimeout_),
  41. readTimeout_(other.readTimeout_)
  42. {
  43. setContent(other.content_, other.contentSize_);
  44. }
  45. ServiceRequest::ServiceRequest(ServiceRequest &&other)
  46. {
  47. *this = std::move(other);
  48. }
  49. ServiceRequest &ServiceRequest::operator=(const ServiceRequest &other)
  50. {
  51. if (this != &other)
  52. {
  53. content_ = nullptr;
  54. contentSize_ = 0;
  55. params_ = other.params_;
  56. connectTimeout_ = other.connectTimeout_;
  57. readTimeout_ = other.readTimeout_;
  58. setContent(other.content_, other.contentSize_);
  59. }
  60. return *this;
  61. }
  62. ServiceRequest &ServiceRequest::operator=(ServiceRequest &&other)
  63. {
  64. if (this != &other)
  65. *this = std::move(other);
  66. return *this;
  67. }
  68. ServiceRequest::~ServiceRequest()
  69. {
  70. if (content_)
  71. delete content_;
  72. }
  73. const char *ServiceRequest::content() const
  74. {
  75. return content_;
  76. }
  77. size_t ServiceRequest::contentSize() const
  78. {
  79. return contentSize_;
  80. }
  81. bool ServiceRequest::hasContent() const
  82. {
  83. return (contentSize_ != 0);
  84. }
  85. void ServiceRequest::setContent(const char *data, size_t size)
  86. {
  87. if (content_)
  88. delete content_;
  89. content_ = nullptr;
  90. contentSize_ = 0;
  91. if (size)
  92. {
  93. contentSize_ = size;
  94. content_ = new char[size];
  95. std::copy(data, data + size, content_);
  96. }
  97. }
  98. void ServiceRequest::addParameter(const ParameterNameType &name,
  99. const ParameterValueType &value)
  100. {
  101. setParameter(name, value);
  102. }
  103. ServiceRequest::ParameterValueType ServiceRequest::parameter(
  104. const ParameterNameType &name) const
  105. {
  106. ParameterCollection::const_iterator it = params_.find(name);
  107. if (it == params_.end())
  108. {
  109. return ParameterValueType("");
  110. }
  111. return it->second;
  112. }
  113. ServiceRequest::ParameterValueType ServiceRequest::coreParameter(
  114. const ParameterNameType &name) const
  115. {
  116. return parameter(name);
  117. }
  118. ServiceRequest::ParameterCollection ServiceRequest::parameters() const
  119. {
  120. return params_;
  121. }
  122. void ServiceRequest::removeParameter(const ParameterNameType &name)
  123. {
  124. params_.erase(name);
  125. }
  126. void ServiceRequest::setParameter(const ParameterNameType &name,
  127. const ParameterValueType &value)
  128. {
  129. params_[name] = value;
  130. }
  131. void ServiceRequest::setCoreParameter(const ParameterNameType &name, const ParameterValueType &value)
  132. {
  133. setParameter(name, value);
  134. }
  135. void ServiceRequest::setParameters(const ParameterCollection &params)
  136. {
  137. params_ = params;
  138. }
  139. void ServiceRequest::setJsonParameters(const ParameterNameType &name, const ParameterCollection &params)
  140. {
  141. params_ = params;
  142. params_ = params;
  143. setCoreParameter(name, AlibabaCloud::MapToJson(params));
  144. }
  145. std::string ServiceRequest::version() const
  146. {
  147. return version_;
  148. }
  149. void ServiceRequest::setVersion(const std::string &version)
  150. {
  151. version_ = version;
  152. }
  153. std::string ServiceRequest::product() const
  154. {
  155. return product_;
  156. }
  157. void ServiceRequest::setProduct(const std::string &product)
  158. {
  159. product_ = product;
  160. }
  161. std::string ServiceRequest::resourcePath() const
  162. {
  163. return resourcePath_;
  164. }
  165. void ServiceRequest::setResourcePath(const std::string &path)
  166. {
  167. resourcePath_ = path;
  168. }
  169. void ServiceRequest::setScheme(const std::string scheme)
  170. {
  171. scheme_ = scheme;
  172. }
  173. std::string ServiceRequest::scheme() const
  174. {
  175. return scheme_;
  176. }
  177. long ServiceRequest::connectTimeout() const
  178. {
  179. return connectTimeout_;
  180. }
  181. long ServiceRequest::readTimeout() const
  182. {
  183. return readTimeout_;
  184. }
  185. void ServiceRequest::setConnectTimeout(const long connectTimeout)
  186. {
  187. connectTimeout_ = connectTimeout;
  188. }
  189. void ServiceRequest::setReadTimeout(const long readTimeout)
  190. {
  191. readTimeout_ = readTimeout;
  192. }
  193. } // namespace AlibabaCloud