1 #ifndef HTTPINTERFACE_H_
2 #define HTTPINTERFACE_H_
12 #include "logging/logging.h"
13 #include "utilities/utils.h"
15 using CurlHandler = std::shared_ptr<CURL>;
19 HttpResponse(std::string body_in,
const long http_status_code_in,
20 CURLcode curl_code_in, std::string error_message_in)
21 : body(std::move(body_in)),
22 http_status_code(http_status_code_in),
23 curl_code(curl_code_in),
24 error_message(std::move(error_message_in)) {}
26 long http_status_code{0};
27 CURLcode curl_code{CURLE_OK};
28 std::string error_message;
29 bool isOk()
const {
return (curl_code == CURLE_OK && http_status_code >= 200 && http_status_code < 400); }
30 bool wasInterrupted()
const {
return curl_code == CURLE_ABORTED_BY_CALLBACK; };
31 std::string getStatusStr()
const {
32 return std::to_string(curl_code) +
" " + error_message +
" HTTP " + std::to_string(http_status_code);
35 Json::Value getJson()
const {
return Utils::parseJSON(body); }
42 virtual HttpResponse get(
const std::string &url, int64_t maxsize) = 0;
43 virtual HttpResponse post(
const std::string &url,
const std::string &content_type,
const std::string &
data) = 0;
44 virtual HttpResponse post(
const std::string &url,
const Json::Value &
data) = 0;
45 virtual HttpResponse put(
const std::string &url,
const std::string &content_type,
const std::string &
data) = 0;
46 virtual HttpResponse put(
const std::string &url,
const Json::Value &
data) = 0;
48 virtual HttpResponse download(
const std::string &url, curl_write_callback write_cb,
49 curl_xferinfo_callback progress_cb,
void *userp, curl_off_t from) = 0;
50 virtual std::future<HttpResponse> downloadAsync(
const std::string &url, curl_write_callback write_cb,
51 curl_xferinfo_callback progress_cb,
void *userp, curl_off_t from,
52 CurlHandler *easyp) = 0;
53 virtual void setCerts(
const std::string &ca, CryptoSource ca_source,
const std::string &cert,
54 CryptoSource cert_source,
const std::string &pkey, CryptoSource pkey_source) = 0;
55 static constexpr int64_t kNoLimit = 0;
56 static constexpr int64_t kPostRespLimit = 64 * 1024;
57 static constexpr int64_t kPutRespLimit = 64 * 1024;
60 #endif // HTTPINTERFACE_H_