Loading...
Searching...
No Matches
HttpClient.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_HTTPCLIENT_H
5#define MALENA_HTTPCLIENT_H
6
7#pragma once
8
12#include <functional>
13#include <string>
14#include <unordered_map>
15#include <nlohmann/json.hpp>
16
17namespace ml
18{
64 {
65 public:
66 using ResponseCallback = std::function<void(NetworkResponse)>;
67
75 explicit HttpClient(const std::string& baseUrl = "");
76
77 // ── Configuration ────────────────────────────────────────────────────
78
80 void setBaseUrl(const std::string& url);
81
87 void setDefaultHeader(const std::string& key, const std::string& value);
88
90 void setDefaultTimeout(int seconds);
91
97 void setBearerToken(const std::string& token);
98
99 // ── Async ────────────────────────────────────────────────────────────
100
106 void get(const std::string& path, ResponseCallback cb);
107
115 void post(const std::string& path,
116 const std::string& body,
118 const std::string& contentType = "application/json");
119
123 void put(const std::string& path,
124 const std::string& body,
126 const std::string& contentType = "application/json");
127
131 void patch(const std::string& path,
132 const std::string& body,
134 const std::string& contentType = "application/json");
135
139 void del(const std::string& path, ResponseCallback cb);
140
141 // ── JSON overloads ───────────────────────────────────────────────────
142
149 void post(const std::string& path,
150 const nlohmann::json& json,
152 {
153 post(path, json.dump(), std::move(cb));
154 }
155
159 void put(const std::string& path,
160 const nlohmann::json& json,
162 {
163 put(path, json.dump(), std::move(cb));
164 }
165
169 void patch(const std::string& path,
170 const nlohmann::json& json,
172 {
173 patch(path, json.dump(), std::move(cb));
174 }
175
182 void send(const NetworkRequest& request, ResponseCallback cb);
183
184 // ── Sync ─────────────────────────────────────────────────────────────
185
197
198 private:
199 std::string _baseUrl;
200 int _defaultTimeout = 30;
201 std::unordered_map<std::string, std::string> _defaultHeaders;
202
204 static NetworkResponse execute(const NetworkRequest& request);
205
207 [[nodiscard]] std::string resolveUrl(const std::string& path) const;
208 };
209
210} // namespace ml
211
212#endif // MALENA_HTTPCLIENT_H
void del(const std::string &path, ResponseCallback cb)
Send a DELETE request asynchronously.
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.
HttpClient(const std::string &baseUrl="")
Construct an HttpClient with an optional base URL.
void setDefaultHeader(const std::string &key, const std::string &value)
Add a default header sent with every request.
void setBaseUrl(const std::string &url)
Set or replace the base URL (scheme + host + optional port).
NetworkResponse sendSync(const NetworkRequest &request)
Execute request synchronously on the calling thread.
std::function< void(NetworkResponse)> ResponseCallback
Definition HttpClient.h:66
void setBearerToken(const std::string &token)
Set the Authorization: Bearer token applied to all requests.
void send(const NetworkRequest &request, ResponseCallback cb)
Send an arbitrary NetworkRequest asynchronously.
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 put(const std::string &path, const nlohmann::json &json, ResponseCallback cb)
Send a PUT request with a nlohmann::json body.
Definition HttpClient.h:159
void patch(const std::string &path, const nlohmann::json &json, ResponseCallback cb)
Send a PATCH request with a nlohmann::json body.
Definition HttpClient.h:169
void get(const std::string &path, ResponseCallback cb)
Send a GET request asynchronously.
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 post(const std::string &path, const nlohmann::json &json, ResponseCallback cb)
Send a POST request with a nlohmann::json body.
Definition HttpClient.h:149
void setDefaultTimeout(int seconds)
Set the default timeout in seconds (default 30).
Fluent builder for an outgoing HTTP request.
nlohmann::json json
Alias for nlohmann::json.
Definition Json.h:32
#define MALENA_API
Definition Component.h:22
The result of an HTTP or WebSocket message.