Skip to content

Commit de591bf

Browse files
author
a-pavlov
committed
Add params to get request, add http code returns for requests
1 parent b773156 commit de591bf

5 files changed

Lines changed: 76 additions & 58 deletions

File tree

client_demo/netlicensing_client_demo.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
int main(int argc, char* argv[]) {
99
using netlicensing::Product;
10-
std::string license_number;
10+
std::string license_number = "I2C3VN7NA-DEMO";
1111
if (argc > 1) {
1212
license_number = argv[1];
1313
}
@@ -29,7 +29,8 @@ int main(int argc, char* argv[]) {
2929
ctx.set_base_url("https://go.netlicensing.io/core/v2/rest/");
3030
ctx.set_username("demo");
3131
ctx.set_password("demo");
32-
std::string res = ctx.post("license", params);
32+
long http_code;
33+
std::string res = ctx.post("license", params, http_code);
3334
std::cout << "license check answer: " << res << std::endl;
3435

3536
//std::string lres = ctx.get("licensetemplate", Context::parameters_type());
@@ -43,8 +44,6 @@ int main(int argc, char* argv[]) {
4344
std::cout << "got validation results: " << vres.size() << std::endl;
4445
for (auto val_res : vres) {
4546
std::cout << val_res.to_string() << std::endl;
46-
//std::cout << "lic model: {" << val_res.licensing_model_ << "} prod mod name {" << val_res.product_module_name_ << "}"
47-
// << " properties size{" << val_res.properties_.size() << "}\n";
4847
}
4948
}
5049
}

include/netlicensing/context.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class Context {
3535
std::string vendor_number() const;
3636
SecurityMode security_mode() const;
3737

38-
std::string post(const std::string& endpoint, const parameters_type& params);
39-
std::string get(const std::string& endpoint, const parameters_type& params);
40-
std::string del(const std::string& endpoint, const parameters_type& params);
38+
std::string post(const std::string& endpoint, const parameters_type& params, long& http_code);
39+
std::string get(const std::string& endpoint, const parameters_type& params, long& http_code);
40+
std::string del(const std::string& endpoint, const parameters_type& params, long& http_code);
4141
private:
4242
const std::string& user() const;
4343
const std::string& pass() const;
@@ -53,6 +53,9 @@ class Context {
5353
std::string vendor_number_;
5454
SecurityMode mode_;
5555
};
56+
57+
// for unit testing
58+
extern std::string url_with_parameters(const std::string& url, const Context::parameters_type& params);
5659
}
5760

5861
#endif //__CONTEXT_H__

include/netlicensing/service.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ T create(Context& ctx) {
2424
//ctx.post(endpoint<T>(), );
2525
};
2626

27-
inline std::list<ValidationResult> validate(Context& ctx, const std::string& licensee_number) {
27+
inline std::list<ValidationResult> validate(Context& ctx,
28+
const std::string& licensee_number,
29+
const std::string& product_number = std::string(),
30+
const std::string& licensee_name = std::string()) {
2831
std::string endpoint = "licensee/" + licensee_number + "/validate";
29-
std::string res = ctx.get(endpoint, Context::parameters_type());
32+
Context::parameters_type params;
33+
if (!product_number.empty()) params.push_back(std::make_pair("productNumber", product_number));
34+
if (!licensee_name.empty()) params.push_back(std::make_pair("licenseeName", licensee_name));
35+
long http_code;
36+
std::string res = ctx.get(endpoint, params, http_code);
3037
Mapper<ValidationResult> mp;
3138
traverse(mp, res);
3239
return mp.items;

src/context.cc

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5977
struct 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_; }
329315
std::string Context::vendor_number() const { return vendor_number_; }
330316
Context::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

350336
const std::string& Context::user() const {

tests/test_context.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef WIN32
2+
#define BOOST_TEST_DYN_LINK
3+
#endif
4+
5+
#include <boost/test/unit_test.hpp>
6+
#include "netlicensing/context.h"
7+
8+
BOOST_AUTO_TEST_SUITE(test_context)
9+
10+
BOOST_AUTO_TEST_CASE(test_params_to_query) {
11+
using netlicensing::url_with_parameters;
12+
BOOST_CHECK_EQUAL("http:///xxx/v/", url_with_parameters("http:///xxx/v/", netlicensing::Context::parameters_type()));
13+
BOOST_CHECK_EQUAL("http:///xxx/vvvv", url_with_parameters("http:///xxx/vvvv", netlicensing::Context::parameters_type()));
14+
15+
netlicensing::Context::parameters_type params;
16+
params.push_back(std::make_pair("k1", "p1"));
17+
BOOST_CHECK_EQUAL("http://host?k1=p1", url_with_parameters("http://host", params));
18+
params.push_back(std::make_pair("k2", "p2"));
19+
BOOST_CHECK_EQUAL("http://host?k1=p1&k2=p2", url_with_parameters("http://host", params));
20+
BOOST_CHECK_EQUAL("https://host?k1=p1&k2=p2", url_with_parameters("https://host", params));
21+
}
22+
23+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)