rest_helper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  *
4  * Author: Monika Florek-Jasinska
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  * may be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "rest_helper.h"
34 #include "rest_exceptions.h"
35 #include <cpr/cpr.h>
36 
37 #include <exception>
38 #include <iostream>
39 
40 namespace rc_rest_api
41 {
42 using std::endl;
43 using std::string;
44 using std::stringstream;
45 
46 static string toString(cpr::Response resp)
47 {
48  stringstream s;
49  s << "status code: " << resp.status_code << endl
50  << "url: " << resp.url << endl
51  << "text: " << resp.text << endl
52  << "error: " << resp.error.message;
53  return s.str();
54 }
55 
56 static void handleCPRResponse(const cpr::Response& r)
57 {
58  if (r.status_code != 200)
59  {
60  throw RequestException(toString(r));
61  }
62 }
63 
64 RestHelper::RestHelper(const string& host, const string& node_name, int timeout)
65  : host_(host)
66  , services_url_("http://" + host + "/api/v1/nodes/" + node_name + "/services/")
67  , params_url_("http://" + host + "/api/v1/nodes/" + node_name + "/parameters")
68  , version_url_("http://" + host + "/api/v1/system")
69  , timeout_curl_(timeout)
70 {
71  int num_tries = 5;
72  bool comm_established = false;
73 
74  while (!comm_established)
75  {
76  const auto response = cpr::Get(version_url_, cpr::Timeout{ timeout_curl_ });
77  if (response.status_code == 200)
78  {
79  comm_established = true;
80  }
81  else
82  {
83  num_tries--;
84  if (num_tries == 0)
85  {
86  throw std::runtime_error("Could not connect to rc_visard");
87  }
88  }
89  }
90 }
91 
92 json RestHelper::servicePutRequest(const std::string& service_name)
93 {
94  cpr::Url url = cpr::Url{ services_url_ + service_name };
95  auto rest_resp = cpr::Put(url, cpr::Timeout{ timeout_curl_ });
96  handleCPRResponse(rest_resp);
97  return json::parse(rest_resp.text)["response"];
98 }
99 
100 json RestHelper::servicePutRequest(const std::string& service_name, const json& js_args)
101 {
102  cpr::Url url = cpr::Url{ services_url_ + service_name };
103  nlohmann::json j = nlohmann::json::object();
104  if (!js_args.empty())
105  j["args"] = js_args;
106  auto rest_resp = cpr::Put(url, cpr::Timeout{ timeout_curl_ }, cpr::Body{ j.dump() },
107  cpr::Header{ { "Content-Type", "application/json" } });
108  handleCPRResponse(rest_resp);
109  return json::parse(rest_resp.text)["response"];
110 }
111 
113 {
114  auto rest_resp = cpr::Get(params_url_, cpr::Timeout{ timeout_curl_ });
115  handleCPRResponse(rest_resp);
116  return json::parse(rest_resp.text);
117 }
118 
120 {
121  auto rest_resp = cpr::Put(params_url_, cpr::Timeout{ timeout_curl_ }, cpr::Body{ js_params.dump() },
122  cpr::Header{ { "Content-Type", "application/json" } });
123 
124  handleCPRResponse(rest_resp);
125  return json::parse(rest_resp.text);
126 }
127 
128 std::tuple<size_t, size_t, size_t> RestHelper::getImageVersion()
129 {
130  const auto response = cpr::Get(version_url_, cpr::Timeout{ timeout_curl_ });
131  handleCPRResponse(response);
132  try
133  {
134  const auto j = json::parse(response.text);
135  std::string image_version = j.at("firmware").at("active_image").at("image_version").get<std::string>();
136  const std::string prefix = "rc_visard_v";
137  if (image_version.find(prefix) == 0)
138  {
139  image_version = image_version.substr(prefix.size());
140  }
141  const auto minus_pos = image_version.find('-');
142  if (minus_pos != std::string::npos)
143  {
144  image_version = image_version.substr(0, minus_pos);
145  }
146  std::istringstream iss(image_version);
147  std::string v;
148  std::vector<size_t> version_components;
149  while (std::getline(iss, v, '.'))
150  {
151  version_components.push_back(std::stoul(v));
152  }
153  if (version_components.size() == 3)
154  {
155  auto image_version_tuple = std::make_tuple(version_components[0], version_components[1], version_components[2]);
156  return image_version_tuple;
157  }
158  else
159  {
160  throw std::runtime_error("Could not parse image version");
161  }
162  }
163  catch (const std::exception& ex)
164  {
165  throw MiscException(std::string("Could not parse response: ") + ex.what());
166  }
167 }
168 
169 } // namespace rc_rest_api
static void handleCPRResponse(const cpr::Response &r)
Definition: rest_helper.cpp:56
RestHelper(const std::string &host, const std::string &node_name, int timeout)
Definition: rest_helper.cpp:64
XmlRpcServer s
std::tuple< size_t, size_t, size_t > getImageVersion()
const std::string services_url_
Definition: rest_helper.h:61
json setParameters(const json &js_params)
nlohmann::json json
const std::string params_url_
Definition: rest_helper.h:61
const std::string version_url_
Definition: rest_helper.h:61
static string toString(cpr::Response resp)
Definition: rest_helper.cpp:46
json servicePutRequest(const std::string &service_name)
Definition: rest_helper.cpp:92


rc_hand_eye_calibration_client
Author(s): Christian Emmerich
autogenerated on Sat Feb 13 2021 03:41:55