pub struct HttpClient {
pub client: Client,
pub base_url: Url,
/* private fields */
}Expand description
Shared HTTP client with base URL, optional rate limiter, and retry config.
This is the common structure used by all API clients to hold the configured reqwest client, base URL, and rate-limiting state.
Fields§
§client: ClientThe underlying reqwest HTTP client
base_url: UrlBase URL for API requests
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn with_base_url(&self, base_url: &str) -> Result<Self, ApiError>
pub fn with_base_url(&self, base_url: &str) -> Result<Self, ApiError>
Clone this client, pointed at a different base URL.
The underlying reqwest client (and so its connection pool), rate limiter, retry config, and concurrency limiter are all shared with the original. Use this to reach a sibling API host that should share the same transport configuration and request budget — several Polymarket APIs live on their own subdomains but are consumed by one client.
Sharing the concurrency limiter is deliberate: the limit exists to keep Cloudflare from seeing a burst from this process, and that is a per-process concern rather than a per-host one.
§Path prefixes are ignored
Request paths are absolute (/user-pnl), and resolving an absolute
path against a base replaces the base’s path entirely. So a prefix in
base_url is silently dropped, with or without a trailing slash:
http://host + /user-pnl -> http://host/user-pnl
http://host/proxy + /user-pnl -> http://host/user-pnl (prefix gone)
http://host/proxy/ + /user-pnl -> http://host/user-pnl (prefix gone)Point this at a scheme, host, and port — not at a sub-path. Fronting the API with a path-prefixed reverse proxy is not supported.
Sourcepub async fn acquire_rate_limit(&self, path: &str, method: Option<&Method>)
pub async fn acquire_rate_limit(&self, path: &str, method: Option<&Method>)
Await rate limiter for the given endpoint path + method.
Sourcepub async fn acquire_concurrency(&self) -> Option<OwnedSemaphorePermit>
pub async fn acquire_concurrency(&self) -> Option<OwnedSemaphorePermit>
Acquire a concurrency permit, if a limiter is configured.
The returned permit must be held until the HTTP response has been
received. Dropping the permit releases the concurrency slot.
Returns None when no concurrency limit is set.
Sourcepub fn should_retry(
&self,
status: StatusCode,
attempt: u32,
retry_after: Option<&str>,
) -> Option<Duration>
pub fn should_retry( &self, status: StatusCode, attempt: u32, retry_after: Option<&str>, ) -> Option<Duration>
Check if a 429 response should be retried; returns backoff duration if yes.
When retry_after is Some, the server-provided delay is used instead of
the client-computed exponential backoff (clamped to max_backoff_ms).
Sourcepub async fn get_bytes(
&self,
path: &str,
query: &[(String, String)],
) -> Result<Vec<u8>, ApiError>
pub async fn get_bytes( &self, path: &str, query: &[(String, String)], ) -> Result<Vec<u8>, ApiError>
GET a URL and return the raw response body as bytes.
Use this for endpoints that return non-JSON payloads (e.g. application/zip
downloads). Applies the same rate-limiting, concurrency gating, and 429
retry behavior as the JSON-oriented Request helper.
Non-2xx responses are mapped to ApiError via
ApiError::from_response.
§Errors
Returns ApiError on URL-join failure, network errors, or non-2xx
responses.
Trait Implementations§
Source§impl Clone for HttpClient
impl Clone for HttpClient
Source§fn clone(&self) -> HttpClient
fn clone(&self) -> HttpClient
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more