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> 38 using json = nlohmann::json;
56 Service(
const std::string& host,
const uint16_t& port,
int timeout = 60);
70 template <
typename Request,
typename Response,
typename... Args>
72 auto request = Request{ args... };
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 });
84 if (res.error.code != cpr::ErrorCode::OK)
91 resJson = json::parse(res.text);
93 if (resJson.count(
"error")) {
94 error = resJson[
"error"].get<decltype(error)>();
96 }
catch (
const std::runtime_error&) {
97 if (res.elapsed >= timeout_) {
98 throw Errors::Network(
"Time out after " + std::to_string(timeout_) +
"s");
105 switch (res.status_code) {
107 return Response{ resJson };
115 if (res.elapsed >= timeout_) {
116 throw Errors::Network(
"Time out after " + std::to_string(timeout_) +
"s");
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