|
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 override={}) |
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 override={}) |
| http_response | post_raw (std::string_view url, std::string_view body, std::string_view content_type, const parameter_list &query={}, std::string_view override={}) |
| 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 override={}) 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 override) const |
| 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 112 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 42 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 360 of file http_client.cpp.
Referenced by get(), post_form(), and post_raw().

|
private |
Definition at line 292 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 173 of file http_client.cpp.
| http_response corespace::http_client::get | ( | std::string_view | url, |
| const parameter_list & | params = {}, | ||
| std::string_view | override = {} ) |
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. |
| override |
| std::runtime_error | on terminal failure as described above. |
Definition at line 68 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 66 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 354 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 | override = {} ) |
Definition at line 103 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 | override = {} ) |
Definition at line 138 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. |
| override |
Definition at line 201 of file http_client.cpp.
References corespace::http_response::error_code, corespace::http_response::error_message, and update_headers().

|
private |
Definition at line 246 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 342 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 347 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 319 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 330 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 374 of file http_client.cpp.
|
private |
Reused easy handle (not thread-safe).
Definition at line 226 of file http_client.hpp.
|
private |
Owned request header list.
Definition at line 229 of file http_client.hpp.
|
private |
Aggregated metrics (atomic counters).
Definition at line 224 of file http_client.hpp.
|
mutableprivate |
Definition at line 225 of file http_client.hpp.
|
private |