ServiceRequest.cc 5.9 KB

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