Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
http_client_interface.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
14#pragma once
15
16#include <chrono>
17#include <cstdint>
18#include <map>
19#include <optional>
20#include <string>
21#include <vector>
22
24
25namespace kcenon::common {
26namespace interfaces {
27
31using http_headers = std::map<std::string, std::string>;
32
39 std::string url;
40
42 std::string method = "GET";
43
46
48 std::vector<uint8_t> body;
49
51 std::chrono::milliseconds timeout{30000};
52
54 bool follow_redirects = true;
55
58
59 // Convenience constructors
60
61 http_request() = default;
62
63 explicit http_request(std::string url_)
64 : url(std::move(url_)) {}
65
66 http_request(std::string url_, std::string method_)
67 : url(std::move(url_)), method(std::move(method_)) {}
68
69 http_request(std::string url_, std::string method_, http_headers headers_)
70 : url(std::move(url_))
71 , method(std::move(method_))
72 , headers(std::move(headers_)) {}
73
74 http_request(std::string url_,
75 std::string method_,
76 http_headers headers_,
77 std::vector<uint8_t> body_)
78 : url(std::move(url_))
79 , method(std::move(method_))
80 , headers(std::move(headers_))
81 , body(std::move(body_)) {}
82
88 http_request& set_content_type(const std::string& content_type) {
89 headers["Content-Type"] = content_type;
90 return *this;
91 }
92
98 http_request& set_authorization(const std::string& auth) {
99 headers["Authorization"] = auth;
100 return *this;
101 }
102
108 http_request& set_body(const std::string& str) {
109 body.assign(str.begin(), str.end());
110 return *this;
111 }
112};
113
120 int status_code = 0;
121
123 std::string reason_phrase;
124
127
129 std::vector<uint8_t> body;
130
132 std::chrono::milliseconds elapsed{0};
133
135 std::optional<std::string> final_url;
136
137 // Convenience methods
138
143 [[nodiscard]] bool is_success() const {
144 return status_code >= 200 && status_code < 300;
145 }
146
151 [[nodiscard]] bool is_client_error() const {
152 return status_code >= 400 && status_code < 500;
153 }
154
159 [[nodiscard]] bool is_server_error() const {
160 return status_code >= 500 && status_code < 600;
161 }
162
167 [[nodiscard]] std::string body_as_string() const {
168 return std::string(body.begin(), body.end());
169 }
170
176 [[nodiscard]] std::optional<std::string> get_header(
177 const std::string& name) const {
178 auto it = headers.find(name);
179 if (it != headers.end()) {
180 return it->second;
181 }
182 return std::nullopt;
183 }
184};
185
212public:
213 virtual ~IHttpClient() = default;
214
220 virtual ::kcenon::common::Result<http_response> send(const http_request& request) = 0;
221
226 [[nodiscard]] virtual bool is_available() const = 0;
227
232 [[nodiscard]] virtual std::string get_implementation_name() const {
233 return "IHttpClient";
234 }
235};
236
245public:
247 return ::kcenon::common::make_error<http_response>(
249 "HTTP client not available",
250 "null_http_client");
251 }
252
253 [[nodiscard]] bool is_available() const override {
254 return false;
255 }
256
257 [[nodiscard]] std::string get_implementation_name() const override {
258 return "null_http_client";
259 }
260};
261
265using HttpClientFactory = std::function<std::shared_ptr<IHttpClient>()>;
266
272public:
273 virtual ~IHttpClientProvider() = default;
274
279 virtual std::shared_ptr<IHttpClient> get_http_client() = 0;
280
286 virtual std::shared_ptr<IHttpClient> create_http_client(
287 std::chrono::milliseconds timeout = std::chrono::milliseconds{30000}) = 0;
288};
289
290} // namespace interfaces
291} // namespace kcenon::common
Result type for error handling with member function support.
Definition core.cppm:165
Interface for modules that provide HTTP client implementations.
virtual std::shared_ptr< IHttpClient > get_http_client()=0
Get the default HTTP client instance.
virtual std::shared_ptr< IHttpClient > create_http_client(std::chrono::milliseconds timeout=std::chrono::milliseconds{30000})=0
Create a new HTTP client with specific configuration.
Abstract interface for HTTP client implementations.
virtual bool is_available() const =0
Check if the HTTP client is available and properly configured.
virtual ::kcenon::common::Result< http_response > send(const http_request &request)=0
Send an HTTP request synchronously.
virtual std::string get_implementation_name() const
Get the implementation name for logging/debugging.
Null implementation for when HTTP transport is disabled.
bool is_available() const override
Check if the HTTP client is available and properly configured.
std::string get_implementation_name() const override
Get the implementation name for logging/debugging.
::kcenon::common::Result< http_response > send(const http_request &) override
Send an HTTP request synchronously.
constexpr int NOT_INITIALIZED
Definition compat.h:36
std::function< std::shared_ptr< IHttpClient >()> HttpClientFactory
Factory function type for creating HTTP client instances.
std::map< std::string, std::string > http_headers
HTTP headers container type.
Core interfaces.
Definition adapter.h:21
Umbrella header for Result<T> type and related utilities.
http_request & set_authorization(const std::string &auth)
Set Authorization header.
bool follow_redirects
Whether to follow redirects (default: true)
http_request & set_body(const std::string &str)
Set body from string.
http_request(std::string url_, std::string method_, http_headers headers_, std::vector< uint8_t > body_)
std::vector< uint8_t > body
Request body (for POST, PUT, PATCH)
http_request & set_content_type(const std::string &content_type)
Set Content-Type header.
int max_redirects
Maximum number of redirects to follow (default: 5)
std::chrono::milliseconds timeout
Request timeout (default: 30 seconds)
std::string url
Request URL (must include scheme, e.g., "https://api.example.com/v1/data")
http_request(std::string url_, std::string method_, http_headers headers_)
http_request(std::string url_, std::string method_)
std::string method
HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
bool is_server_error() const
Check if response indicates server error (5xx status)
bool is_success() const
Check if response indicates success (2xx status)
std::string body_as_string() const
Get body as string.
std::optional< std::string > final_url
Final URL after redirects (may differ from request URL)
std::chrono::milliseconds elapsed
Time taken to receive the response.
std::vector< uint8_t > body
Response body.
bool is_client_error() const
Check if response indicates client error (4xx status)
int status_code
HTTP status code (e.g., 200, 404, 500)
std::optional< std::string > get_header(const std::string &name) const
Get a specific header value.
std::string reason_phrase
Status reason phrase (e.g., "OK", "Not Found")