JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
JodaClient.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 19/12/17.
3 //
4 
5 #ifndef JODA_JODACLIENT_H
6 #define JODA_JODACLIENT_H
7 #include <httplib.h>
8 #include <rapidjson/document.h>
9 #include <string>
10 #include "../../../../../extern/cpp-httplib/httplib.h"
11 
13 #define JODA_CLIENT_NON_INTERACTIVE_BATCH_SIZE 5000
14 #define JODA_CLIENT_TIMEOUT 600
15 
16 class JodaClientException : public std::runtime_error {
17  public:
18  explicit JodaClientException(const std::string &what)
19  : runtime_error("Client Error: " + what){};
20 };
21 
22 class JodaServerException : public std::runtime_error {
23  public:
24  explicit JodaServerException(const std::string &what)
25  : runtime_error("Server Error: " + what){};
26 };
27 
28 class JodaClient {
29  public:
30  JodaClient(const std::string &addr, int port);
31  // Interactive
32  void cli();
33  // Non-Interactive
34  void nonInteractiveQuery(const std::string &query, unsigned long offset,
35  unsigned long count);
36 
37  static rapidjson::Document parseResponse(httplib::Response &res);
38  static void checkError(const rapidjson::Document &doc);
39  static constexpr size_t VERSION = 2;
40 
41  protected:
42  std::string query;
43  httplib::Client client;
44 
45  static constexpr auto prefix = "/api/v2";
46 
47  static void handleError(JodaClientException &e);
48  static void handleError(JodaServerException &e);
49 
50  std::string &ltrim(std::string &str);
51  std::string &rtrim(std::string &str);
52  std::string parseCommand();
53 
54  static void getStringWindowSize(const std::string &str, int &y, int &x);
55  void browseReturn(unsigned long id, unsigned long max);
56 };
57 
59  public:
61  : JodaClientException("Got no response, server online?"){};
62 };
63 
65  public:
66  explicit JodaServerHTTPException(int status)
67  : JodaServerException("Got status " + std::to_string(status)){};
68 };
69 
71  public:
72  explicit JodaServerAPIErrorException(const std::string &what)
73  : JodaServerException("API returned error: " + what){};
74 };
75 
77  public:
78  explicit JodaServerInvalidJSONException(const std::string &what)
79  : JodaServerException("Could not parse response: " + what){};
80 };
81 } // namespace joda::network::client
82 
83 #endif // JODA_JODACLIENT_H
JodaClientException(const std::string &what)
Definition: JodaClient.h:18
JodaClientNoResponseException()
Definition: JodaClient.h:60
Definition: JodaClient.h:28
static void getStringWindowSize(const std::string &str, int &y, int &x)
Definition: JodaClient.cpp:216
std::string query
Definition: JodaClient.h:42
void browseReturn(unsigned long id, unsigned long max)
Definition: JodaClient.cpp:63
static constexpr auto prefix
Definition: JodaClient.h:45
static rapidjson::Document parseResponse(httplib::Response &res)
Definition: JodaClient.cpp:313
void cli()
Definition: JodaClient.cpp:19
static void checkError(const rapidjson::Document &doc)
Definition: JodaClient.cpp:299
static constexpr size_t VERSION
Definition: JodaClient.h:39
std::string & rtrim(std::string &str)
Definition: JodaClient.cpp:48
httplib::Client client
Definition: JodaClient.h:43
std::string parseCommand()
Definition: JodaClient.cpp:56
std::string & ltrim(std::string &str)
Definition: JodaClient.cpp:40
void nonInteractiveQuery(const std::string &query, unsigned long offset, unsigned long count)
Definition: JodaClient.cpp:234
JodaClient(const std::string &addr, int port)
Definition: JodaClient.cpp:283
static void handleError(JodaClientException &e)
Definition: JodaClient.cpp:289
JodaServerAPIErrorException(const std::string &what)
Definition: JodaClient.h:72
JodaServerException(const std::string &what)
Definition: JodaClient.h:24
JodaServerHTTPException(int status)
Definition: JodaClient.h:66
JodaServerInvalidJSONException(const std::string &what)
Definition: JodaClient.h:78
Definition: JodaClient.h:12