Horizon
http_client.hpp
1 #pragma once
2 #include "nlohmann/json.hpp"
3 #include <curl/curl.h>
4 #include <string>
5 
6 namespace HTTP {
7 class Client {
8  friend size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp);
9 
10 public:
11  Client();
12  void set_auth(const std::string &user, const std::string &passwd);
13  void set_timeout(int timeout);
14  void append_header(const char *header);
15  void append_header(const std::string &header)
16  {
17  append_header(header.c_str());
18  }
19 
20  std::string get(const std::string &url);
21  std::string post(const std::string &url, const std::string &postdata = "");
22 
23  ~Client();
24 
25 private:
26  CURL *curl = nullptr;
27  curl_slist *header_list = nullptr;
28  char errbuf[CURL_ERROR_SIZE];
29 
30  std::string response;
31  std::string postdata;
32 
33  class PostBuffer {
34  public:
35  const char *readptr = nullptr;
36  size_t sizeleft = 0;
37  };
38  PostBuffer post_buffer;
39 };
40 
41 using json = nlohmann::json;
42 
43 class RESTClient : public HTTP::Client {
44 public:
45  RESTClient(const std::string &base);
46 
47  json get(const std::string &url);
48  json post(const std::string &url, const json &postdata = json());
49 
50 private:
51  const std::string base_url;
52 };
53 } // namespace HTTP
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61
HTTP::Client
Definition: http_client.hpp:7
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:161
HTTP::RESTClient
Definition: http_client.hpp:43