2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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 # ==================================================================================
18 #include"RestClient.h"
21 void cpprestclient::SetbaseUrl(std::string url)
23 this->baseUrl = utility::conversions::to_string_t(url);
26 cpprestclient::cpprestclient(std::string base_url) {
27 this->baseUrl = utility::conversions::to_string_t(base_url);
28 this->Baseurl = base_url;
30 cpprestclient::cpprestclient(std::string base_url, std::function<void(web::http::http_request)>callback):listener{ utility::conversions::to_string_t(resp_url) }
32 this->baseUrl= utility::conversions::to_string_t(base_url);
33 this->Baseurl = base_url;
36 listener.support(web::http::methods::POST, [callback](web::http::http_request request) { callback(request); });
40 t = new std::thread*[1];
41 t[0] = new std::thread(&cpprestclient::response_listener, this);
48 catch (std::exception const & e)
50 std::wcout << e.what() << std::endl;
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) }{
56 this->baseUrl= utility::conversions::to_string_t(base_url);
57 this->Baseurl = base_url;
60 listener.support(web::http::methods::POST, [callback](web::http::http_request request) { callback(request); });
63 t = new std::thread*[1];
64 t[0] = new std::thread(&cpprestclient::response_listener, this);
72 catch (std::exception const & e)
74 std::wcout << e.what() << std::endl;
82 void cpprestclient::start_response_listener() {
84 t[0] = new std::thread(&cpprestclient::response_listener, this);
87 catch (std::exception const & e)
89 std::wcout << e.what() << std::endl;
93 std::string cpprestclient::getBaseUrl()
97 void cpprestclient::response_listener() {
100 this->listener.open().wait();
101 //std::cout<<"kumar "<<"\n";
102 //std::this_thread::sleep_for(std::chrono::seconds(20));
105 //std::cout<<"sandeeep ";
111 catch (std::exception const & e)
113 std::wcout << e.what() << std::endl;
116 void cpprestclient::stop_response_listener() {
122 std::cout << "\nclosed1\n";
124 catch (std::exception const & e)
126 std::wcout << e.what() << std::endl;
129 oresponse_t cpprestclient::post_subscriptions(const nlohmann::json & json, std::string path) {
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"));
142 .then([&](web::http::http_response response) {
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()));
148 ucout << response.to_string() << "\n";
149 response.headers().set_content_type(U("application/json"));
150 return response.extract_json();
153 .then([&](web::json::value jsonObject) {
154 res.SubscriptionId = utility::conversions::to_utf8string(jsonObject[U("SubscriptionId")].as_string());
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);
164 xapp::model::to_json(ans, SResp);
165 std::cout << ans.dump(4) << "\n";
171 catch (const std::exception& e) {
173 printf("Error exception:%s\n", e.what());
178 int cpprestclient::delete_subscriptions(std::string Id, std::string path)
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);
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()));
201 catch (const std::exception& e) {
202 printf("Error exception:%s\n", e.what());
207 response_t cpprestclient::get_config(std::string path)
211 res.body = utility::conversions::to_string_t("");
212 auto postJson = pplx::create_task([&]() {
215 web::http::client::http_client client(this->baseUrl);
218 return client.request(web::http::methods::GET, web::uri_builder(U("/")).append_path(utility::conversions::to_string_t(path)).to_string());
220 .then([&](web::http::http_response response) {
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()));
226 ucout << response.to_string() << "\n";
227 response.headers().set_content_type(U("application/json"));
228 return response.extract_json();
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);
240 xapp::model::to_json(ans, cfg);
241 std::cout << ans.dump(4) << "\n";
246 catch (const std::exception& e) {
247 printf("Error exception:%s\n", e.what());
251 response_t cpprestclient::get_subscriptions(std::string path)
255 res.body = utility::conversions::to_string_t("");
256 auto postJson = pplx::create_task([&]() {
259 web::http::client::http_client client(this->baseUrl);
262 return client.request(web::http::methods::GET, web::uri_builder(U("/")).append_path(utility::conversions::to_string_t(path)).to_string());
264 .then([&](web::http::http_response response) {
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()));
270 ucout << response.to_string() << "\n";
271 response.headers().set_content_type(U("application/json"));
272 return response.extract_json();
275 .then([&](web::json::value jsonObject) {
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);
285 xapp::model::to_json(ans, subList);
286 std::cout << ans.dump(4) << "\n";
292 catch (const std::exception& e) {
293 printf("Error exception:%s\n", e.what());
297 response_t cpprestclient::do_post(const nlohmann::json & json, std::string path)
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"));
312 .then([&](web::http::http_response response) {
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()));
318 ucout << response.to_string() << "\n";
319 response.headers().set_content_type(U("application/json"));
320 return response.extract_json();
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();
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);
335 catch (const std::exception& e) {
336 printf("Error exception:%s\n", e.what());
340 response_t cpprestclient::do_del(std::string Id, std::string path)
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);
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()));
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;
365 .then([&](web::json::value jsonObject) {
367 //resp.body = jsonObject.serialize();
368 std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
369 res.body = nlohmann::json::parse(s);
375 catch (const std::exception& e) {
376 printf("Error exception:%s\n", e.what());
382 response_t cpprestclient::do_get(std::string path)
386 res.body = utility::conversions::to_string_t("");
387 auto postJson = pplx::create_task([&]() {
390 web::http::client::http_client client(this->baseUrl);
393 return client.request(web::http::methods::GET, web::uri_builder(U("/")).append_path(utility::conversions::to_string_t(path)).to_string());
395 .then([&](web::http::http_response response) {
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()));
401 ucout << response.to_string() << "\n";
402 response.headers().set_content_type(U("application/json"));
403 return response.extract_json();
406 .then([&](web::json::value jsonObject) {
408 //resp.body = jsonObject.serialize();
409 std::string s = utility::conversions::to_utf8string(jsonObject.serialize());
410 res.body = nlohmann::json::parse(s);
415 catch (const std::exception& e) {
416 printf("Error exception:%s\n", e.what());