MotionLib  1.0.0
SamBuCa motion library
Client.cpp
1 /*
2 ** Copyright (C) 2022 CERN
3 **
4 ** This software is provided 'as-is', without any express or implied
5 ** warranty. In no event will the authors be held liable for any damages
6 ** arising from the use of this software.
7 **
8 ** Permission is granted to anyone to use this software for any purpose,
9 ** including commercial applications, and to alter it and redistribute it
10 ** freely, subject to the following restrictions:
11 **
12 ** 1. The origin of this software must not be misrepresented; you must not
13 ** claim that you wrote the original software. If you use this software
14 ** in a product, an acknowledgment in the product documentation would be
15 ** appreciated but is not required.
16 ** 2. Altered source versions must be plainly marked as such, and must not be
17 ** misrepresented as being the original software.
18 ** 3. This notice may not be removed or altered from any source distribution.
19 **
20 ** Created on: 2022-07-14T18:27:29
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "Client.hpp"
25 
26 #include <oatpp/network/tcp/client/ConnectionProvider.hpp>
27 #include <oatpp/parser/json/mapping/ObjectMapper.hpp>
28 #include <oatpp/web/client/HttpRequestExecutor.hpp>
29 
30 namespace smc {
31 namespace debug {
32 
33 Client::Client(const oatpp::network::Address &address) :
34  oatpp::web::client::ApiClient(
35  oatpp::web::client::HttpRequestExecutor::createShared(
36  oatpp::network::tcp::client::ConnectionProvider::createShared(
37  address)),
38  oatpp::parser::json::mapping::ObjectMapper::createShared())
39 {}
40 
41 std::shared_ptr<oatpp::web::protocol::http::incoming::Response> Client::put(
42  const std::string &path,
43  const oatpp::Void &body,
44  const std::unordered_map<oatpp::String, oatpp::String> &pathParams)
45 {
46  oatpp::data::share::StringTemplate pathTemplate = parsePathTemplate(
47  "put", oatpp::String(path));
48  oatpp::web::client::ApiClient::Headers headers;
49  std::unordered_map<oatpp::String, oatpp::String> queryParams;
50  std::shared_ptr<oatpp::web::protocol::http::outgoing::Body> bodyVoid;
51  if (bool(body))
52  {
53  bodyVoid = oatpp::web::protocol::http::outgoing::BufferBody::createShared(
54  m_objectMapper->writeToString(body),
55  m_objectMapper->getInfo().http_content_type);
56  }
57  return executeRequest("PUT", pathTemplate, headers, pathParams, queryParams,
58  bodyVoid);
59 }
60 
61 std::shared_ptr<oatpp::web::protocol::http::incoming::Response> Client::post(
62  const std::string &path,
63  const oatpp::Void &body,
64  const std::unordered_map<oatpp::String, oatpp::String> &pathParams)
65 {
66  oatpp::data::share::StringTemplate pathTemplate = parsePathTemplate(
67  "post", oatpp::String(path));
68  oatpp::web::client::ApiClient::Headers headers;
69  std::unordered_map<oatpp::String, oatpp::String> queryParams;
70  std::shared_ptr<oatpp::web::protocol::http::outgoing::Body> bodyVoid;
71  if (bool(body))
72  {
73  bodyVoid = oatpp::web::protocol::http::outgoing::BufferBody::createShared(
74  m_objectMapper->writeToString(body),
75  m_objectMapper->getInfo().http_content_type);
76  }
77  return executeRequest("POST", pathTemplate, headers, pathParams,
78  queryParams, bodyVoid);
79 }
80 
81 std::shared_ptr<oatpp::web::protocol::http::incoming::Response> Client::get(
82  const std::string &path,
83  const std::unordered_map<oatpp::String, oatpp::String> &queryParams,
84  const std::unordered_map<oatpp::String, oatpp::String> &pathParams)
85 {
86  oatpp::data::share::StringTemplate pathTemplate = parsePathTemplate(
87  "get", oatpp::String(path));
88  oatpp::web::client::ApiClient::Headers headers;
89  std::shared_ptr<oatpp::web::protocol::http::outgoing::Body> body;
90  return executeRequest("GET", pathTemplate, headers, pathParams, queryParams,
91  body);
92 }
93 
94 std::shared_ptr<oatpp::web::protocol::http::incoming::Response> Client::del(
95  const std::string &path,
96  const std::unordered_map<oatpp::String, oatpp::String> &queryParams,
97  const std::unordered_map<oatpp::String, oatpp::String> &pathParams)
98 {
99  oatpp::data::share::StringTemplate pathTemplate = parsePathTemplate(
100  "delete", oatpp::String(path));
101  oatpp::web::client::ApiClient::Headers headers;
102  std::shared_ptr<oatpp::web::protocol::http::outgoing::Body> body;
103  return executeRequest("DELETE", pathTemplate, headers, pathParams,
104  queryParams, body);
105 }
106 
107 } // namespace debug
108 } // namespace smc
main motion-lib namespace
Definition: Client.cpp:30