Loading...
Searching...
No Matches
ml::HttpClient Class Reference

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).

Detailed Description

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.

Async (recommended)

ml::HttpClient client("https://api.example.com");
client.setBearerToken(myToken);
// GET /questions — callback runs on the main thread
client.get("/questions", [this](ml::NetworkResponse resp) {
if (!resp.ok()) return;
loadQuestions(resp.body);
});
// POST with JSON body
client.post("/answers", R"({"answer":"42"})",
[](ml::NetworkResponse resp) { ... });
Async and sync HTTP client backed by the background thread pool.
Definition HttpClient.h:64
The result of an HTTP or WebSocket message.
std::string body
Response body as a UTF-8 string.
bool ok() const
Return true when the request succeeded with a 2xx status.

Sync (use sparingly — blocks the calling thread)

auto resp = client.sendSync(
ml::NetworkRequest("https://api.example.com/ping").timeout(5));
Fluent builder for an outgoing HTTP request.

Per-request customisation

Build a NetworkRequest manually for full control:

auto req = ml::NetworkRequest("https://api.example.com/upload")
.method("PUT")
.header("X-Upload-ID", uploadId)
.body(data, "application/octet-stream")
.timeout(60);
client.send(req, [](ml::NetworkResponse resp) { ... });
NetworkRequest & method(const std::string &m)
Override the HTTP method.
NetworkRequest & timeout(int seconds)
Set the request timeout in seconds. Default is 30 s.
NetworkRequest & header(const std::string &key, const std::string &value)
Add or replace a request header.
NetworkRequest & body(const std::string &data, const std::string &contentType="application/json")
Set the request body.
See also
NetworkRequest, NetworkResponse, NetworkManager

Definition at line 63 of file HttpClient.h.

Member Typedef Documentation

◆ ResponseCallback

using ml::HttpClient::ResponseCallback = std::function<void(NetworkResponse)>

Definition at line 66 of file HttpClient.h.

Constructor & Destructor Documentation

◆ HttpClient()

ml::HttpClient::HttpClient ( const std::string & baseUrl = "")
explicit

Construct an HttpClient with an optional base URL.

Parameters
baseUrlScheme + host (+ optional port) prepended to every relative path. E.g. "https://api.example.com". Leave empty when passing fully-qualified URLs directly.

Member Function Documentation

◆ del()

void ml::HttpClient::del ( const std::string & path,
ResponseCallback cb )

Send a DELETE request asynchronously.

◆ get()

void ml::HttpClient::get ( const std::string & path,
ResponseCallback cb )

Send a GET request asynchronously.

Parameters
pathURL path (appended to baseUrl) or a fully-qualified URL.
cbCallback invoked on the main thread with the response.

◆ patch() [1/2]

void ml::HttpClient::patch ( const std::string & path,
const nlohmann::json & json,
ResponseCallback cb )
inline

Send a PATCH request with a nlohmann::json body.

Definition at line 169 of file HttpClient.h.

◆ patch() [2/2]

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.

◆ post() [1/2]

void ml::HttpClient::post ( const std::string & path,
const nlohmann::json & json,
ResponseCallback cb )
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.

◆ post() [2/2]

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.

Parameters
pathURL path or fully-qualified URL.
bodyRequest body string.
cbCallback invoked on the main thread.
contentTypeContent-Type header value. Default: "application/json".

◆ put() [1/2]

void ml::HttpClient::put ( const std::string & path,
const nlohmann::json & json,
ResponseCallback cb )
inline

Send a PUT request with a nlohmann::json body.

Definition at line 159 of file HttpClient.h.

◆ put() [2/2]

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.

◆ send()

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.

◆ sendSync()

NetworkResponse ml::HttpClient::sendSync ( const NetworkRequest & request)

Execute request synchronously on the calling thread.

Warning
Blocks until the response arrives or the timeout fires. Never call this from the main render loop — use the async overloads instead.
Parameters
requestThe request to execute.
Returns
The server's response.

◆ setBaseUrl()

void ml::HttpClient::setBaseUrl ( const std::string & url)

Set or replace the base URL (scheme + host + optional port).

◆ setBearerToken()

void ml::HttpClient::setBearerToken ( const std::string & token)

Set the Authorization: Bearer token applied to all requests.

Equivalent to calling setDefaultHeader("authorization", "Bearer <token>").

◆ setDefaultHeader()

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.

◆ setDefaultTimeout()

void ml::HttpClient::setDefaultTimeout ( int seconds)

Set the default timeout in seconds (default 30).


The documentation for this class was generated from the following file: