18 #include <curl/curl.h>
19 #include <curl/easy.h>
20 #include <nlohmann/json.hpp>
28 using json = nlohmann::json;
30 namespace pinecone::net
32 static constexpr
size_t kInitialDataSize = 1024;
33 static constexpr int64_t kHttpOk = 200;
34 static constexpr int64_t kHttpCreated = 201;
35 static constexpr int64_t kHttpAccepted = 202;
41 template <threading_mode Mode>
44 static constexpr
auto kContentType =
"Content-Type: application/json; charset=utf-8";
60 auto* curl_handle = curl_easy_init();
61 curl_slist* headers{};
62 headers = curl_slist_append(headers, kContentType);
63 headers = curl_slist_append(headers,
args.api_key_header());
64 if (headers ==
nullptr) {
74 curl_easy_cleanup(_curl_handle);
75 curl_slist_free_all(_headers);
78 http_client(http_client
const&) =
delete;
79 auto operator=(http_client
const&) -> http_client& =
delete;
80 http_client(http_client&&) noexcept = default;
81 auto operator=(http_client&&) noexcept -> http_client& = default;
91 template <domain::operation_type Op, typename Body =
std::monostate>
92 auto request(domain::operation_args<Op, Body> op_args) const noexcept
93 -> util::result<typename domain::operation_args<Op, Body>::parsed_type>
98 operation.
set_opts(_curl_handle, _headers)
100 return curl_easy_setopt(_curl_handle, CURLOPT_WRITEFUNCTION, http_client::read);
102 .and_then([
this]() {
return curl_easy_setopt(_curl_handle, CURLOPT_WRITEDATA,
this); })
103 .and_then([
this]() {
return curl_easy_perform(_curl_handle); });
104 if (result.is_error()) {
105 return {result.error()};
108 int64_t http_code = 0;
109 curl_easy_getinfo(_curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
115 return op_args.parse(_data);
116 }
catch (json::exception& ex) {
120 return {http_code,
std::string(_data.begin(), _data.end())};
125 auto receive_chunk(
void* buffer,
size_t n) noexcept ->
size_t
127 auto* bytes =
static_cast<uint8_t*
>(buffer);
128 for (
size_t i = 0; i < n; ++i) {
129 _data.emplace_back(*(bytes + i));
136 static auto read(
void* buffer,
size_t sz,
size_t n,
void* f) noexcept ->
size_t
138 return static_cast<http_client*
>(f)->receive_chunk(buffer, n * sz);
142 connection_args _args;
144 curl_slist* _headers;
147 http_client(connection_args args, CURL* curl_handle, curl_slist* headers) noexcept
148 : _args(
std::move(args)), _curl_handle(curl_handle), _headers(headers)
150 assert(_curl_handle !=
nullptr);
151 _data.
reserve(kInitialDataSize);
User-facing arguments for networking operations.
threading_mode
Threading behaviors supported by http_client.
@ sync
Synchronous operation; in this mode, http_client instances are not thread-safe.
Metaprogamming infrastructure for generation of per-operation code.
Operations made available by the Pinecone API.
Models the possibility of failure for Pinecone operations.
Operation-specific arguments.
Data common to all Pinecone API operation types.
constexpr auto set_opts(CURL *curl, curl_slist *headers) noexcept -> util::curl_result
Set CURL options for the operation.
Arguments required to connect to a Pinecone server.
static auto build(connection_args args) noexcept -> std::unique_ptr< http_client >
Attempts to construct a synchronous HTTP client via user-specific connection_args.
An HTTP client responsible for making requests to a Pinecone instance.