Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more methods to HTTPManager #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/httpmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,34 @@ 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<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodPUT, pszUrl, pszText, callback, headers);
}

void HTTPManager::PATCH(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodPATCH, pszUrl, pszText, callback, headers);
}

void HTTPManager::DELETE(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodDELETE, pszUrl, pszText, callback, headers);
}

void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers)
{
//Message("Sending HTTP:\n%s\n", pszText);
auto hReq = g_http->CreateHTTPRequest(method, pszUrl);
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;
Expand All @@ -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);
}
}
5 changes: 4 additions & 1 deletion src/httpmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class HTTPManager
public:
void GET(const char* pszUrl, CompletedCallback callback, std::vector<HTTPHeader>* headers = nullptr);
void POST(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers = nullptr);
void PUT(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers = nullptr);
void PATCH(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers = nullptr);
void DELETE(const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers = nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're gonna need to pick a different name for this function. Apparently DELETE is already defined as a macro in Windows build environments, leading to Windows builds failing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If DELETE is renamed, the other should probably be renamed as well for consistency's sake? We could also just have a single method that takes the HTTP method as a parameter (either by creating our own enum or using valve's).

Copy link
Contributor

@Vauff Vauff Jul 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever works, GET/POST were only split up because of the extra param. Do be mindful of making compatibility breaks with those existing functions though, they are heavily used in server-specific forks.

bool HasAnyPendingRequests() const { return m_PendingRequests.size() > 0; }

private:
Expand All @@ -83,4 +86,4 @@ class HTTPManager
private:
std::vector<HTTPManager::TrackedRequest*> m_PendingRequests;
void GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText, CompletedCallback callback, std::vector<HTTPHeader>* headers);
};
};
Loading