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