mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-26 17:36:05 -05:00
new util methods, rename postFields and customRequest
This commit is contained in:
parent
91703e4192
commit
8ccc61febb
2 changed files with 84 additions and 23 deletions
|
@ -136,7 +136,7 @@ namespace geode::utils::web {
|
|||
* automatically followed
|
||||
* @returns An AsyncWebResponse object
|
||||
*/
|
||||
AsyncWebResponse fetch(std::string const& url);
|
||||
AsyncWebResponse fetch(std::string_view const url);
|
||||
/**
|
||||
* Begin the web request. It's not always necessary to call this as the
|
||||
* destructor calls it automatically, but if you need access to the
|
||||
|
@ -145,6 +145,27 @@ namespace geode::utils::web {
|
|||
*/
|
||||
SentAsyncWebRequestHandle send();
|
||||
|
||||
/**
|
||||
* Shorthand for a GET request to the given url.
|
||||
* Same return value as fetch.
|
||||
*/
|
||||
AsyncWebResponse get(std::string_view const url);
|
||||
/**
|
||||
* Shorthand for a POST request to the given url.
|
||||
* Same return value as fetch.
|
||||
*/
|
||||
AsyncWebResponse post(std::string_view const url);
|
||||
/**
|
||||
* Shorthand for a PUT request to the given url.
|
||||
* Same return value as fetch.
|
||||
*/
|
||||
AsyncWebResponse put(std::string_view const url);
|
||||
/**
|
||||
* Shorthand for a PATCH request to the given url.
|
||||
* Same return value as fetch.
|
||||
*/
|
||||
AsyncWebResponse patch(std::string_view const url);
|
||||
|
||||
/**
|
||||
* If you only want one instance of this web request to run (for example,
|
||||
* you're downloading some global data for a manager), then use this
|
||||
|
@ -155,40 +176,53 @@ namespace geode::utils::web {
|
|||
* recommended to be something unique
|
||||
* @returns Same AsyncWebRequest
|
||||
*/
|
||||
AsyncWebRequest& join(std::string const& requestID);
|
||||
AsyncWebRequest& join(std::string_view const requestID);
|
||||
|
||||
// Request parameters
|
||||
|
||||
/**
|
||||
* In order to specify a http header to the request, give it here.
|
||||
* Can be called more than once.
|
||||
*/
|
||||
AsyncWebRequest& header(std::string const& header);
|
||||
AsyncWebRequest& header(std::string_view const header);
|
||||
/**
|
||||
* In order to specify a http header to the request, give it here.
|
||||
* Can be called more than once.
|
||||
*/
|
||||
AsyncWebRequest& header(std::string_view const headerName, std::string_view const headerValue);
|
||||
/**
|
||||
* In order to specify an user agent to the request, give it here.
|
||||
*/
|
||||
AsyncWebRequest& userAgent(std::string const& userAgent);
|
||||
AsyncWebRequest& userAgent(std::string_view const userAgent);
|
||||
/**
|
||||
* Sets the Content-Type header to the specified value.
|
||||
*/
|
||||
AsyncWebRequest& contentType(std::string_view const contentType);
|
||||
/**
|
||||
* Specify that the request is a POST request.
|
||||
*/
|
||||
AsyncWebRequest& postRequest();
|
||||
/**
|
||||
* Specify that the request is a custom request like PUT and DELETE.
|
||||
* Specify the HTTP method for the request, like PUT and DELETE.
|
||||
*/
|
||||
AsyncWebRequest& customRequest(std::string const& request);
|
||||
AsyncWebRequest& method(std::string_view const method);
|
||||
/**
|
||||
* Specify the post fields to send with the request. Only valid if
|
||||
* `postRequest` or `customRequest` was called before.
|
||||
* Specify the raw request body to send with the request.
|
||||
* Content type is unchanged.
|
||||
*/
|
||||
AsyncWebRequest& postFields(std::string const& fields);
|
||||
AsyncWebRequest& bodyRaw(std::string_view const content);
|
||||
/**
|
||||
* Specify the post fields to send with the request. Only valid if
|
||||
* `postRequest` or `customRequest` was called before. Additionally
|
||||
* sets the content type to application/json.
|
||||
* Specify a json request body. Additionally sets the content type to
|
||||
* application/json.
|
||||
*/
|
||||
AsyncWebRequest& postFields(matjson::Value const& fields);
|
||||
AsyncWebRequest& body(matjson::Value const& value);
|
||||
/**
|
||||
* Specify a timeout, in seconds, in which the request will fail.
|
||||
*/
|
||||
AsyncWebRequest& timeout(std::chrono::seconds seconds);
|
||||
|
||||
// Callbacks
|
||||
|
||||
/**
|
||||
* Specify a callback to run if the download fails. The callback is
|
||||
* always ran in the GD thread, so interacting with UI is safe
|
||||
|
|
|
@ -501,35 +501,38 @@ AsyncWebRequest& AsyncWebRequest::setThen(AsyncThen then) {
|
|||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::join(std::string const& requestID) {
|
||||
AsyncWebRequest& AsyncWebRequest::join(std::string_view const requestID) {
|
||||
m_impl->m_joinID = requestID;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::userAgent(std::string const& userAgent) {
|
||||
AsyncWebRequest& AsyncWebRequest::userAgent(std::string_view const userAgent) {
|
||||
m_impl->m_userAgent = userAgent;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::contentType(std::string_view const contentType) {
|
||||
return this->header("Content-Type", contentType);
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::postRequest() {
|
||||
m_impl->m_isPostRequest = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::customRequest(std::string const& request) {
|
||||
AsyncWebRequest& AsyncWebRequest::method(std::string_view const request) {
|
||||
m_impl->m_customRequest = request;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::postFields(std::string const& fields) {
|
||||
m_impl->m_isPostRequest = true;
|
||||
AsyncWebRequest& AsyncWebRequest::bodyRaw(std::string_view const fields) {
|
||||
m_impl->m_postFields = fields;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::postFields(matjson::Value const& fields) {
|
||||
AsyncWebRequest& AsyncWebRequest::body(matjson::Value const& fields) {
|
||||
m_impl->m_isJsonRequest = true;
|
||||
return this->postFields(fields.dump());
|
||||
return this->bodyRaw(fields.dump());
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::timeout(std::chrono::seconds seconds) {
|
||||
|
@ -537,12 +540,36 @@ AsyncWebRequest& AsyncWebRequest::timeout(std::chrono::seconds seconds) {
|
|||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebRequest& AsyncWebRequest::header(std::string const& header) {
|
||||
m_impl->m_httpHeaders.push_back(header);
|
||||
AsyncWebRequest& AsyncWebRequest::header(std::string_view const header) {
|
||||
m_impl->m_httpHeaders.push_back(std::string(header));
|
||||
return *this;
|
||||
}
|
||||
|
||||
AsyncWebResponse AsyncWebRequest::fetch(std::string const& url) {
|
||||
AsyncWebRequest& AsyncWebRequest::header(std::string_view const headerName, std::string_view const headerValue) {
|
||||
return this->header(fmt::format("{}: {}", headerName, headerValue));
|
||||
}
|
||||
|
||||
AsyncWebResponse AsyncWebRequest::get(std::string_view const url) {
|
||||
this->method("GET");
|
||||
return this->fetch(url);
|
||||
}
|
||||
|
||||
AsyncWebResponse AsyncWebRequest::post(std::string_view const url) {
|
||||
this->method("POST");
|
||||
return this->fetch(url);
|
||||
}
|
||||
|
||||
AsyncWebResponse AsyncWebRequest::put(std::string_view const url) {
|
||||
this->method("PUT");
|
||||
return this->fetch(url);
|
||||
}
|
||||
|
||||
AsyncWebResponse AsyncWebRequest::patch(std::string_view const url) {
|
||||
this->method("PATCH");
|
||||
return this->fetch(url);
|
||||
}
|
||||
|
||||
AsyncWebResponse AsyncWebRequest::fetch(std::string_view const url) {
|
||||
m_impl->m_url = url;
|
||||
return AsyncWebResponse(*this);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue