Skip to content

Commit 3775710

Browse files
author
a-pavlov
committed
Prepare templates for get, create, update, list, delete funcrions, add implementation for Product
1 parent b74f5e9 commit 3775710

10 files changed

Lines changed: 152 additions & 56 deletions

File tree

client_demo/netlicensing_client_demo.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,25 @@ int main(int argc, char* argv[]) {
2828
Context ctx;
2929
ctx.set_base_url("https://go.netlicensing.io/core/v2/rest/");
3030
ctx.set_username("demo");
31-
ctx.set_password("demo");
32-
long http_code;
33-
std::string res = ctx.post("license", params, http_code);
34-
std::cout << "license check answer: " << res << std::endl;
31+
ctx.set_password("demo");
3532

36-
//std::string lres = ctx.get("licensetemplate", Context::parameters_type());
37-
//std::cout << "licensee list " << lres << std::endl;
33+
// product section
34+
netlicensing::Product p;
35+
p.name_ = "Test name";
36+
p.number_ = "Some number 5";
37+
netlicensing::Product newp = netlicensing::create(ctx, p);
3838

39-
//std::string ldel = ctx.del("licensetemplate/E00101-DEMO", Context::parameters_type());
40-
//std::cout << "delete license " << ldel << std::endl;
39+
newp.name_ = "Updated name";
40+
netlicensing::Product newp2 = netlicensing::update(ctx, newp.number_, newp);
41+
42+
std::list<netlicensing::Product> products = netlicensing::list<netlicensing::Product>(ctx, "");
43+
std::cout << "before delete products count " << products.size() << std::endl;
44+
45+
netlicensing::del<netlicensing::Product>(ctx, newp2.number_, false);
46+
47+
products = netlicensing::list<netlicensing::Product>(ctx, "");
48+
std::cout << "after delete products count " << products.size() << std::endl;
49+
4150
if (!license_number.empty()) {
4251
std::cout << "start validation for " << license_number << std::endl;
4352
std::list<netlicensing::ValidationResult> vres = netlicensing::validate(ctx, license_number);

include/netlicensing/constants.h

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
#ifndef __CONSTANTS_HPP__
2-
#define __CONSTANTS_HPP__
1+
#ifndef __CONSTANTS_H__
2+
#define __CONSTANTS_H__
3+
4+
#include <list>
5+
#include <string>
36

47
namespace netlicensing {
5-
const char* ID = "id";
6-
const char* ACTIVE = "active";
7-
const char* NUMBER = "number";
8-
const char* NAME = "name";
9-
const char* VERSION = "version";
10-
const char* DELETED = "deleted";
11-
const char* CASCADE = "forceCascade";
12-
const char* PRICE = "price";
13-
const char* DISCOUNT = "discount";
14-
const char* CURRENCY = "currency";
15-
const char* IN_USE = "inUse";
16-
const char* FILTER = "filter";
17-
const char* BASE_URL = "baseUrl";
18-
const char* USERNAME = "username";
19-
const char* PASSWORD = "password";
20-
const char* SECURITY_MODE = "securityMode";
21-
const char* PROP_ID = "ID";
22-
const char* PROP_TTL = "TTL";
8+
static const char* ID = "id";
9+
static const char* ACTIVE = "active";
10+
static const char* NUMBER = "number";
11+
static const char* NAME = "name";
12+
static const char* VERSION = "version";
13+
static const char* DELETED = "deleted";
14+
static const char* CASCADE = "forceCascade";
15+
static const char* PRICE = "price";
16+
static const char* DISCOUNT = "discount";
17+
static const char* CURRENCY = "currency";
18+
static const char* IN_USE = "inUse";
19+
static const char* FILTER = "filter";
20+
static const char* BASE_URL = "baseUrl";
21+
static const char* USERNAME = "username";
22+
static const char* PASSWORD = "password";
23+
static const char* SECURITY_MODE = "securityMode";
24+
static const char* PROP_ID = "ID";
25+
static const char* PROP_TTL = "TTL";
26+
27+
typedef std::list<std::pair<std::string, std::string> > parameters_type;
2328
}
2429

25-
#endif //__CONSTANTS_HPP__
30+
#endif //__CONSTANTS_H__

include/netlicensing/context.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#ifndef __CONTEXT_H__
22
#define __CONTEXT_H__
33

4-
#include <string>
5-
#include <list>
4+
#include "netlicensing/constants.h"
65

76
namespace netlicensing {
87

@@ -16,8 +15,7 @@ class Context {
1615
BASIC_AUTHENTICATION,
1716
APIKEY_IDENTIFICATION
1817
};
19-
20-
typedef std::list<std::pair<std::string, std::string> > parameters_type;
18+
2119
Context();
2220
~Context();
2321

@@ -55,7 +53,12 @@ class Context {
5553
};
5654

5755
// for unit testing
58-
extern std::string url_with_parameters(const std::string& url, const Context::parameters_type& params);
56+
extern std::string url_with_parameters(const std::string& url, const parameters_type& params);
57+
58+
/**
59+
* @brief returns url encoded string
60+
*/
61+
extern std::string escape_string(const std::string& s);
5962
}
6063

6164
#endif //__CONTEXT_H__

include/netlicensing/entity.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#ifndef __ENTITY_H__
22
#define __ENTITY_H__
33

4-
#include <string>
5-
#include <list>
4+
#include "netlicensing/constants.h"
65
#include <map>
76
#include <memory>
87
#include <stdexcept>
@@ -70,6 +69,7 @@ class Entity {
7069
bool add_property(const std::string& key, const std::string& value);
7170
std::string get_property(const std::string& key) const;
7271
std::string to_string() const;
72+
parameters_type to_parameters_list() const;
7373
};
7474

7575
}

include/netlicensing/product.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __PRODUCT_H__
33

44
#include "netlicensing/entity.h"
5+
#include "netlicensing/constants.h"
56

67
namespace netlicensing {
78

@@ -26,6 +27,7 @@ struct Product : public Entity {
2627

2728
void add_list(std::shared_ptr<PropertyType> ptr);
2829
void add_property(const std::string& name, const std::string& value);
30+
parameters_type to_parameters_list() const;
2931
};
3032

3133
}

include/netlicensing/service.h

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,86 @@ template<class T>
1313
std::string endpoint();
1414

1515
template<class T>
16-
T get(Context& ctx) {
17-
T res;
18-
//std::string res = ctx.get();
19-
return res;
16+
T get(Context& ctx, const std::string& number) {
17+
long http_code;
18+
std::string res = ctx.get(endpoint<T>() + "/" + escape_string(number), parameters_type(), http_code);
19+
Mapper<T> mp;
20+
traverse(mp, res);
21+
if (http_code != 200) {
22+
throw RestException(mp.info_, http_code);
23+
}
24+
25+
return mp.info_.front();
26+
};
27+
28+
template<class T>
29+
void del(Context& ctx, const std::string& number, bool force_cascade) {
30+
parameters_type params;
31+
if (force_cascade) {
32+
params.push_back(std::make_pair(CASCADE, "true"));
33+
}
34+
35+
long http_code;
36+
ctx.del(endpoint<T>() + "/" + escape_string(number), params, http_code);
37+
if (http_code != 200 && http_code != 204) {
38+
throw RestException(std::list<Info>(), http_code);
39+
}
2040
};
2141

2242
template<class T>
23-
T create(Context& ctx) {
24-
T item;
25-
//ctx.post(endpoint<T>(), );
43+
T update_create(Context& ctx, const std::string& number, const T& value) {
44+
long http_code;
45+
std::string ep = endpoint<T>() + (number.empty()?"":("/" + escape_string(number)));
46+
std::string res = ctx.post(ep, value.to_parameters_list(), http_code);
47+
Mapper<T> mp;
48+
traverse(mp, res);
49+
50+
if (http_code != 200) {
51+
throw RestException(mp.info_, http_code);
52+
}
53+
54+
return mp.items.front();
2655
};
2756

57+
template<class T>
58+
T update(Context& ctx, const std::string& number, const T& value) {
59+
return update_create(ctx, number, value);
60+
}
61+
62+
template<class T>
63+
T create(Context& ctx, const T& value) {
64+
return update_create(ctx, std::string(), value);
65+
}
66+
67+
template<class T>
68+
std::list<T> list(Context& ctx, const std::string& filter) {
69+
parameters_type params;
70+
if (!filter.empty()) {
71+
params.push_back(std::make_pair(FILTER, escape_string(filter)));
72+
}
73+
74+
long http_code;
75+
std::string res = ctx.get(endpoint<T>(), params, http_code);
76+
Mapper<T> mp;
77+
traverse(mp, res);
78+
79+
// TODO(a-pavlov) fix code checking
80+
if (http_code != 200) {
81+
throw RestException(mp.info_, http_code);
82+
}
83+
84+
return mp.items;
85+
}
86+
2887
inline std::list<ValidationResult> validate(Context& ctx,
2988
const std::string& licensee_number,
3089
const std::string& product_number = std::string(),
3190
const std::string& licensee_name = std::string()) {
32-
std::string endpoint = "licensee/" + licensee_number + "/validate";
33-
Context::parameters_type params;
34-
if (!product_number.empty()) params.push_back(std::make_pair("productNumber", product_number));
35-
if (!licensee_name.empty()) params.push_back(std::make_pair("licenseeName", licensee_name));
91+
std::string endpoint = "licensee/" + escape_string(licensee_number) + "/validate";
92+
parameters_type params;
93+
if (!product_number.empty()) params.push_back(std::make_pair("productNumber", escape_string(product_number)));
94+
if (!licensee_name.empty()) params.push_back(std::make_pair("licenseeName", escape_string(licensee_name)));
95+
3696
long http_code;
3797
std::string res = ctx.get(endpoint, params, http_code);
3898
Mapper<ValidationResult> mp;

src/context.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ std::string escape_string(const std::string& s) {
5656
}
5757

5858

59-
std::string url_with_parameters(const std::string& url, const Context::parameters_type& params) {
59+
std::string url_with_parameters(const std::string& url, const parameters_type& params) {
6060

6161
std::string query_part;
6262

@@ -152,23 +152,23 @@ class Context::NetworkService {
152152
#define MAKE_HEADER(name, value) (std::string(name) + ": " + std::string(value)).c_str()
153153

154154
std::string post(const std::string& url,
155-
const Context::parameters_type& params,
155+
const parameters_type& params,
156156
const std::string username,
157157
const std::string password,
158158
long& http_code) {
159159
return send_request(POST, url, params, username, password, http_code);
160160
}
161161

162162
std::string get(const std::string& url,
163-
const Context::parameters_type& params,
163+
const parameters_type& params,
164164
const std::string username,
165165
const std::string password,
166166
long& http_code) {
167-
return send_request(GET, url_with_parameters(url, params), Context::parameters_type(), username, password, http_code);
167+
return send_request(GET, url_with_parameters(url, params), parameters_type(), username, password, http_code);
168168
}
169169

170170
std::string del(const std::string& url,
171-
const Context::parameters_type& params,
171+
const parameters_type& params,
172172
const std::string username,
173173
const std::string password,
174174
long& http_code) {
@@ -178,7 +178,7 @@ class Context::NetworkService {
178178
std::string send_request(
179179
RequestType type,
180180
const std::string& url,
181-
const Context::parameters_type& params,
181+
const parameters_type& params,
182182
const std::string username,
183183
const std::string password,
184184
long& http_code) {

src/entity.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ std::string Entity::to_string() const {
2323
return ss.str();
2424
}
2525

26+
parameters_type Entity::to_parameters_list() const {
27+
parameters_type res;
28+
for (auto p : user_defined_properties_) {
29+
res.push_back(std::make_pair(p.first, p.second));
30+
}
31+
32+
return res;
33+
}
34+
2635
}

src/product.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ void Product::add_property(const std::string& name, const std::string& value) {
2121
else Entity::add_property(name, value);
2222
}
2323

24+
parameters_type Product::to_parameters_list() const {
25+
parameters_type params;
26+
params.push_back(std::make_pair("number", number_));
27+
params.push_back(std::make_pair("name", name_));
28+
params.splice(params.end(), Entity::to_parameters_list());
29+
return params;
30+
}
31+
2432
}

tests/test_context.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ BOOST_AUTO_TEST_SUITE(test_context)
99

1010
BOOST_AUTO_TEST_CASE(test_params_to_query) {
1111
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()));
12+
BOOST_CHECK_EQUAL("http:///xxx/v/", url_with_parameters("http:///xxx/v/", netlicensing::parameters_type()));
13+
BOOST_CHECK_EQUAL("http:///xxx/vvvv", url_with_parameters("http:///xxx/vvvv", netlicensing::parameters_type()));
1414

15-
netlicensing::Context::parameters_type params;
15+
netlicensing::parameters_type params;
1616
params.push_back(std::make_pair("k1", "p1"));
1717
BOOST_CHECK_EQUAL("http://host?k1=p1", url_with_parameters("http://host", params));
1818
params.push_back(std::make_pair("k2", "p2"));

0 commit comments

Comments
 (0)