RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / rest-client / RestClient.cpp
1 /*
2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
17 */
18 #include"RestClient.h"
19 namespace xapp
20 {
21         void cpprestclient::SetbaseUrl(std::string url)
22         {
23                 this->baseUrl = utility::conversions::to_string_t(url);
24                 this->Baseurl = url;
25         }
26         cpprestclient::cpprestclient(std::string base_url) {
27                 this->baseUrl = utility::conversions::to_string_t(base_url);
28                 this->Baseurl = base_url;
29         }
30         cpprestclient::cpprestclient(std::string base_url, std::function<void(web::http::http_request)>callback):listener{ utility::conversions::to_string_t(resp_url) }
31         {
32                 this->baseUrl= utility::conversions::to_string_t(base_url);
33                 this->Baseurl = base_url;
34                 try
35                 {
36                         listener.support(web::http::methods::POST, [callback](web::http::http_request request) { callback(request); });
37                         ok_2_run = true;
38                         /*
39                         ok_2_run = true;
40                         t = new std::thread*[1];
41                         t[0] = new std::thread(&cpprestclient::response_listener, this);
42                         t[0]->detach();
43                         */
44                         response_listener();
45
46                         
47                 }
48                 catch (std::exception const & e)
49                 {
50                         std::wcout << e.what() << std::endl;
51                 }
52
53         }
54         cpprestclient::cpprestclient(std::string base_url,std::string response_url, std::function<void(web::http::http_request)>callback):listener{ utility::conversions::to_string_t(response_url) }{
55                 
56                 this->baseUrl= utility::conversions::to_string_t(base_url);
57                 this->Baseurl = base_url;
58                 try
59                 {
60                         listener.support(web::http::methods::POST, [callback](web::http::http_request request) { callback(request); });
61                         /*
62                         ok_2_run = true;
63                         t = new std::thread*[1];
64                         t[0] = new std::thread(&cpprestclient::response_listener, this);
65                         t[0]->detach();
66                         */
67                         response_listener();
68
69
70
71                 }
72                 catch (std::exception const & e)
73                 {
74                         std::wcout << e.what() << std::endl;
75                 }
76
77
78
79         }
80
81         /*
82         void cpprestclient::start_response_listener() {
83                 try {
84                         t[0] = new std::thread(&cpprestclient::response_listener, this);
85                         t[0]->detach();
86                 }
87                 catch (std::exception const & e)
88                 {
89                         std::wcout << e.what() << std::endl;
90                 }
91         }
92         */
93         std::string cpprestclient::getBaseUrl()
94         {
95                 return Baseurl;
96         }
97         void cpprestclient::response_listener() {
98                 try
99                 {
100                         this->listener.open().wait();
101                         //std::cout<<"kumar "<<"\n";
102                         //std::this_thread::sleep_for(std::chrono::seconds(20));
103                        /*
104                         while (ok_2_run) {
105                                 //std::cout<<"sandeeep ";
106
107                         }
108                         */
109
110                 }
111                 catch (std::exception const & e)
112                 {
113                         std::wcout << e.what() << std::endl;
114                 }
115         }
116         void cpprestclient::stop_response_listener() {
117                 try
118                 {
119                         ok_2_run = false;
120                         listener.close();
121                         //delete[] t;
122                         std::cout << "\nclosed1\n";
123                 }
124                 catch (std::exception const & e)
125                 {
126                         std::wcout << e.what() << std::endl;
127                 }
128         }
129         oresponse_t cpprestclient::post_subscriptions(const nlohmann::json & json, std::string path) {
130                 oresponse_t res;
131                 res.status_code = 0;
132                 res.SubscriptionId = "";
133                 auto postJson = pplx::create_task([&]() {
134                         utility::stringstream_t s;
135                         s << json.dump().c_str();
136                         web::json::value sdk_json = web::json::value::parse(s);
137                         web::uri_builder uri(this->baseUrl + utility::conversions::to_string_t(path));
138                         auto addr = uri.to_uri().to_string();
139                         web::http::client::http_client client(addr);
140                         return client.request(web::http::methods::POST, U("/"), sdk_json.serialize(), U("application/json"));
141                 })
142                         .then([&](web::http::http_response response) {
143
144                         res.status_code = response.status_code();
145                         if (response.status_code() != 201) {
146                                 throw std::runtime_error("Returned " + std::to_string(response.status_code()));
147                         }
148                         ucout << response.to_string() << "\n";
149                         response.headers().set_content_type(U("application/json"));
150                         return response.extract_json();
151                 })
152
153                         .then([&](web::json::value jsonObject) {
154                         res.SubscriptionId = utility::conversions::to_utf8string(jsonObject[U("SubscriptionId")].as_string());
155
156                         //validating subscription response against its schema
157                         std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
158                         nlohmann::json  j= nlohmann::json::parse(s);
159                         xapp::model::SubscriptionResponse SResp;
160                         xapp::model::from_json(j, SResp);
161                         
162                         //sanity check
163                         nlohmann::json ans;
164                         xapp::model::to_json(ans, SResp);
165                         std::cout << ans.dump(4) << "\n";
166
167                 });
168                 try {
169                         postJson.wait();
170                 }
171                 catch (const std::exception& e) {
172                         
173                         printf("Error exception:%s\n", e.what());
174                 }
175                 return res;
176
177         }
178         int  cpprestclient::delete_subscriptions(std::string Id, std::string path)
179         {
180                 int status_code=0;
181                 auto delJson = pplx::create_task([&]() {
182                         web::uri_builder uri(this->baseUrl + utility::conversions::to_string_t(path));
183                         auto addr = uri.to_uri().to_string();
184                         web::http::client::http_client client(addr);
185                         addr.append(utility::conversions::to_string_t(Id));
186                         //ucout << utility::string_t(U("making requests at: ")) << addr << std::endl;
187                         return client.request(web::http::methods::DEL);
188                 })
189
190
191                         .then([&](web::http::http_response response) {
192                         status_code = response.status_code();
193                         if (response.status_code() != 204) {
194                                 throw std::runtime_error("Returned " + std::to_string(response.status_code()));
195                         }
196                 });
197
198                 try {
199                         delJson.wait();
200                 }
201                 catch (const std::exception& e) {
202                         printf("Error exception:%s\n", e.what());
203                 }
204
205                 return status_code;
206         }
207         response_t  cpprestclient::get_config(std::string path)
208         {
209                 response_t res;
210                 res.status_code = 0;
211                 res.body = utility::conversions::to_string_t("");
212                 auto postJson = pplx::create_task([&]() {
213
214
215                         web::http::client::http_client client(this->baseUrl);
216
217
218                         return client.request(web::http::methods::GET, web::uri_builder(U("/")).append_path(utility::conversions::to_string_t(path)).to_string());
219                 })
220                         .then([&](web::http::http_response response) {
221
222                         res.status_code = response.status_code();
223                         if (response.status_code() != 200) {
224                                 throw std::runtime_error("Returned " + std::to_string(response.status_code()));
225                         }
226                         ucout << response.to_string() << "\n";
227                         response.headers().set_content_type(U("application/json"));
228                         return response.extract_json();
229                 })
230
231                         .then([&](web::json::value jsonObject) {
232                         //validating configlist against its schema
233                         std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
234                         res.body = nlohmann::json::parse(s);
235                         std::vector<xapp::model::XAppConfig> cfg;
236                         xapp::model::from_json(res.body, cfg);
237                         
238                         //sanity check
239                         nlohmann::json ans;
240                         xapp::model::to_json(ans, cfg);
241                         std::cout << ans.dump(4) << "\n";
242                 });
243                 try {
244                         postJson.wait();
245                 }
246                 catch (const std::exception& e) {
247                         printf("Error exception:%s\n", e.what());
248                 }
249                 return res;
250         }
251         response_t  cpprestclient::get_subscriptions(std::string path)
252         {
253                 response_t res;
254                 res.status_code = 0;
255                 res.body = utility::conversions::to_string_t("");
256                 auto postJson = pplx::create_task([&]() {
257
258
259                         web::http::client::http_client client(this->baseUrl);
260
261
262                         return client.request(web::http::methods::GET, web::uri_builder(U("/")).append_path(utility::conversions::to_string_t(path)).to_string());
263                 })
264                         .then([&](web::http::http_response response) {
265
266                         res.status_code = response.status_code();
267                         if (response.status_code() != 200) {
268                                 throw std::runtime_error("Returned " + std::to_string(response.status_code()));
269                         }
270                         ucout << response.to_string() << "\n";
271                         response.headers().set_content_type(U("application/json"));
272                         return response.extract_json();
273                 })
274
275                         .then([&](web::json::value jsonObject) {
276
277                         //validating subscription list  against its schema
278                         std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
279                         res.body = nlohmann::json::parse(s);
280                         std::vector<xapp::model::SubscriptionData> subList;
281                         xapp::model::from_json(res.body, subList);
282
283                         //sanity check
284                         nlohmann::json ans;
285                         xapp::model::to_json(ans, subList);
286                         std::cout << ans.dump(4) << "\n";
287
288                 });
289                 try {
290                         postJson.wait();
291                 }
292                 catch (const std::exception& e) {
293                         printf("Error exception:%s\n", e.what());
294                 }
295                 return res;
296         }
297         response_t cpprestclient::do_post(const nlohmann::json & json, std::string path)
298         {
299                 response_t res;
300                 res.status_code = 0;
301                 res.body = utility::conversions::to_string_t("");
302                 auto postJson = pplx::create_task([&]() {
303                         //conversion from nlhomann json to cpprestsdk json
304                         utility::stringstream_t s;
305                         s << json.dump().c_str();
306                         web::json::value sdk_json = web::json::value::parse(s);
307                         web::uri_builder uri(this->baseUrl + utility::conversions::to_string_t(path));
308                         auto addr = uri.to_uri().to_string();
309                         web::http::client::http_client client(addr);
310                         return client.request(web::http::methods::POST, U("/"), sdk_json.serialize(), U("application/json"));
311                 })
312                                 .then([&](web::http::http_response response) {
313
314                                         res.status_code = response.status_code();
315                                         if (response.status_code() != 201) {
316                                         throw std::runtime_error("Returned " + std::to_string(response.status_code()));
317                                         }
318                                         ucout << response.to_string() << "\n";
319                                         response.headers().set_content_type(U("application/json"));
320                                         return response.extract_json();
321                         })
322                                 
323                                 .then([&](web::json::value jsonObject) {
324                                         //std::cout << "\nRecieved REST subscription response\n";
325                                         //std::wcout << jsonObject.serialize().c_str() << "\n";
326                                         //resp.body = jsonObject.serialize();
327
328                                 //conversion from cpprestsdk json to nlhomann json
329                                 std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
330                                 res.body = nlohmann::json::parse(s);
331                         });
332                         try {
333                                 postJson.wait();
334                         }
335                         catch (const std::exception& e) {
336                                 printf("Error exception:%s\n", e.what());
337                         }
338                         return res;
339         }
340         response_t cpprestclient::do_del(std::string Id, std::string path)
341         {
342                 response_t res;
343                 res.status_code = 0;
344                 res.body = utility::conversions::to_string_t("");
345                 auto delJson = pplx::create_task([&]() {
346                         web::uri_builder uri(this->baseUrl + utility::conversions::to_string_t(path));
347                         auto addr = uri.to_uri().to_string();
348                         web::http::client::http_client client(addr);
349                         addr.append(utility::conversions::to_string_t(Id));
350                         //ucout << utility::string_t(U("making requests at: ")) << addr << std::endl;
351                         return client.request(web::http::methods::DEL);
352                 })
353
354                         
355                         .then([&](web::http::http_response response) {
356                                 res.status_code = response.status_code();
357                                 if (response.status_code() != 204) {
358                                         throw std::runtime_error("Returned " + std::to_string(response.status_code()));
359                                 }
360                                 ucout << response.to_string() << "\n";
361                                 response.headers().set_content_type(U("application/json"));
362                                 return response.extract_json();
363                                 //std::wcout << "Deleted: " << std::boolalpha << (response.status_code() == 204) << std::endl;
364                 })
365                         .then([&](web::json::value jsonObject) {
366
367                                 //resp.body = jsonObject.serialize();
368                         std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
369                         res.body = nlohmann::json::parse(s);
370                 });
371
372                 try {
373                         delJson.wait();
374                 }
375                 catch (const std::exception& e) {
376                         printf("Error exception:%s\n", e.what());
377                 }
378
379                 return res;
380
381         }
382          response_t cpprestclient::do_get(std::string path)
383         {
384                 response_t res;
385                 res.status_code = 0;
386                 res.body = utility::conversions::to_string_t("");
387                 auto postJson = pplx::create_task([&]() {
388
389
390                         web::http::client::http_client client(this->baseUrl);
391
392                         
393                         return client.request(web::http::methods::GET, web::uri_builder(U("/")).append_path(utility::conversions::to_string_t(path)).to_string());
394                 })
395                         .then([&](web::http::http_response response) {
396
397                         res.status_code = response.status_code();
398                         if (response.status_code() != 200) {
399                                 throw std::runtime_error("Returned " + std::to_string(response.status_code()));
400                         }
401                         ucout << response.to_string() << "\n";
402                         response.headers().set_content_type(U("application/json"));
403                         return response.extract_json();
404                 })
405
406                         .then([&](web::json::value jsonObject) {
407         
408                         //resp.body = jsonObject.serialize();
409                         std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
410                         res.body = nlohmann::json::parse(s);
411                 });
412                 try {
413                         postJson.wait();
414                 }
415                 catch (const std::exception& e) {
416                         printf("Error exception:%s\n", e.what());
417                 }
418                 return res;
419         }
420 }
421
422