11 #include <curl/curl.h>
12 #include <curl/easy.h>
19 namespace pinecone::domain
33 [[nodiscard]]
auto url()
const noexcept ->
char const* {
return _url.
c_str(); }
43 template <operation_type op>
59 template <operation_type op>
61 std::string_view resource_name) noexcept ->
std::string
63 if constexpr (
op_api_type(op) == api_type::service) {
64 return url_builder.build<op>(resource_name);
65 }
else if constexpr (
op_api_type(op) == api_type::controller) {
67 oss << url_builder.build<op>() << resource_name;
76 template <operation_type op>
79 std::string_view resource_name) noexcept
80 :
arg_base(build_url<op>(url_builder, resource_name))
92 template <operation_type op,
typename Body>
96 :
arg_base(build_url<op>(url_builder, resource_name))
106 [[nodiscard]]
auto body() noexcept ->
char const* {
return _body.
c_str(); }
119 template <operation_type op,
typename Body>
132 [[nodiscard]]
auto body() noexcept ->
char const* {
return _body.
c_str(); }
145 template <operation_type op,
typename Body>
149 :
arg_base(build_url<op>(url_builder, resource_name))
159 [[nodiscard]]
auto body() noexcept ->
char const* {
return _body.
c_str(); }
171 template <operation_type,
typename Body = std::monostate>
174 static constexpr
auto kDelete =
"DELETE";
175 static constexpr
auto kPatch =
"PATCH";
180 template <operation_type Op,
typename Dep = std::monostate>
195 [[nodiscard]] constexpr
auto method() const noexcept {
return _method; }
206 curl_easy_reset(curl);
208 return set_method_opts(curl)
209 .and_then([
this, curl]() {
return curl_easy_setopt(curl, CURLOPT_URL, _args.url()); })
210 .and_then([
this, curl, headers]() {
211 return curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
219 return {curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, kDelete)};
221 if constexpr (op_method(
op_type) == method::patch) {
222 return util::curl_result(curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, kPatch))
224 [
this, curl]() {
return curl_easy_setopt(curl, CURLOPT_POSTFIELDS, _args.body()); });
227 return util::curl_result(curl_easy_setopt(curl, CURLOPT_POST, 1L)).and_then([
this, curl]() {
228 return curl_easy_setopt(curl, CURLOPT_POSTFIELDS, _args.body());
235 operation_args<Op, Dep> _args;
236 domain::method _method;
Wraps CURL results for type and exception safety.
auto build_url(net::url_builder const &url_builder, std::string_view resource_name) noexcept -> std::string
Constructs a URL for an operation that represents a unary resource.
Operations made available by the Pinecone API.
constexpr auto op_api_type(operation_type op) -> api_type
Returns the API type of an operation.
constexpr auto op_method(operation_type op) -> method
Returns the HTTP method for an operation.
All operations require a common subset of information; this information forms the base arguments.
Create operations construct themselves using a unique resource name and an operation-dependent body p...
auto body() noexcept -> char const *
Describe operations construct themselves using a unique resource name.
List operations are simple and construct themselves using only a URL.
Operation-specific arguments.
Data common to all Pinecone API operation types.
constexpr auto method() const noexcept
constexpr auto set_opts(CURL *curl, curl_slist *headers) noexcept -> util::curl_result
Set CURL options for the operation.
static constexpr auto op_type
The type of the operation.
Patch operations construct themselves using a unique resource name and an operation-dependent body pa...
auto body() noexcept -> char const *
Vector operations construct themselves using a unique resource name and an operation-dependent body p...
auto body() noexcept -> char const *
Constructs Pinecone API URLs.
auto build() const noexcept -> std::string
Constructs a URL for a specific operation_type.
Models the possibly of failure for remote HTTP operations.
constexpr auto and_then(std::function< curl_result()> const &func) const noexcept -> curl_result
Runs a transformation function on a successful result; does nothing on a failed result.
Constructs Pinecone API URLs.