|
Arachne 1.0
Arachne - the perpetual stitcher of Wikidata entities.
|
Minimal, synchronous HTTP GET client built on libcurl. More...
#include <include/http_client.hpp>

Public Member Functions | |
| http_client () | |
| Construct a client and initialize libcurl. | |
| http_response | get (std::string_view url, const parameter_list ¶ms={}, std::string_view accept={}, int timeout_sec=-1) |
Perform an HTTP GET to url with optional query params. | |
| http_response | post_form (std::string_view url, const parameter_list &form, const parameter_list &query={}, std::string_view accept={}, int timeout_sec=-1) |
| Perform an HTTP POST with form-encoded body. | |
| http_response | post_raw (std::string_view url, std::string_view body, std::string_view content_type, const parameter_list &query={}, std::string_view accept={}, int timeout_sec=-1) |
| Perform an HTTP POST with a raw body. | |
| const network_metrics & | metrics_info () const |
| Access aggregated network metrics. | |
Private Types | |
| using | curl_url_ptr = std::unique_ptr<CURLU, decltype(&curl_url_cleanup)> |
| Unique pointer type for CURLU with proper deleter. | |
Private Member Functions | |
| http_response | request_get (CURLU *url_handle, std::chrono::milliseconds &elapsed, std::string_view accept={}, int timeout_sec=-1) const |
| Execute a single HTTP GET using the prepared URL handle. | |
| http_response | request_post (CURLU *url_handle, std::chrono::milliseconds &elapsed, std::string_view content_type, std::string_view body, std::string_view accept={}, int timeout_sec=-1) const |
| Execute a single HTTP POST with given body and content type. | |
| std::string | build_form_body (const parameter_list &form) const |
| void | update_headers (http_response &response) const |
| Refresh the header multimap from the last transfer. | |
| void | update_metrics (const http_response &response, std::chrono::milliseconds elapsed) |
| Update counters and histograms after an attempt. | |
| long long | next_delay (int attempt) const |
Compute the next backoff delay for attempt (1-based). | |
| void | apply_server_retry_hint (long long &sleep_ms) const |
| Apply server-provided retry hint if present. | |
Static Private Member Functions | |
| static curl_url_ptr | build_url (std::string_view url, const parameter_list ¶ms) |
Construct a CURLU handle from url and append params. | |
| static bool | status_good (const http_response &response) |
| Success predicate: transport OK and HTTP 2xx. | |
| static bool | status_retry (const http_response &response, bool net_ok) |
| Retry predicate for transient outcomes. | |
| static size_t | write_callback (const char *ptr, size_t size, size_t n, void *data) |
| libcurl write callback: append chunk to response body. | |
Private Attributes | |
| const network_options | opt {} |
| Fixed options installed at construction. | |
| network_metrics | metrics |
| Aggregated metrics (atomic counters). | |
| std::mutex | mu |
| std::unique_ptr< CURL, decltype(&curl_easy_cleanup)> | curl |
| Reused easy handle (not thread-safe). | |
| std::unique_ptr< curl_slist, decltype(&curl_slist_free_all)> | header_list |
| Owned request header list. | |
Minimal, synchronous HTTP GET client built on libcurl.
Responsibilities:
Lifetime and thread-safety:
Definition at line 50 of file http_client.hpp.
|
private |
Unique pointer type for CURLU with proper deleter.
Definition at line 149 of file http_client.hpp.
|
explicit |
Construct a client and initialize libcurl.
Effects:
| std::runtime_error | if libcurl initialization fails or header allocation fails. |
Definition at line 48 of file http_client.cpp.
References corespace::anonymous_namespace{http_client.cpp}::curl_inited(), and corespace::anonymous_namespace{http_client.cpp}::global_curl.

|
private |
Apply server-provided retry hint if present.
If CURLINFO_RETRY_AFTER yields a non-negative value, interpret it as seconds and raise sleep_ms to at least that many milliseconds.
| sleep_ms | In/out: proposed client backoff in milliseconds. |
Definition at line 376 of file http_client.cpp.
Referenced by get(), post_form(), and post_raw().

|
private |
Definition at line 308 of file http_client.cpp.
|
staticprivate |
Construct a CURLU handle from url and append params.
Each parameter is URL-encoded and appended via CURLU_APPENDQUERY.
| url | Base URL. |
| params | Query parameters. |
| std::runtime_error | if allocation or URL assembly fails. |
Definition at line 181 of file http_client.cpp.
| http_response corespace::http_client::get | ( | std::string_view | url, |
| const parameter_list & | params = {}, | ||
| std::string_view | accept = {}, | ||
| int | timeout_sec = -1 ) |
Perform an HTTP GET to url with optional query params.
Behavior:
Failure:
Metrics:
| url | Absolute or base URL. |
| params | Optional list of query parameters to append. |
| accept | Optional Accept header value to override the default; empty string uses the client's configured accept header. |
| timeout_sec | Optional per-request timeout in seconds; if negative the client's default timeout is used. |
| std::runtime_error | on terminal failure as described above. |
Definition at line 74 of file http_client.cpp.
References apply_server_retry_hint(), corespace::http_response::error_code, next_delay(), status_good(), and update_metrics().

|
nodiscard |
Access aggregated network metrics.
Definition at line 72 of file http_client.cpp.
|
nodiscardprivate |
Compute the next backoff delay for attempt (1-based).
Strategy: exponential backoff with full jitter. The base grows as retry_base_ms * 2^(attempt-1) and a uniform random component in [0, base] is added; the result is capped at retry_max_ms.
| attempt | Attempt number starting from 1. |
Definition at line 370 of file http_client.cpp.
Referenced by get(), post_form(), and post_raw().

| http_response corespace::http_client::post_form | ( | std::string_view | url, |
| const parameter_list & | form, | ||
| const parameter_list & | query = {}, | ||
| std::string_view | accept = {}, | ||
| int | timeout_sec = -1 ) |
Perform an HTTP POST with form-encoded body.
Builds a URL from url and query, serializes form as application/x-www-form-urlencoded and posts it. Retry behaviour and metrics follow the same semantics as get().
| url | Endpoint URL. |
| form | Form key/value pairs to serialize in the body. |
| query | Optional query parameters appended to the URL. |
| accept | Optional Accept header override. |
| timeout_sec | Optional per-request timeout in seconds; negative to use default. |
| std::runtime_error | on terminal failure. |
Definition at line 109 of file http_client.cpp.
References apply_server_retry_hint(), corespace::http_response::error_code, next_delay(), status_good(), and update_metrics().

| http_response corespace::http_client::post_raw | ( | std::string_view | url, |
| std::string_view | body, | ||
| std::string_view | content_type, | ||
| const parameter_list & | query = {}, | ||
| std::string_view | accept = {}, | ||
| int | timeout_sec = -1 ) |
Perform an HTTP POST with a raw body.
Builds a URL from url and query and posts the raw body with Content-Type set to content_type. Retry behaviour and metrics follow the same semantics as get().
| url | Endpoint URL. |
| body | Raw request body to send. |
| content_type | Content-Type header value for the body. |
| query | Optional query parameters appended to the URL. |
| accept | Optional Accept header override. |
| timeout_sec | Optional per-request timeout in seconds; negative to use default. |
| std::runtime_error | on terminal failure. |
Definition at line 145 of file http_client.cpp.
References apply_server_retry_hint(), corespace::http_response::error_code, next_delay(), status_good(), and update_metrics().

|
private |
Execute a single HTTP GET using the prepared URL handle.
Side effects:
elapsed.| url_handle | Prepared CURLU handle (owned by caller). |
| elapsed | Out: time spent in curl_easy_perform. |
| accept | Optional Accept header override; empty means use client default. |
| timeout_sec | Optional per-request timeout in seconds; negative to use client default. |
Definition at line 209 of file http_client.cpp.
References corespace::http_response::error_code, corespace::http_response::error_message, and update_headers().

|
private |
Execute a single HTTP POST with given body and content type.
Sets temporary headers (Content-Type and Accept), posts the body, measures elapsed time in elapsed, reads status and headers, and records any libcurl error message.
| url_handle | Prepared CURLU handle (owned by caller). |
| elapsed | Out: time spent in curl_easy_perform. |
| content_type | Content-Type header value. |
| body | Body bytes to send. |
| accept | Optional Accept header override; empty means use client default. |
| timeout_sec | Optional per-request timeout in seconds; negative to use client default. |
Definition at line 258 of file http_client.cpp.
References corespace::http_response::error_code, corespace::http_response::error_message, and update_headers().

|
staticnodiscardprivate |
Success predicate: transport OK and HTTP 2xx.
| response | Response to check. |
Definition at line 358 of file http_client.cpp.
References corespace::http_response::error_code.
Referenced by get(), post_form(), and post_raw().

|
staticnodiscardprivate |
Retry predicate for transient outcomes.
Retries on:
| response | Response to inspect. |
| net_ok | Whether the transport completed without libcurl error. |
Definition at line 363 of file http_client.cpp.
|
private |
Refresh the header multimap from the last transfer.
Enumerates headers via curl_easy_nextheader and fills response.header.
| response | Response object to update. |
Definition at line 335 of file http_client.cpp.
Referenced by request_get(), and request_post().

|
private |
Update counters and histograms after an attempt.
Increments requests, accumulates network_ms, bumps status histogram (if within bounds), and adds to bytes_received.
| response | Result of the attempt. |
| elapsed | Duration spent in libcurl during the attempt. |
Definition at line 346 of file http_client.cpp.
Referenced by get(), post_form(), and post_raw().

|
staticprivate |
libcurl write callback: append chunk to response body.
| ptr | Pointer to received data. |
| size | Element size. |
| n | Number of elements. |
| data | std::string* accumulator (response body). |
Definition at line 390 of file http_client.cpp.
|
private |
Reused easy handle (not thread-safe).
Definition at line 284 of file http_client.hpp.
|
private |
Owned request header list.
Definition at line 287 of file http_client.hpp.
|
private |
Aggregated metrics (atomic counters).
Definition at line 282 of file http_client.hpp.
|
mutableprivate |
Definition at line 283 of file http_client.hpp.
|
private |