pub struct HttpClient { /* private fields */ }Expand description
Async HTTP client with retry and cancellation support.
Wraps reqwest with:
- Exponential backoff retries on 429/503
- Respect for
Retry-Afterheaders - Cancellation via
CancellationToken(checked between retries and viatokio::select!)
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new(
api_url: impl Into<String>,
headers: HeaderMap,
timeout: Option<TimeoutConfig>,
) -> Result<Self, HttpError>
pub fn new( api_url: impl Into<String>, headers: HeaderMap, timeout: Option<TimeoutConfig>, ) -> Result<Self, HttpError>
Create a new HTTP client.
Sourcepub fn with_retry_config(self, config: RetryConfig) -> Self
pub fn with_retry_config(self, config: RetryConfig) -> Self
Create a client with custom retry configuration.
Sourcepub fn with_circuit_breaker(self, cb: Arc<CircuitBreaker>) -> Self
pub fn with_circuit_breaker(self, cb: Arc<CircuitBreaker>) -> Self
Attach a circuit breaker to this client.
When set, every request is gated by the circuit breaker. Successful responses close the circuit; failures (transport-level or 5xx) open it.
Sourcepub async fn post_json(
&self,
payload: &Value,
cancel: Option<&CancellationToken>,
) -> Result<HttpResult, HttpError>
pub async fn post_json( &self, payload: &Value, cancel: Option<&CancellationToken>, ) -> Result<HttpResult, HttpError>
POST JSON with retry logic and optional cancellation.
On 429/503 responses, retries with exponential backoff. Respects
Retry-After headers. Checks the cancellation token between attempts
and races it against each request via tokio::select!.
When a circuit breaker is attached, requests are rejected immediately if the circuit is open.
Sourcepub async fn send_streaming_request(
&self,
url: &str,
payload: &Value,
cancel: Option<&CancellationToken>,
) -> Result<Response, HttpError>
pub async fn send_streaming_request( &self, url: &str, payload: &Value, cancel: Option<&CancellationToken>, ) -> Result<Response, HttpError>
Send a POST request and return the raw response for streaming.
Unlike post_json, this does NOT read the response body. The caller
is responsible for consuming the response (e.g., reading SSE lines).
Retries on transport errors and retryable HTTP status codes (429/503) before any response body has been consumed. Once a successful response is returned to the caller, no further retries are attempted.