2020-08-20 01:08:57 +00:00
|
|
|
#include "http.hpp"
|
|
|
|
|
2020-09-21 22:47:34 +00:00
|
|
|
//#define USE_LOCAL_PROXY
|
2020-08-20 01:08:57 +00:00
|
|
|
HTTPClient::HTTPClient(std::string api_base)
|
2020-10-06 01:37:07 +00:00
|
|
|
: m_api_base(api_base) {
|
|
|
|
m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks));
|
|
|
|
}
|
2020-08-20 01:08:57 +00:00
|
|
|
|
2020-11-27 06:37:01 +00:00
|
|
|
void HTTPClient::SetUserAgent(std::string agent) {
|
|
|
|
m_agent = agent;
|
|
|
|
}
|
|
|
|
|
2020-08-20 01:08:57 +00:00
|
|
|
void HTTPClient::SetAuth(std::string auth) {
|
|
|
|
m_authorization = auth;
|
|
|
|
}
|
|
|
|
|
2020-08-30 05:59:14 +00:00
|
|
|
void HTTPClient::MakeDELETE(std::string path, std::function<void(cpr::Response r)> cb) {
|
|
|
|
printf("DELETE %s\n", path.c_str());
|
|
|
|
auto url = cpr::Url { m_api_base + path };
|
|
|
|
auto headers = cpr::Header {
|
|
|
|
{ "Authorization", m_authorization },
|
|
|
|
};
|
2020-11-27 06:37:01 +00:00
|
|
|
auto ua = cpr::UserAgent {m_agent != "" ? m_agent : "Abaddon" };
|
|
|
|
|
2020-08-30 05:59:14 +00:00
|
|
|
#ifdef USE_LOCAL_PROXY
|
2020-09-20 07:01:53 +00:00
|
|
|
m_futures.push_back(cpr::DeleteCallback(
|
2020-08-30 05:59:14 +00:00
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, ua,
|
2020-08-30 05:59:14 +00:00
|
|
|
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
|
|
|
|
cpr::VerifySsl { false }));
|
|
|
|
#else
|
|
|
|
m_futures.push_back(cpr::DeleteCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, ua));
|
2020-08-30 05:59:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-20 01:08:57 +00:00
|
|
|
void HTTPClient::MakePATCH(std::string path, std::string payload, std::function<void(cpr::Response r)> cb) {
|
|
|
|
printf("PATCH %s\n", path.c_str());
|
|
|
|
auto url = cpr::Url { m_api_base + path };
|
|
|
|
auto headers = cpr::Header {
|
|
|
|
{ "Authorization", m_authorization },
|
|
|
|
{ "Content-Type", "application/json" },
|
|
|
|
};
|
2020-11-27 06:37:01 +00:00
|
|
|
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
|
|
|
|
|
2020-08-20 01:08:57 +00:00
|
|
|
auto body = cpr::Body { payload };
|
|
|
|
#ifdef USE_LOCAL_PROXY
|
|
|
|
m_futures.push_back(cpr::PatchCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, body, ua,
|
2020-08-20 01:08:57 +00:00
|
|
|
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
|
|
|
|
cpr::VerifySsl { false }));
|
|
|
|
#else
|
|
|
|
m_futures.push_back(cpr::PatchCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, body, ua));
|
2020-08-20 01:08:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPClient::MakePOST(std::string path, std::string payload, std::function<void(cpr::Response r)> cb) {
|
|
|
|
printf("POST %s\n", path.c_str());
|
|
|
|
auto url = cpr::Url { m_api_base + path };
|
|
|
|
auto headers = cpr::Header {
|
|
|
|
{ "Authorization", m_authorization },
|
|
|
|
{ "Content-Type", "application/json" },
|
|
|
|
};
|
2020-11-27 06:37:01 +00:00
|
|
|
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
|
|
|
|
|
2020-08-20 01:08:57 +00:00
|
|
|
auto body = cpr::Body { payload };
|
2020-08-20 07:19:16 +00:00
|
|
|
#ifdef USE_LOCAL_PROXY
|
2020-08-22 02:25:23 +00:00
|
|
|
m_futures.push_back(cpr::PostCallback(
|
2020-08-20 07:19:16 +00:00
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, body, ua,
|
2020-08-20 07:19:16 +00:00
|
|
|
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
|
|
|
|
cpr::VerifySsl { false }));
|
|
|
|
#else
|
2020-08-22 02:25:23 +00:00
|
|
|
m_futures.push_back(cpr::PostCallback(
|
2020-08-20 07:19:16 +00:00
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, body, ua));
|
2020-08-20 07:19:16 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-12 22:17:53 +00:00
|
|
|
void HTTPClient::MakePUT(std::string path, std::string payload, std::function<void(cpr::Response r)> cb) {
|
|
|
|
printf("PUT %s\n", path.c_str());
|
|
|
|
auto url = cpr::Url { m_api_base + path };
|
|
|
|
auto headers = cpr::Header {
|
|
|
|
{ "Authorization", m_authorization },
|
|
|
|
{ "Content-Type", "application/json" },
|
|
|
|
};
|
2020-11-27 06:37:01 +00:00
|
|
|
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
|
|
|
|
|
2020-10-12 22:17:53 +00:00
|
|
|
auto body = cpr::Body { payload };
|
|
|
|
#ifdef USE_LOCAL_PROXY
|
|
|
|
m_futures.push_back(cpr::PutCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, body, ua,
|
2020-10-12 22:17:53 +00:00
|
|
|
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
|
|
|
|
cpr::VerifySsl { false }));
|
|
|
|
#else
|
|
|
|
m_futures.push_back(cpr::PutCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, body, ua));
|
2020-10-12 22:17:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-20 07:19:16 +00:00
|
|
|
void HTTPClient::MakeGET(std::string path, std::function<void(cpr::Response r)> cb) {
|
2020-08-22 02:25:23 +00:00
|
|
|
printf("GET %s\n", path.c_str());
|
2020-08-20 07:19:16 +00:00
|
|
|
auto url = cpr::Url { m_api_base + path };
|
|
|
|
auto headers = cpr::Header {
|
|
|
|
{ "Authorization", m_authorization },
|
|
|
|
{ "Content-Type", "application/json" },
|
|
|
|
};
|
2020-11-27 06:37:01 +00:00
|
|
|
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
|
|
|
|
|
|
|
|
auto x = cpr::UserAgent {};
|
2020-08-20 07:19:16 +00:00
|
|
|
#ifdef USE_LOCAL_PROXY
|
|
|
|
m_futures.push_back(cpr::GetCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, ua,
|
2020-08-20 07:19:16 +00:00
|
|
|
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
|
|
|
|
cpr::VerifySsl { false }));
|
|
|
|
#else
|
|
|
|
m_futures.push_back(cpr::GetCallback(
|
|
|
|
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
|
2020-11-27 06:37:01 +00:00
|
|
|
url, headers, ua));
|
2020-08-20 07:19:16 +00:00
|
|
|
#endif
|
2020-08-20 01:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPClient::CleanupFutures() {
|
|
|
|
for (auto it = m_futures.begin(); it != m_futures.end();) {
|
|
|
|
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
|
|
|
it = m_futures.erase(it);
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 01:37:07 +00:00
|
|
|
void HTTPClient::RunCallbacks() {
|
|
|
|
m_mutex.lock();
|
|
|
|
m_queue.front()();
|
|
|
|
m_queue.pop();
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2020-08-20 01:08:57 +00:00
|
|
|
void HTTPClient::OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb) {
|
|
|
|
CleanupFutures();
|
2020-09-06 00:59:17 +00:00
|
|
|
try {
|
2020-10-06 01:37:07 +00:00
|
|
|
m_mutex.lock();
|
|
|
|
m_queue.push([this, r, cb] { cb(r); });
|
|
|
|
m_dispatcher.emit();
|
|
|
|
m_mutex.unlock();
|
2020-09-06 00:59:17 +00:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
fprintf(stderr, "error handling response (%s, code %d): %s\n", r.url.c_str(), r.status_code, e.what());
|
|
|
|
}
|
2020-08-20 01:08:57 +00:00
|
|
|
}
|