Async and sync HTTP client backed by the background thread pool. More...
#include <Malena/Engine/Networking/HttpClient.h>
Public Types | |
| using | ResponseCallback = std::function<void(NetworkResponse)> |
Public Member Functions | |
| HttpClient (const std::string &baseUrl="") | |
Construct an HttpClient with an optional base URL. | |
| void | del (const std::string &path, ResponseCallback cb) |
Send a DELETE request asynchronously. | |
| void | get (const std::string &path, ResponseCallback cb) |
Send a GET request asynchronously. | |
| void | patch (const std::string &path, const nlohmann::json &json, ResponseCallback cb) |
Send a PATCH request with a nlohmann::json body. | |
| void | patch (const std::string &path, const std::string &body, ResponseCallback cb, const std::string &contentType="application/json") |
Send a PATCH request with a body asynchronously. | |
| void | post (const std::string &path, const nlohmann::json &json, ResponseCallback cb) |
Send a POST request with a nlohmann::json body. | |
| void | post (const std::string &path, const std::string &body, ResponseCallback cb, const std::string &contentType="application/json") |
Send a POST request with a body asynchronously. | |
| void | put (const std::string &path, const nlohmann::json &json, ResponseCallback cb) |
Send a PUT request with a nlohmann::json body. | |
| void | put (const std::string &path, const std::string &body, ResponseCallback cb, const std::string &contentType="application/json") |
Send a PUT request with a body asynchronously. | |
| void | send (const NetworkRequest &request, ResponseCallback cb) |
Send an arbitrary NetworkRequest asynchronously. | |
| NetworkResponse | sendSync (const NetworkRequest &request) |
Execute request synchronously on the calling thread. | |
| void | setBaseUrl (const std::string &url) |
| Set or replace the base URL (scheme + host + optional port). | |
| void | setBearerToken (const std::string &token) |
Set the Authorization: Bearer token applied to all requests. | |
| void | setDefaultHeader (const std::string &key, const std::string &value) |
| Add a default header sent with every request. | |
| void | setDefaultTimeout (int seconds) |
| Set the default timeout in seconds (default 30). | |
Async and sync HTTP client backed by the background thread pool.
All async methods submit the request to NetworkManager's thread pool and deliver the NetworkResponse callback on the main thread at the next frame flush — keeping the render loop unblocked.
Build a NetworkRequest manually for full control:
Definition at line 63 of file HttpClient.h.
| using ml::HttpClient::ResponseCallback = std::function<void(NetworkResponse)> |
Definition at line 66 of file HttpClient.h.
|
explicit |
Construct an HttpClient with an optional base URL.
| baseUrl | Scheme + host (+ optional port) prepended to every relative path. E.g. "https://api.example.com". Leave empty when passing fully-qualified URLs directly. |
| void ml::HttpClient::del | ( | const std::string & | path, |
| ResponseCallback | cb ) |
Send a DELETE request asynchronously.
| void ml::HttpClient::get | ( | const std::string & | path, |
| ResponseCallback | cb ) |
Send a GET request asynchronously.
| path | URL path (appended to baseUrl) or a fully-qualified URL. |
| cb | Callback invoked on the main thread with the response. |
|
inline |
Send a PATCH request with a nlohmann::json body.
Definition at line 169 of file HttpClient.h.
| void ml::HttpClient::patch | ( | const std::string & | path, |
| const std::string & | body, | ||
| ResponseCallback | cb, | ||
| const std::string & | contentType = "application/json" ) |
Send a PATCH request with a body asynchronously.
|
inline |
Send a POST request with a nlohmann::json body.
Serialises json to a string and sets Content-Type to application/json automatically.
Definition at line 149 of file HttpClient.h.
| void ml::HttpClient::post | ( | const std::string & | path, |
| const std::string & | body, | ||
| ResponseCallback | cb, | ||
| const std::string & | contentType = "application/json" ) |
Send a POST request with a body asynchronously.
| path | URL path or fully-qualified URL. |
| body | Request body string. |
| cb | Callback invoked on the main thread. |
| contentType | Content-Type header value. Default: "application/json". |
|
inline |
Send a PUT request with a nlohmann::json body.
Definition at line 159 of file HttpClient.h.
| void ml::HttpClient::put | ( | const std::string & | path, |
| const std::string & | body, | ||
| ResponseCallback | cb, | ||
| const std::string & | contentType = "application/json" ) |
Send a PUT request with a body asynchronously.
| void ml::HttpClient::send | ( | const NetworkRequest & | request, |
| ResponseCallback | cb ) |
Send an arbitrary NetworkRequest asynchronously.
Default headers and the bearer token are merged in unless the request already sets those keys.
| NetworkResponse ml::HttpClient::sendSync | ( | const NetworkRequest & | request | ) |
Execute request synchronously on the calling thread.
| request | The request to execute. |
| void ml::HttpClient::setBaseUrl | ( | const std::string & | url | ) |
Set or replace the base URL (scheme + host + optional port).
| void ml::HttpClient::setBearerToken | ( | const std::string & | token | ) |
Set the Authorization: Bearer token applied to all requests.
Equivalent to calling setDefaultHeader("authorization", "Bearer <token>").
| void ml::HttpClient::setDefaultHeader | ( | const std::string & | key, |
| const std::string & | value ) |
Add a default header sent with every request.
Overridden per-request if the same key is set on a NetworkRequest.
| void ml::HttpClient::setDefaultTimeout | ( | int | seconds | ) |
Set the default timeout in seconds (default 30).