new util methods, rename postFields and customRequest

This commit is contained in:
matcool 2024-01-09 20:10:58 -03:00
parent 91703e4192
commit 8ccc61febb
2 changed files with 84 additions and 23 deletions

View file

@ -136,7 +136,7 @@ namespace geode::utils::web {
* automatically followed * automatically followed
* @returns An AsyncWebResponse object * @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 * 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 * destructor calls it automatically, but if you need access to the
@ -145,6 +145,27 @@ namespace geode::utils::web {
*/ */
SentAsyncWebRequestHandle send(); 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, * 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 * 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 * recommended to be something unique
* @returns Same AsyncWebRequest * @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. * In order to specify a http header to the request, give it here.
* Can be called more than once. * 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. * 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. * Specify that the request is a POST request.
*/ */
AsyncWebRequest& postRequest(); 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 * Specify the raw request body to send with the request.
* `postRequest` or `customRequest` was called before. * 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 * Specify a json request body. Additionally sets the content type to
* `postRequest` or `customRequest` was called before. Additionally * application/json.
* 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. * Specify a timeout, in seconds, in which the request will fail.
*/ */
AsyncWebRequest& timeout(std::chrono::seconds seconds); AsyncWebRequest& timeout(std::chrono::seconds seconds);
// Callbacks
/** /**
* Specify a callback to run if the download fails. The callback is * Specify a callback to run if the download fails. The callback is
* always ran in the GD thread, so interacting with UI is safe * always ran in the GD thread, so interacting with UI is safe

View file

@ -501,35 +501,38 @@ AsyncWebRequest& AsyncWebRequest::setThen(AsyncThen then) {
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::join(std::string const& requestID) { AsyncWebRequest& AsyncWebRequest::join(std::string_view const requestID) {
m_impl->m_joinID = requestID; m_impl->m_joinID = requestID;
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::userAgent(std::string const& userAgent) { AsyncWebRequest& AsyncWebRequest::userAgent(std::string_view const userAgent) {
m_impl->m_userAgent = userAgent; m_impl->m_userAgent = userAgent;
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::contentType(std::string_view const contentType) {
return this->header("Content-Type", contentType);
}
AsyncWebRequest& AsyncWebRequest::postRequest() { AsyncWebRequest& AsyncWebRequest::postRequest() {
m_impl->m_isPostRequest = true; m_impl->m_isPostRequest = true;
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::customRequest(std::string const& request) { AsyncWebRequest& AsyncWebRequest::method(std::string_view const request) {
m_impl->m_customRequest = request; m_impl->m_customRequest = request;
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::postFields(std::string const& fields) { AsyncWebRequest& AsyncWebRequest::bodyRaw(std::string_view const fields) {
m_impl->m_isPostRequest = true;
m_impl->m_postFields = fields; m_impl->m_postFields = fields;
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::postFields(matjson::Value const& fields) { AsyncWebRequest& AsyncWebRequest::body(matjson::Value const& fields) {
m_impl->m_isJsonRequest = true; m_impl->m_isJsonRequest = true;
return this->postFields(fields.dump()); return this->bodyRaw(fields.dump());
} }
AsyncWebRequest& AsyncWebRequest::timeout(std::chrono::seconds seconds) { AsyncWebRequest& AsyncWebRequest::timeout(std::chrono::seconds seconds) {
@ -537,12 +540,36 @@ AsyncWebRequest& AsyncWebRequest::timeout(std::chrono::seconds seconds) {
return *this; return *this;
} }
AsyncWebRequest& AsyncWebRequest::header(std::string const& header) { AsyncWebRequest& AsyncWebRequest::header(std::string_view const header) {
m_impl->m_httpHeaders.push_back(header); m_impl->m_httpHeaders.push_back(std::string(header));
return *this; 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; m_impl->m_url = url;
return AsyncWebResponse(*this); return AsyncWebResponse(*this);
} }