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