summaryrefslogtreecommitdiff
path: root/discord/httpclient.cpp
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2021-01-23 21:07:03 +0000
committerGitHub <noreply@github.com>2021-01-23 21:07:03 +0000
commitb28bfd6f208bc04b4157130eabfcf61b1825570d (patch)
tree7c2f770d7cda4d48571c8df69dc7460f1aa7bea0 /discord/httpclient.cpp
parent547124c94f8808d6dca407af7b6eb0d29ac12e45 (diff)
downloadabaddon-portaudio-b28bfd6f208bc04b4157130eabfcf61b1825570d.tar.gz
abaddon-portaudio-b28bfd6f208bc04b4157130eabfcf61b1825570d.zip
remove cpr as a dependency (#21)
abstract away library usage
Diffstat (limited to 'discord/httpclient.cpp')
-rw-r--r--discord/httpclient.cpp136
1 files changed, 136 insertions, 0 deletions
diff --git a/discord/httpclient.cpp b/discord/httpclient.cpp
new file mode 100644
index 0000000..dc94566
--- /dev/null
+++ b/discord/httpclient.cpp
@@ -0,0 +1,136 @@
+#include "httpclient.hpp"
+#include "httpclient.hpp"
+
+//#define USE_LOCAL_PROXY
+HTTPClient::HTTPClient(std::string api_base)
+ : m_api_base(api_base) {
+ m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks));
+}
+
+void HTTPClient::SetUserAgent(std::string agent) {
+ m_agent = agent;
+}
+
+void HTTPClient::SetAuth(std::string auth) {
+ m_authorization = auth;
+}
+
+void HTTPClient::MakeDELETE(const std::string &path, std::function<void(http::response_type r)> cb) {
+ printf("DELETE %s\n", path.c_str());
+ m_futures.push_back(std::async(std::launch::async, [this, path, cb] {
+ http::request req(http::REQUEST_DELETE, m_api_base + path);
+ req.set_header("Authorization", m_authorization);
+ req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
+#ifdef USE_LOCAL_PROXY
+ req.set_proxy("http://127.0.0.1:8888");
+ req.set_verify_ssl(false);
+#endif
+
+ auto res = req.execute();
+
+ OnResponse(res, cb);
+ }));
+}
+
+void HTTPClient::MakePATCH(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb) {
+ printf("PATCH %s\n", path.c_str());
+ m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] {
+ http::request req(http::REQUEST_PATCH, m_api_base + path);
+ req.set_header("Authorization", m_authorization);
+ req.set_header("Content-Type", "application/json");
+ req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
+ req.set_body(payload);
+#ifdef USE_LOCAL_PROXY
+ req.set_proxy("http://127.0.0.1:8888");
+ req.set_verify_ssl(false);
+#endif
+
+ auto res = req.execute();
+
+ OnResponse(res, cb);
+ }));
+}
+
+void HTTPClient::MakePOST(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb) {
+ printf("POST %s\n", path.c_str());
+ m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] {
+ http::request req(http::REQUEST_POST, m_api_base + path);
+ req.set_header("Authorization", m_authorization);
+ req.set_header("Content-Type", "application/json");
+ req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
+ req.set_body(payload);
+#ifdef USE_LOCAL_PROXY
+ req.set_proxy("http://127.0.0.1:8888");
+ req.set_verify_ssl(false);
+#endif
+
+ auto res = req.execute();
+
+ OnResponse(res, cb);
+ }));
+}
+
+void HTTPClient::MakePUT(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb) {
+ printf("PUT %s\n", path.c_str());
+ m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] {
+ http::request req(http::REQUEST_PUT, m_api_base + path);
+ req.set_header("Authorization", m_authorization);
+ req.set_header("Content-Type", "application/json");
+ req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
+ req.set_body(payload);
+#ifdef USE_LOCAL_PROXY
+ req.set_proxy("http://127.0.0.1:8888");
+ req.set_verify_ssl(false);
+#endif
+
+ auto res = req.execute();
+
+ OnResponse(res, cb);
+ }));
+}
+
+void HTTPClient::MakeGET(const std::string &path, std::function<void(http::response_type r)> cb) {
+ printf("GET %s\n", path.c_str());
+ m_futures.push_back(std::async(std::launch::async, [this, path, cb] {
+ http::request req(http::REQUEST_GET, m_api_base + path);
+ req.set_header("Authorization", m_authorization);
+ req.set_header("Content-Type", "application/json");
+ req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
+#ifdef USE_LOCAL_PROXY
+ req.set_proxy("http://127.0.0.1:8888");
+ req.set_verify_ssl(false);
+#endif
+
+ auto res = req.execute();
+
+ OnResponse(res, cb);
+ }));
+}
+
+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++;
+ }
+}
+
+void HTTPClient::RunCallbacks() {
+ m_mutex.lock();
+ m_queue.front()();
+ m_queue.pop();
+ m_mutex.unlock();
+}
+
+void HTTPClient::OnResponse(const http::response_type &r, std::function<void(http::response_type r)> cb) {
+ CleanupFutures();
+ try {
+ m_mutex.lock();
+ m_queue.push([this, r, cb] { cb(r); });
+ m_dispatcher.emit();
+ m_mutex.unlock();
+ } catch (const std::exception &e) {
+ fprintf(stderr, "error handling response (%s, code %d): %s\n", r.url.c_str(), r.status_code, e.what());
+ }
+}