@@ -55,6 +55,24 @@ std::string escape_string(const std::string& s) {
5555 return escape_string (s.c_str (), s.length ());
5656}
5757
58+
59+ std::string url_with_parameters (const std::string& url, const Context::parameters_type& params) {
60+
61+ std::string query_part;
62+
63+ char prefix = ' ?' ;
64+
65+ if (url.find (' ?' ) != std::string::npos) prefix = ' &' ;
66+
67+ for (auto pval : params) {
68+ query_part += prefix;
69+ query_part += pval.first + " =" + escape_string (pval.second );
70+ prefix = ' &' ;
71+ }
72+
73+ return url + query_part;
74+ }
75+
5876// pair to string functor
5977struct pair2string {
6078 std::string operator ()(const std::pair<std::string, std::string>& p) {
@@ -136,30 +154,34 @@ class Context::NetworkService {
136154 std::string post (const std::string& url,
137155 const Context::parameters_type& params,
138156 const std::string username,
139- const std::string password) {
140- return send_request (POST , url, params, username, password);
157+ const std::string password,
158+ long & http_code) {
159+ return send_request (POST , url, params, username, password, http_code);
141160 }
142161
143162 std::string get (const std::string& url,
144163 const Context::parameters_type& params,
145164 const std::string username,
146- const std::string password) {
147- return send_request (GET , url, params, username, password);
165+ const std::string password,
166+ long & http_code) {
167+ return send_request (GET , url_with_parameters (url, params), Context::parameters_type (), username, password, http_code);
148168 }
149169
150170 std::string del (const std::string& url,
151171 const Context::parameters_type& params,
152172 const std::string username,
153- const std::string password) {
154- return send_request (DEL , url, params, username, password);
173+ const std::string password,
174+ long & http_code) {
175+ return send_request (DEL , url, params, username, password, http_code);
155176 }
156177
157178 std::string send_request (
158179 RequestType type,
159180 const std::string& url,
160181 const Context::parameters_type& params,
161182 const std::string username,
162- const std::string password) {
183+ const std::string password,
184+ long & http_code) {
163185 std::string response_body;
164186 std::string request_body;
165187 std::pair<const char *, size_t > request_body_info;
@@ -235,43 +257,7 @@ class Context::NetworkService {
235257
236258 if (res != 0 ) throw ServiceException (curl_easy_strerror (res), res, errors);
237259
238- // TODO(a-pavlov) validate error handlers from restful-mapper
239- long http_code = 0 ;
240260 curl_easy_getinfo (handle_, CURLINFO_RESPONSE_CODE , &http_code);
241-
242- // bad request or validation error
243- if (http_code == 400 ) {
244- // TODO(a-pavlov) activate this after response parser ready
245- // throw RestException(response_body, http_code);
246- return response_body;
247- }
248-
249- if (http_code == 401 ) throw RestException (" Authentication error" , http_code);
250-
251- bool http_ok = false ;
252-
253- switch (type) {
254- case GET :
255- http_ok = (http_code == 200 );
256- break ;
257- case POST :
258- http_ok = (http_code == 201 );
259- break ;
260- case PUT :
261- http_ok = (http_code == 200 || http_code == 201 );
262- break ;
263- case DEL :
264- http_ok = (http_code == 204 );
265- break ;
266- default :
267- break ;
268- }
269-
270- if (!http_ok) {
271- // TODO(a-pavlov) activate when json parser ready
272- throw RestException (response_body, http_code);
273- }
274-
275261 return response_body;
276262 }
277263
@@ -329,22 +315,22 @@ std::string Context::api_key() const { return api_key_; }
329315std::string Context::vendor_number () const { return vendor_number_; }
330316Context::SecurityMode Context::security_mode () const { return mode_; }
331317
332- std::string Context::post (const std::string& endpoint, const parameters_type& params) {
318+ std::string Context::post (const std::string& endpoint, const parameters_type& params, long & http_code ) {
333319 assert (!endpoint.empty ());
334320 assert (network_service_ != NULL );
335- return network_service_->post (base_url_ + " /" + endpoint, params, user (), pass ());
321+ return network_service_->post (base_url_ + " /" + endpoint, params, user (), pass (), http_code );
336322}
337323
338- std::string Context::get (const std::string& endpoint, const parameters_type& params) {
324+ std::string Context::get (const std::string& endpoint, const parameters_type& params, long & http_code ) {
339325 assert (!endpoint.empty ());
340326 assert (network_service_ != NULL );
341- return network_service_->get (base_url_ + " /" + endpoint, params, user (), pass ());
327+ return network_service_->get (base_url_ + " /" + endpoint, params, user (), pass (), http_code );
342328}
343329
344- std::string Context::del (const std::string& endpoint, const parameters_type& params) {
330+ std::string Context::del (const std::string& endpoint, const parameters_type& params, long & http_code ) {
345331 assert (!endpoint.empty ());
346332 assert (network_service_ != NULL );
347- return network_service_->del (base_url_ + " /" + endpoint, params, user (), pass ());
333+ return network_service_->del (base_url_ + " /" + endpoint, params, user (), pass (), http_code );
348334}
349335
350336const std::string& Context::user () const {
0 commit comments