summaryrefslogtreecommitdiff
path: root/discord/http.cpp
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2020-08-19 21:08:57 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2020-08-19 21:08:57 -0400
commit4b903bbd3e8436e1d63b8c12e76d8a3c924da5fc (patch)
treeaaf25a464d5a308322afd82805bbbcb31ab2f65c /discord/http.cpp
parent0cd0260f2e4cfe11678cdac4f965c9abf64b3592 (diff)
downloadabaddon-portaudio-4b903bbd3e8436e1d63b8c12e76d8a3c924da5fc.tar.gz
abaddon-portaudio-4b903bbd3e8436e1d63b8c12e76d8a3c924da5fc.zip
add http client and channel reordering (waste of time)
Diffstat (limited to 'discord/http.cpp')
-rw-r--r--discord/http.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/discord/http.cpp b/discord/http.cpp
new file mode 100644
index 0000000..429a3b2
--- /dev/null
+++ b/discord/http.cpp
@@ -0,0 +1,53 @@
+#include "http.hpp"
+
+HTTPClient::HTTPClient(std::string api_base)
+ : m_api_base(api_base) {}
+
+void HTTPClient::SetAuth(std::string auth) {
+ m_authorization = auth;
+}
+
+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" },
+ };
+ auto body = cpr::Body { payload };
+#ifdef USE_LOCAL_PROXY
+ m_futures.push_back(cpr::PatchCallback(
+ std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
+ url, headers, body,
+ 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),
+ url, headers, body));
+#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" },
+ };
+ auto body = cpr::Body { payload };
+}
+
+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::OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb) {
+ CleanupFutures();
+ cb(r);
+}