@@ -13,26 +13,86 @@ template<class T>
1313std::string endpoint ();
1414
1515template <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
2242template <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+
2887inline 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;
0 commit comments