NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
transport.hpp
Go to the documentation of this file.
1// Copyright 2026 Aniket Kulkarni
2// SPDX-License-Identifier: Apache-2.0
3
6
7#pragma once
8
10#include "neuralplus/types.hpp"
11
12#include <chrono>
13#include <functional>
14#include <memory>
15#include <mutex>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace neuralplus {
22
24enum class HttpMethod {
25 get,
26 post,
27 put,
28 patch,
29 delete_,
30};
31
33struct HttpHeader {
35 std::string name;
36
38 std::string value;
39
41 bool sensitive{false};
42};
43
47 HttpMethod method{HttpMethod::post};
48
50 std::string url;
51
53 std::vector<HttpHeader> headers;
54
56 std::string body;
57
59 std::chrono::milliseconds timeout{60000};
60};
61
63struct NEURALPLUS_API HttpResponse {
65 int status{0};
66
68 std::vector<HttpHeader> headers;
69
71 std::string body;
72
74 [[nodiscard]] std::optional<std::string> header(std::string_view name) const;
75};
76
81class NEURALPLUS_API HttpTransport {
82public:
83 virtual ~HttpTransport();
84
86 virtual HttpResponse send(const HttpRequest& request) = 0;
87
88protected:
89 HttpTransport() = default;
90
91private:
92 HttpTransport(const HttpTransport&) = delete;
93 HttpTransport& operator=(const HttpTransport&) = delete;
94};
95
99 std::chrono::milliseconds connect_timeout{10000};
100
102 bool verify_tls{true};
103
105 std::string proxy;
106
108 std::string user_agent{"NeuralPlus"};
109
111 std::size_t max_response_body_bytes{16U * 1024U * 1024U};
112
114 std::size_t max_response_header_bytes{256U * 1024U};
115};
116
133class NEURALPLUS_API CurlHttpTransport final : public HttpTransport {
134public:
137 ~CurlHttpTransport() override;
138
140 HttpResponse send(const HttpRequest& request) override;
141
142private:
143 CurlHttpTransport(const CurlHttpTransport&) = delete;
144 CurlHttpTransport& operator=(const CurlHttpTransport&) = delete;
145
146 class Impl;
147 std::unique_ptr<Impl> impl_;
148};
149
151[[nodiscard]] NEURALPLUS_API std::shared_ptr<HttpTransport>
153
155class NEURALPLUS_API MockHttpTransport final : public HttpTransport {
156public:
160 using Handler = std::function<HttpResponse(const HttpRequest&)>;
161
163 explicit MockHttpTransport(Handler handler);
164 ~MockHttpTransport() override;
165
167 HttpResponse send(const HttpRequest& request) override;
168
170 [[nodiscard]] std::vector<HttpRequest> requests() const;
171
174
175private:
176 MockHttpTransport(const MockHttpTransport&) = delete;
177 MockHttpTransport& operator=(const MockHttpTransport&) = delete;
178
179 Handler handler_;
180 mutable std::mutex mutex_;
181 std::vector<HttpRequest> requests_;
182};
183
184} // namespace neuralplus
Definition transport.hpp:133
HttpResponse send(const HttpRequest &request) override
Sends one request synchronously through libcurl.
CurlHttpTransport(HttpTransportOptions options={})
Creates a libcurl transport with the supplied connection settings.
Definition transport.hpp:81
virtual HttpResponse send(const HttpRequest &request)=0
Sends one complete request and returns its HTTP response.
Deterministic callback transport that also records requests for tests.
Definition transport.hpp:155
std::vector< HttpRequest > requests() const
Returns a thread-safe snapshot of recorded requests.
HttpResponse send(const HttpRequest &request) override
Records the request and delegates response creation to the handler.
MockHttpTransport(Handler handler)
Creates a recording transport backed by handler.
std::function< HttpResponse(const HttpRequest &)> Handler
Definition transport.hpp:160
void clear_requests()
Removes all recorded requests.
One HTTP header.
Definition transport.hpp:33
bool sensitive
Treats this value as a credential for exact response/error redaction.
Definition transport.hpp:41
std::string name
Header field name.
Definition transport.hpp:35
std::string value
Header field value.
Definition transport.hpp:38
Provider-independent HTTP request.
Definition transport.hpp:45
std::string body
Serialized request body.
Definition transport.hpp:56
std::string url
Absolute request URL.
Definition transport.hpp:50
std::chrono::milliseconds timeout
Complete request timeout.
Definition transport.hpp:59
HttpMethod method
Request method.
Definition transport.hpp:47
std::vector< HttpHeader > headers
Request headers, including provider authentication.
Definition transport.hpp:53
Provider-independent HTTP response.
Definition transport.hpp:63
std::vector< HttpHeader > headers
Response headers.
Definition transport.hpp:68
std::optional< std::string > header(std::string_view name) const
Finds a header using case-insensitive ASCII comparison.
std::string body
Raw response body.
Definition transport.hpp:71
Settings for the built-in libcurl transport.
Definition transport.hpp:97
std::string user_agent
HTTP User-Agent sent by the built-in transport.
Definition transport.hpp:108
std::chrono::milliseconds connect_timeout
TCP/TLS connection timeout.
Definition transport.hpp:99
bool verify_tls
Enables peer and host certificate verification.
Definition transport.hpp:102
std::string proxy
Optional libcurl proxy URL; cleartext loopback URLs always bypass it.
Definition transport.hpp:105
std::size_t max_response_header_bytes
Maximum aggregate response-header bytes accepted from a provider.
Definition transport.hpp:114
std::size_t max_response_body_bytes
Maximum response-body bytes accepted from a provider.
Definition transport.hpp:111
NEURALPLUS_API std::shared_ptr< HttpTransport > make_default_http_transport(HttpTransportOptions options={})
Returns the built-in production HTTP transport.
HttpMethod
HTTP method used by provider adapters.
Definition transport.hpp:24