iota.lib.cpp
IOTA C++ Library
service.hpp
1 //
2 // MIT License
3 //
4 // Copyright (c) 2017-2018 Thibault Martinez and Simon Ninon
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //
24 //
25 
26 #pragma once
27 
28 #include <cpr/cpr.h>
29 #include <json.hpp>
30 
31 #include <iota/constants.hpp>
32 #include <iota/errors/bad_request.hpp>
33 #include <iota/errors/internal_server_error.hpp>
34 #include <iota/errors/network.hpp>
35 #include <iota/errors/unauthorized.hpp>
36 #include <iota/errors/unrecognized.hpp>
37 
38 using json = nlohmann::json;
39 
40 namespace IOTA {
41 
42 namespace API {
43 
47 class Service {
48 public:
56  Service(const std::string& host, const uint16_t& port, int timeout = 60);
60  virtual ~Service() = default;
61 
62 public:
70  template <typename Request, typename Response, typename... Args>
71  Response request(Args&&... args) const {
72  auto request = Request{ args... };
73 
74  json data;
75  request.serialize(data);
76 
77  auto url = cpr::Url{ "http://" + host_ + ":" + std::to_string(port_) };
78  auto body = cpr::Body{ data.dump() };
79  auto headers = cpr::Header{ { "Content-Type", "application/json" },
80  { "Content-Length", std::to_string(body.size()) },
81  { "X-IOTA-API-Version", APIVersion } };
82  auto res = cpr::Post(url, body, headers, cpr::Timeout{ timeout_ * 1000 });
83 
84  if (res.error.code != cpr::ErrorCode::OK)
85  throw Errors::Network(res.error.message);
86 
87  json resJson;
88  std::string error;
89 
90  try {
91  resJson = json::parse(res.text);
92 
93  if (resJson.count("error")) {
94  error = resJson["error"].get<decltype(error)>();
95  }
96  } catch (const std::runtime_error&) {
97  if (res.elapsed >= timeout_) {
98  throw Errors::Network("Time out after " + std::to_string(timeout_) + "s");
99  }
100 
101  throw Errors::Unrecognized("Invalid reply from node (unrecognized format): " + res.text);
102  }
103 
104  Response response;
105  switch (res.status_code) {
106  case 200:
107  return Response{ resJson };
108  case 400:
109  throw Errors::BadRequest(error);
110  case 401:
111  throw Errors::Unauthorized(error);
112  case 500:
113  throw Errors::InternalServerError(error);
114  default:
115  if (res.elapsed >= timeout_) {
116  throw Errors::Network("Time out after " + std::to_string(timeout_) + "s");
117  }
118 
119  throw Errors::Unrecognized(error);
120  }
121 
122  return response;
123  }
124 
125 private:
129  std::string host_;
133  unsigned int port_;
137  const int timeout_;
138 };
139 
140 } // namespace API
141 
142 } // namespace IOTA
virtual ~Service()=default
Definition: unrecognized.hpp:37
Definition: network.hpp:37
Definition: unauthorized.hpp:37
Definition: bad_request.hpp:37
Definition: service.hpp:47
Service(const std::string &host, const uint16_t &port, int timeout=60)
Definition: internal_server_error.hpp:37
Response request(Args &&... args) const
Definition: service.hpp:71
Definition: core.hpp:33