Skip to content

Commit 4c24700

Browse files
committed
Interface changed to match common NetLicensing API bindings convention. Includes functionality proposed by pull request from TechToast. Some code moved to headers (targeting header-only lib).
1 parent 821cad7 commit 4c24700

27 files changed

Lines changed: 860 additions & 648 deletions

client_demo/netlicensing_client_demo.cc

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include "netlicensing/netlicensing.h"
66

77
int main(int argc, char* argv[]) {
8-
using netlicensing::Product;
9-
std::string licensee_number = "I2C3VN7NA-DEMO";
8+
using namespace netlicensing;
9+
10+
std::string licensee_number = "I5-DEMO";
1011
if (argc > 1) {
1112
licensee_number = argv[1];
1213
}
@@ -18,43 +19,39 @@ int main(int argc, char* argv[]) {
1819
std::string productNumber = ss.str();
1920

2021
std::cout << "Hello, this is NetLicensing demo client\n";
21-
std::cout << "Product endpoint " << netlicensing::endpoint<Product>() << std::endl;
22+
std::cout << "Product endpoint " << endpoint<Product>() << std::endl;
2223
std::cout << "Product test number " << productNumber << std::endl;
2324

24-
using netlicensing::Context;
2525
try {
2626
Context ctx;
2727
ctx.set_base_url("https://go.netlicensing.io/core/v2/rest/");
2828
ctx.set_username("demo");
2929
ctx.set_password("demo");
3030

3131
// product section
32-
netlicensing::Product p;
32+
Product p;
3333
p.setName("Test name");
3434
p.setNumber(productNumber);
35-
netlicensing::Product newp = netlicensing::ProductService::create(ctx, p);
35+
Product newp = ProductService::create(ctx, p);
3636

3737
newp.setName("Updated name");
38-
netlicensing::Product newp2 = netlicensing::ProductService::update(ctx, newp.getNumber(), newp);
38+
Product newp2 = ProductService::update(ctx, newp.getNumber(), newp);
3939

40-
std::list<netlicensing::Product> products = netlicensing::ProductService::list(ctx, "");
40+
std::list<Product> products = ProductService::list(ctx, "");
4141
std::cout << "before delete products count " << products.size() << std::endl;
4242

43-
netlicensing::ProductService::del(ctx, newp2.getNumber(), false);
43+
ProductService::del(ctx, newp2.getNumber(), false);
4444

45-
products = netlicensing::ProductService::list(ctx, "");
45+
products = ProductService::list(ctx, "");
4646
std::cout << "after delete products count " << products.size() << std::endl;
4747

4848
if (!licensee_number.empty()) {
4949
std::cout << "start validation for " << licensee_number << std::endl;
50-
std::list<netlicensing::ValidationResult> vres = netlicensing::ValidationService::validate(ctx, licensee_number);
51-
std::cout << "got validation results: " << vres.size() << std::endl;
52-
for (auto val_res : vres) {
53-
std::cout << val_res.to_string() << std::endl;
54-
}
50+
ValidationResult vres = LicenseeService::validate(ctx, licensee_number);
51+
std::cout << "got validation results:\n" << vres.toString() << std::endl;
5552
}
5653
}
57-
catch (const netlicensing::RestException& e) {
54+
catch (const RestException& e) {
5855
std::cerr << e.what() << " code " << e.http_code() << std::endl;
5956
for (auto det : e.get_details()) {
6057
std::cerr << det.to_string() << std::endl;

include/netlicensing/constants.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace netlicensing {
1010
static const char* NUMBER = "number";
1111
static const char* NAME = "name";
1212
static const char* VERSION = "version";
13+
static const char* LICENSEE_AUTOCREATE = "licenseeAutoCreate";
14+
static const char* DESCRIPTION = "description";
15+
static const char* LICENSING_INFO = "licensingInfo";
1316
static const char* DELETED = "deleted";
1417
static const char* CASCADE = "forceCascade";
1518
static const char* PRICE = "price";
@@ -23,8 +26,6 @@ namespace netlicensing {
2326
static const char* SECURITY_MODE = "securityMode";
2427
static const char* PROP_ID = "ID";
2528
static const char* PROP_TTL = "TTL";
26-
27-
typedef std::list<std::pair<std::string, std::string> > parameters_type;
2829
}
2930

3031
#endif //__CONSTANTS_H__

include/netlicensing/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __CONTEXT_H__
22
#define __CONTEXT_H__
33

4-
#include "netlicensing/constants.h"
4+
#include "netlicensing/converters.h"
55

66
namespace netlicensing {
77

include/netlicensing/converters.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef __CONVERTERS_H__
2+
#define __CONVERTERS_H__
3+
4+
#include "netlicensing/constants.h"
5+
#include "netlicensing/datatypes.h"
6+
#include "netlicensing/entity.h"
7+
#include "netlicensing/licensee.h"
8+
#include "netlicensing/product.h"
9+
10+
namespace netlicensing {
11+
12+
template<typename T>
13+
parameters_type toParametersList(T value);
14+
15+
template<>
16+
inline parameters_type toParametersList<BaseEntity>(BaseEntity value) {
17+
parameters_type params;
18+
params.push_back(std::make_pair(NUMBER, value.getNumber()));
19+
params.push_back(std::make_pair(ACTIVE, value.getActive().toString()));
20+
for (const auto& prop : value.getProperties()) {
21+
params.push_back(prop);
22+
}
23+
return params;
24+
}
25+
26+
template<>
27+
inline parameters_type toParametersList<Product>(Product value) {
28+
parameters_type params = toParametersList<BaseEntity>(value);
29+
params.push_back(std::make_pair(NAME, value.getName()));
30+
params.push_back(std::make_pair(VERSION, value.getVersion()));
31+
params.push_back(std::make_pair(LICENSEE_AUTOCREATE, value.getLicenseeAutoCreate().toString()));
32+
params.push_back(std::make_pair(DESCRIPTION, value.getDescription()));
33+
params.push_back(std::make_pair(LICENSING_INFO, value.getLicensingInfo()));
34+
// TODO(2K): convert discounts
35+
/*
36+
for (const auto& discount : value.getDiscounts()) {
37+
// ...
38+
}
39+
*/
40+
return params;
41+
}
42+
43+
template<>
44+
inline parameters_type toParametersList<Licensee>(Licensee value) {
45+
parameters_type params = toParametersList<BaseEntity>(value);
46+
params.push_back(std::make_pair(NAME, value.getName()));
47+
return params;
48+
}
49+
50+
} // namespace netlicensing
51+
52+
#endif // __CONVERTERS_H__

include/netlicensing/datatypes.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#ifndef __DATATYPES_H__
2+
#define __DATATYPES_H__
3+
4+
#include <stdint.h>
5+
#include <sstream>
6+
#include <string>
7+
8+
#include "netlicensing/exception.h"
9+
10+
namespace netlicensing {
11+
12+
// This is a simplified version of std::optional (or boost::optional) to avoid dependency
13+
// on not yet widely supported C++17 or external library (boost).
14+
template <typename T>
15+
class Optional {
16+
bool available_i;
17+
T value_i;
18+
19+
public:
20+
Optional() : available_i(false), value_i() {}
21+
Optional(T value) : available_i(true), value_i(value) {}
22+
Optional(const char* value);
23+
24+
bool operator()() const { return available_i; }
25+
26+
operator T() const { return value_i; }
27+
28+
std::string toString() const;
29+
};
30+
31+
32+
// TODO(2K): Precision is currently fixed at 2 digits after decimal point, make variable
33+
class FixedPoint {
34+
int64_t value_i;
35+
36+
public:
37+
FixedPoint(const std::string& value) {
38+
if (value == "0") {
39+
value_i = 0;
40+
} else if ((value.length() > 3) && (value[value.length() - 3] == '.')) {
41+
// TODO(2K): too primitive parsing - improve
42+
std::string rawValue = value;
43+
rawValue.erase(value.length() - 3, 1);
44+
auto is = std::istringstream(rawValue);
45+
is >> value_i;
46+
} else {
47+
throw MalformedArgumentsException("Not supported formatting for fixed point value");
48+
}
49+
}
50+
51+
std::string toString() const {
52+
std::ostringstream os;
53+
os << value_i;
54+
std::string sValue = os.str();
55+
sValue.insert(sValue.length() - 2, ".");
56+
return sValue;
57+
}
58+
};
59+
60+
61+
typedef Optional<std::string> String_t;
62+
typedef Optional<FixedPoint> Decimal_t;
63+
typedef Optional<bool> Boolean_t;
64+
65+
template<>
66+
inline Optional<std::string>::Optional(const char* value)
67+
: available_i(true), value_i(value) { }
68+
69+
template<>
70+
inline std::string Optional<std::string>::toString() const {
71+
return value_i;
72+
}
73+
74+
template<>
75+
inline std::string Optional<FixedPoint>::toString() const {
76+
return value_i.toString();
77+
}
78+
79+
template<>
80+
inline Optional<bool>::Optional(const char* value)
81+
: available_i(true), value_i(std::string(value) == "true") { }
82+
83+
template<>
84+
inline std::string Optional<bool>::toString() const {
85+
return value_i ? "true" : "false";
86+
}
87+
88+
typedef std::list<std::pair<std::string, std::string> > parameters_type;
89+
90+
} // namespace netlicensing
91+
92+
#endif // __DATATYPES_H__

include/netlicensing/entity.h

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
11
#ifndef __ENTITY_H__
22
#define __ENTITY_H__
33

4-
#include "netlicensing/constants.h"
54
#include <map>
6-
#include <memory>
7-
#include <stdexcept>
85
#include <sstream>
9-
#include <cassert>
6+
#include <string>
107

11-
namespace netlicensing {
12-
13-
struct NamedList {
14-
std::string name_;
15-
16-
bool add_property(const std::string& name, const std::string& value) {
17-
if (name == "name") {
18-
name_ = value;
19-
return true;
20-
}
21-
22-
return false;
23-
}
24-
};
25-
26-
template<class T>
27-
struct RecursiveList : public NamedList {
28-
std::list<std::shared_ptr<RecursiveList<T> > > nested_lists_;
8+
#include "netlicensing/datatypes.h"
299

30-
void add_list(std::shared_ptr<RecursiveList<T> > ptr) {
31-
nested_lists_.push_back(ptr);
32-
}
33-
};
34-
35-
template<class T>
36-
struct FlatList : public NamedList {
37-
void add_list(std::shared_ptr<FlatList<T> > ptr) {
38-
throw std::logic_error("Flat list is not support nested levels");
39-
}
40-
};
10+
namespace netlicensing {
4111

12+
/*
4213
template<typename S, typename T>
4314
void lexical_cast(T& t, const S& s) {
4415
std::stringstream stream;
@@ -60,17 +31,67 @@ inline void assign(bool& target, const std::string& source) {
6031
inline void assign(int& target, const std::string& source) {
6132
lexical_cast(target, source);
6233
}
34+
*/
6335

64-
class Entity {
36+
class BaseEntity {
6537
private:
66-
typedef std::map<std::string, std::string> user_properties_map;
67-
user_properties_map user_defined_properties_;
38+
String_t number_i;
39+
Boolean_t active_i;
40+
std::map<std::string, String_t> properties_i;
41+
42+
public:
43+
void setNumber(const String_t& number) {
44+
number_i = number;
45+
}
46+
47+
const String_t& getNumber() const {
48+
return number_i;
49+
}
50+
51+
void setActive(Boolean_t active) {
52+
active_i = active;
53+
}
54+
55+
Boolean_t getActive() const {
56+
return active_i;
57+
}
58+
59+
// Methods for working with custom properties
60+
61+
const std::map<std::string, String_t>& getProperties() const {
62+
return properties_i;
63+
}
64+
65+
void addProperty(const std::string& property, String_t value) {
66+
properties_i[property] = value;
67+
}
68+
69+
void removeProperty(const std::string& property) {
70+
properties_i.erase(property);
71+
}
72+
};
73+
74+
/*
75+
class BaseEntityConverter {
76+
BaseEntity& baseEntity_i;
6877
public:
69-
bool add_property(const std::string& key, const std::string& value);
70-
std::string get_property(const std::string& key) const;
71-
std::string to_string() const;
72-
parameters_type to_parameters_list() const;
78+
BaseEntityConverter(BaseEntity& baseEntity) : baseEntity_i(baseEntity) {}
79+
bool convert(const std::string& property, const std::string value) {
80+
if (property == "number") {
81+
baseEntity_i.setNumber(value);
82+
return true;
83+
} else if (property == "active") {
84+
baseEntity_i.setActive(value == "true");
85+
return true;
86+
}
87+
return false;
88+
}
89+
};
90+
91+
template<typename E>
92+
class EntityConverter : public BaseEntityConverter {
7393
};
94+
*/
7495

7596
}
7697

include/netlicensing/exception.h

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

4+
#include <list>
45
#include <stdexcept>
56
#include "netlicensing/info.h"
67

@@ -57,6 +58,6 @@ class MalformedArgumentsException : public std::runtime_error {
5758
MalformedArgumentsException(const std::string& msg) : std::runtime_error(msg) {}
5859
};
5960

60-
};
61+
} // namespace netlicensing
6162

62-
#endif //__EXCEPTION_H__
63+
#endif // __EXCEPTION_H__

0 commit comments

Comments
 (0)