From a9b57a946d9bea87001d5cc77158233cfe2a0565 Mon Sep 17 00:00:00 2001 From: AlphaKeks Date: Sat, 6 Jul 2024 02:10:51 +0200 Subject: [PATCH] Add more methods to HTTPManager --- src/httpmanager.cpp | 24 ++++++++++++++++++++++-- src/httpmanager.h | 5 ++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/httpmanager.cpp b/src/httpmanager.cpp index 75ed29d1..a80c2346 100644 --- a/src/httpmanager.cpp +++ b/src/httpmanager.cpp @@ -113,6 +113,21 @@ void HTTPManager::POST(const char* pszUrl, const char* pszText, CompletedCallbac GenerateRequest(k_EHTTPMethodPOST, pszUrl, pszText, callback, headers); } +void HTTPManager::PUT(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers) +{ + GenerateRequest(k_EHTTPMethodPUT, pszUrl, pszText, callback, headers); +} + +void HTTPManager::PATCH(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers) +{ + GenerateRequest(k_EHTTPMethodPATCH, pszUrl, pszText, callback, headers); +} + +void HTTPManager::DELETE(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers) +{ + GenerateRequest(k_EHTTPMethodDELETE, pszUrl, pszText, callback, headers); +} + void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers) { //Message("Sending HTTP:\n%s\n", pszText); @@ -120,7 +135,12 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const int size = strlen(pszText); //Message("HTTP request: %p\n", hReq); - if (method == k_EHTTPMethodPOST && !g_http->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size)) + bool shouldHaveBody = method == k_EHTTPMethodPOST + || method == k_EHTTPMethodPATCH + || method == k_EHTTPMethodPUT + || method == k_EHTTPMethodDELETE; + + if (shouldHaveBody && !g_http->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size)) { //Message("Failed to SetHTTPRequestRawPostBody\n"); return; @@ -139,4 +159,4 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const g_http->SendHTTPRequest(hReq, &hCall); new TrackedRequest(hReq, hCall, pszUrl, pszText, callback); -} \ No newline at end of file +} diff --git a/src/httpmanager.h b/src/httpmanager.h index c7a47919..896a7338 100644 --- a/src/httpmanager.h +++ b/src/httpmanager.h @@ -62,6 +62,9 @@ class HTTPManager public: void GET(const char* pszUrl, CompletedCallback callback, std::vector* headers = nullptr); void POST(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers = nullptr); + void PUT(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers = nullptr); + void PATCH(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers = nullptr); + void DELETE(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers = nullptr); bool HasAnyPendingRequests() const { return m_PendingRequests.size() > 0; } private: @@ -83,4 +86,4 @@ class HTTPManager private: std::vector m_PendingRequests; void GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector* headers); -}; \ No newline at end of file +};