Skip to main content

qubit_http/client/
http_client.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! HTTP client: builds requests, applies defaults and interceptors, executes
11//! them with optional retry, and exposes SSE helpers with reconnect.
12//!
13//! Single-shot execution is [`HttpClient::execute`] / [`HttpClient::execute_once`];
14//! retry policy comes from [`crate::HttpClientOptions::retry`] unless overridden
15//! per request.
16//!
17
18use std::time::{
19    Duration,
20    Instant,
21};
22
23use qubit_retry::{
24    AttemptFailure,
25    AttemptFailureDecision,
26    Retry,
27    RetryContext,
28    RetryError,
29    RetryErrorReason,
30};
31
32use crate::sanitize::LogSanitizer;
33use crate::sse::SseReconnectRunner;
34use crate::{
35    response::HttpResponseOptions,
36    sse::{
37        SseMessageStream,
38        SseReconnectOptions,
39    },
40    AsyncHttpHeaderInjector,
41    HttpClientOptions,
42    HttpError,
43    HttpHeaderInjector,
44    HttpLogger,
45    HttpRequest,
46    HttpRequestBuilder,
47    HttpRequestInterceptor,
48    HttpRequestInterceptors,
49    HttpResponse,
50    HttpResponseInterceptor,
51    HttpResponseInterceptors,
52    HttpResponseMeta,
53    HttpResult,
54    HttpRetryOptions,
55};
56
57/// High-level HTTP client: default headers, injectors, interceptors, logging,
58/// timeouts, and optional per-request retry.
59///
60/// [`Clone`] is shallow and cheap enough for typical use (including passing into
61/// retry closures); cloning does not duplicate the underlying connection pool
62/// beyond what [`reqwest::Client`] already shares.
63#[derive(Clone)]
64pub struct HttpClient {
65    /// Pluggable low-level HTTP stack used to send requests (currently reqwest).
66    pub(super) backend: reqwest::Client,
67    /// Timeouts, proxy, logging, default headers, and related settings.
68    pub(super) options: HttpClientOptions,
69    /// Header injectors applied to every outgoing request after default
70    /// headers.
71    pub(super) injectors: Vec<HttpHeaderInjector>,
72    /// Async header injectors applied after sync injectors and before request-level headers.
73    pub(super) async_injectors: Vec<AsyncHttpHeaderInjector>,
74    /// Request interceptors applied before request send for each attempt.
75    request_interceptors: HttpRequestInterceptors,
76    /// Response interceptors applied on successful responses before return.
77    response_interceptors: HttpResponseInterceptors,
78}
79
80impl HttpClient {
81    /// Wraps a built [`reqwest::Client`] with the given options and an empty
82    /// injector list.
83    ///
84    /// # Parameters
85    /// - `backend`: Configured low-level HTTP client used for I/O.
86    /// - `options`: Client-wide timeouts, headers, proxy, logging, etc.
87    ///
88    /// # Returns
89    /// A new [`HttpClient`] with no injectors until
90    /// [`HttpClient::add_header_injector`] is called.
91    pub(crate) fn new(backend: reqwest::Client, options: HttpClientOptions) -> Self {
92        Self {
93            backend,
94            options,
95            injectors: Vec::new(),
96            async_injectors: Vec::new(),
97            request_interceptors: HttpRequestInterceptors::new(),
98            response_interceptors: HttpResponseInterceptors::new(),
99        }
100    }
101
102    /// Returns a reference to the client-wide options (timeouts, proxy, logging,
103    /// default headers, retry defaults, etc.).
104    ///
105    /// # Returns
106    /// Immutable borrow of [`HttpClientOptions`]. Never `None`; always the
107    /// options installed on this client.
108    pub fn options(&self) -> &HttpClientOptions {
109        &self.options
110    }
111
112    /// Appends a [`HttpHeaderInjector`] so its mutation function runs on every
113    /// request. Mutates `self` in place.
114    ///
115    /// # Parameters
116    /// - `injector`: Injector to append (order is preserved).
117    pub fn add_header_injector(&mut self, injector: HttpHeaderInjector) {
118        self.injectors.push(injector);
119    }
120
121    /// Appends an async header injector whose mutation runs after sync injectors.
122    /// Mutates `self` in place.
123    ///
124    /// # Parameters
125    /// - `injector`: Async injector to append (order is preserved).
126    pub fn add_async_header_injector(&mut self, injector: AsyncHttpHeaderInjector) {
127        self.async_injectors.push(injector);
128    }
129
130    /// Appends a request interceptor run before each send attempt (including
131    /// each retry attempt). Mutates `self` in place.
132    ///
133    /// # Parameters
134    /// - `interceptor`: Request interceptor to append (order is preserved).
135    pub fn add_request_interceptor(&mut self, interceptor: HttpRequestInterceptor) {
136        self.request_interceptors.push(interceptor);
137    }
138
139    /// Appends a response interceptor run only after a successful HTTP status
140    /// (after the internal `execute_once` step) and before response body logging.
141    /// Mutates `self` in place.
142    ///
143    /// # Parameters
144    /// - `interceptor`: Response interceptor to append (order is preserved).
145    pub fn add_response_interceptor(&mut self, interceptor: HttpResponseInterceptor) {
146        self.response_interceptors.push(interceptor);
147    }
148
149    /// Validates and adds one client-level default header.
150    ///
151    /// The header is applied to every request before header injectors and
152    /// request-level headers.
153    ///
154    /// # Parameters
155    /// - `name`: Header name.
156    /// - `value`: Header value.
157    ///
158    /// # Returns
159    /// `Ok(self)` after the header is stored.
160    ///
161    /// # Errors
162    /// Returns [`HttpError`] when the header name or value is invalid.
163    pub fn add_header(&mut self, name: &str, value: &str) -> HttpResult<&mut Self> {
164        self.options.add_header(name, value)?;
165        Ok(self)
166    }
167
168    /// Validates and adds many client-level default headers atomically.
169    ///
170    /// If any input pair is invalid, no header from this batch is applied.
171    ///
172    /// # Parameters
173    /// - `headers`: Iterator of `(name, value)` pairs.
174    ///
175    /// # Returns
176    /// `Ok(self)` after all headers are stored.
177    ///
178    /// # Errors
179    /// Returns [`HttpError`] when any name/value pair is invalid (nothing from
180    /// this call is applied).
181    pub fn add_headers(&mut self, headers: &[(&str, &str)]) -> HttpResult<&mut Self> {
182        self.options.add_headers(headers)?;
183        Ok(self)
184    }
185
186    /// Clears all synchronous header injectors. Mutates `self` in place.
187    pub fn clear_header_injectors(&mut self) {
188        self.injectors.clear();
189    }
190
191    /// Clears all async header injectors. Mutates `self` in place.
192    pub fn clear_async_header_injectors(&mut self) {
193        self.async_injectors.clear();
194    }
195
196    /// Clears all request interceptors. Mutates `self` in place.
197    pub fn clear_request_interceptors(&mut self) {
198        self.request_interceptors.clear();
199    }
200
201    /// Clears all response interceptors. Mutates `self` in place.
202    pub fn clear_response_interceptors(&mut self) {
203        self.response_interceptors.clear();
204    }
205
206    /// Starts building an [`HttpRequest`] with the given method and path
207    /// (relative or absolute URL string).
208    ///
209    /// # Parameters
210    /// - `method`: HTTP verb (GET, POST, …).
211    /// - `path`: Path relative to [`HttpClientOptions::base_url`] or a full URL
212    ///   string.
213    ///
214    /// # Returns
215    /// A new [`HttpRequestBuilder`] borrowing this client for defaults; it is
216    /// not sent until built and passed to [`HttpClient::execute`] (or related
217    /// APIs).
218    pub fn request(&self, method: http::Method, path: &str) -> HttpRequestBuilder {
219        HttpRequestBuilder::new(method, path, self)
220    }
221
222    /// Returns a clone of the client-level default header map.
223    ///
224    /// Used when constructing a built [`HttpRequest`] so the snapshot reflects
225    /// headers at build time.
226    ///
227    /// # Returns
228    /// Owned [`http::HeaderMap`] copy of [`HttpClientOptions`] default headers.
229    pub(crate) fn headers_snapshot(&self) -> http::HeaderMap {
230        self.options.default_headers.clone()
231    }
232
233    /// Returns a clone of the registered synchronous header injectors list.
234    ///
235    /// # Returns
236    /// New [`Vec`] with the same injectors and order as on this client.
237    pub(crate) fn injectors_snapshot(&self) -> Vec<HttpHeaderInjector> {
238        self.injectors.clone()
239    }
240
241    /// Returns a clone of the registered async header injectors list.
242    ///
243    /// # Returns
244    /// New [`Vec`] with the same injectors and order as on this client.
245    pub(crate) fn async_injectors_snapshot(&self) -> Vec<AsyncHttpHeaderInjector> {
246        self.async_injectors.clone()
247    }
248
249    /// Sends the request and returns a unified [`HttpResponse`].
250    ///
251    /// Chooses retry vs single attempt from resolved [`HttpRetryOptions`] for
252    /// this request. Performs network I/O and may await the internal
253    /// `execute_once` path
254    /// multiple times with backoff between attempts when retry is enabled.
255    ///
256    /// # Parameters
257    /// - `request`: Built request (URL resolved against `base_url` if path is
258    ///   not absolute).
259    ///
260    /// # Returns
261    /// - `Ok(HttpResponse)` when the HTTP status is success
262    ///   ([`http::StatusCode::is_success`]).
263    /// - `Err(HttpError)` when any attempt fails for URL/header validation,
264    ///   cancellation, interceptor failure, transport/timeout, non-success
265    ///   status, or when the retry executor aborts or exceeds limits.
266    pub async fn execute(&self, request: HttpRequest) -> HttpResult<HttpResponse> {
267        let retry_options = self.options.retry.resolve(&request);
268        if retry_options.should_retry(&request) {
269            self.execute_with_retry(request, retry_options).await
270        } else {
271            self.execute_once(request).await
272        }
273    }
274
275    /// Performs one non-retrying execution: pre-send cancellation check,
276    /// request interceptors, resolve URL, merge headers, log the request, send
277    /// with configured timeouts, map non-success status to an error, then
278    /// response interceptors and response logging. The returned body is read
279    /// lazily according to [`HttpResponse`].
280    ///
281    /// # Parameters
282    /// - `request`: Built request to send (same fields as for
283    ///   [`HttpClient::execute`]).
284    ///
285    /// # Returns
286    /// - `Ok(HttpResponse)` on success status and after interceptors/logging
287    ///   steps succeed.
288    /// - `Err(HttpError)` from request/response interceptors, cancellation,
289    ///   send/transport errors, status mapping, URL resolution for the response
290    ///   wrapper, or response logging failures.
291    ///
292    /// # Side effects
293    /// Network I/O, optional logging, and user-provided interceptor callbacks.
294    pub(crate) async fn execute_once(&self, request: HttpRequest) -> HttpResult<HttpResponse> {
295        let mut request = request;
296        if let Some(error) = request.cancelled_error_if_needed("Request cancelled before sending") {
297            return Err(error);
298        }
299        self.request_interceptors.apply(&mut request)?;
300        let response = self
301            .prepare_and_send_once(request, "Request cancelled before sending")
302            .await?;
303        let mut response = response.into_success_or_status_error("HTTP request failed").await?;
304        self.response_interceptors.apply(&mut response.meta)?;
305        let logger = HttpLogger::new(&self.options);
306        logger.log_response(&mut response).await?;
307        Ok(response)
308    }
309
310    /// Single low-level send: cancellation check, request logging, one backend
311    /// round-trip, then wraps the backend response as [`HttpResponse`].
312    ///
313    /// Does not run response interceptors or success-status enforcement; those
314    /// happen in [`HttpClient::execute_once`] after this returns.
315    ///
316    /// # Parameters
317    /// - `request`: Request to send (may be mutated for logging/send path).
318    /// - `cancellation_message`: Message embedded if the request is already
319    ///   cancelled when this runs.
320    ///
321    /// # Returns
322    /// - `Ok(HttpResponse)` with lazy body and metadata.
323    /// - `Err(HttpError)` if cancelled before send, URL resolution fails, or
324    ///   send fails.
325    ///
326    /// # Side effects
327    /// Async network I/O and request logging via [`HttpLogger`].
328    async fn prepare_and_send_once(
329        &self,
330        request: HttpRequest,
331        cancellation_message: &str,
332    ) -> HttpResult<HttpResponse> {
333        let mut request = request;
334        if let Some(error) = request.cancelled_error_if_needed(cancellation_message) {
335            return Err(error);
336        }
337        let logger = HttpLogger::new(&self.options);
338        let request_url = request.resolved_url()?;
339        let backend_response = request.send_impl(&self.backend, &logger).await?;
340        let meta = HttpResponseMeta::new(
341            backend_response.status(),
342            backend_response.headers().clone(),
343            backend_response.url().clone(),
344            request.method().clone(),
345        )
346        .with_log_sanitize_policy(self.options.log_sanitize_policy.clone());
347        let response_options = HttpResponseOptions::new(
348            self.options.error_response_preview_limit,
349            self.options.sse_json_mode,
350            self.options.sse_max_line_bytes,
351            self.options.sse_max_frame_bytes,
352            self.options.sse_done_marker_policy.clone(),
353            LogSanitizer::new(self.options.log_sanitize_policy.clone()),
354        );
355        Ok(HttpResponse::from_backend(
356            meta,
357            backend_response,
358            request.read_timeout(),
359            request.cancellation_token().cloned(),
360            request_url,
361            response_options,
362        ))
363    }
364
365    /// Runs [`HttpClient::execute_once`] under the given retry policy.
366    ///
367    /// Between attempts waits according to the resolved retry delay, optionally
368    /// honoring `Retry-After` by extending the next sleep. Each attempt clones
369    /// the request so request bodies can be rebuilt when supported.
370    ///
371    /// # Parameters
372    /// - `request`: Built request passed to each [`HttpClient::execute_once`]
373    ///   attempt (cloned per retry closure).
374    /// - `options`: Effective retry options for this request (from resolution
375    ///   in [`HttpClient::execute`]).
376    ///
377    /// # Returns
378    /// - `Ok(HttpResponse)` when an attempt completes with success status.
379    /// - `Err(HttpError)` from any [`HttpClient::execute_once`] failure that is
380    ///   non-retryable, or from retry exhaustion/max-duration enforcement.
381    ///
382    /// # Side effects
383    /// Multiple async HTTP attempts and optional sleeps.
384    async fn execute_with_retry(&self, request: HttpRequest, options: HttpRetryOptions) -> HttpResult<HttpResponse> {
385        let honor_retry_after = request.retry_override().should_honor_retry_after();
386        let retry_options = options.to_executor_options();
387        let started_at = Instant::now();
388
389        let retry_policy_options = options.clone();
390        let retry_delay_options = retry_options.clone();
391        let retry_policy = Retry::<HttpError>::builder()
392            .options(retry_options)
393            .retry_after_from_error(move |error| honor_retry_after.then_some(error.retry_after).flatten())
394            .on_failure(move |failure: &AttemptFailure<HttpError>, context: &RetryContext| {
395                Self::retry_failure_decision(failure, context, &retry_policy_options, &retry_delay_options)
396            })
397            .build()
398            .expect("validated HTTP retry options should build retry policy");
399
400        let cancellation_token = request.cancellation_token().cloned();
401        let request_method = request.method().clone();
402        let request_url = request.resolved_url().ok();
403        let retry_request = request.clone();
404        let retry_future = retry_policy.run_async(|| {
405            let attempt_request = retry_request.clone();
406            async move { self.execute_once(attempt_request).await }
407        });
408
409        let retry_result = if let Some(token) = cancellation_token.as_ref() {
410            tokio::select! {
411                _ = token.cancelled() => {
412                    return Err(Self::retry_cancelled_error(
413                        "HTTP retry cancelled while waiting before next attempt",
414                        &request_method,
415                        request_url.as_ref(),
416                    ));
417                }
418                result = retry_future => result,
419            }
420        } else {
421            retry_future.await
422        };
423
424        match retry_result {
425            Ok(response) => Ok(response),
426            Err(error) => Err(Self::map_retry_error(
427                error,
428                started_at,
429                options.max_duration,
430                options.max_attempts,
431            )),
432        }
433    }
434
435    /// Returns whether `error` is retryable under `options`.
436    ///
437    /// # Parameters
438    /// - `error`: Error produced by a single HTTP attempt.
439    /// - `options`: Effective retry options for the request.
440    ///
441    /// # Returns
442    /// `true` if another attempt may be scheduled.
443    fn is_retryable_error(error: &HttpError, options: &HttpRetryOptions) -> bool {
444        if error.kind == crate::HttpErrorKind::Status {
445            error.status.is_some_and(|status| options.is_retryable_status(status))
446        } else {
447            options.is_retryable_error_kind(error.kind)
448        }
449    }
450
451    /// Computes the sleep before the next retry attempt.
452    ///
453    /// # Parameters
454    /// - `base_delay`: Delay selected from retry policy and jitter.
455    /// - `retry_after_hint`: Retry-After delay extracted by the retry policy.
456    ///
457    /// # Returns
458    /// `base_delay`, or the larger `Retry-After` value when present.
459    fn retry_sleep_delay(base_delay: Duration, retry_after_hint: Option<Duration>) -> Duration {
460        retry_after_hint
461            .map(|retry_after| retry_after.max(base_delay))
462            .unwrap_or(base_delay)
463    }
464
465    /// Decides how HTTP retry should handle one failed attempt.
466    ///
467    /// # Parameters
468    /// - `failure`: Failed attempt reported by `qubit-retry`.
469    /// - `context`: Retry context for the failed attempt.
470    /// - `policy_options`: HTTP retry allowlists and method policy.
471    /// - `delay_options`: Retry executor options used to calculate base delay.
472    ///
473    /// # Returns
474    /// Decision for `qubit-retry`: abort non-retryable HTTP errors, otherwise
475    /// retry after the larger base delay / Retry-After hint. Non-HTTP runtime
476    /// failures fall back to the retry executor default.
477    fn retry_failure_decision(
478        failure: &AttemptFailure<HttpError>,
479        context: &RetryContext,
480        policy_options: &HttpRetryOptions,
481        delay_options: &qubit_retry::RetryOptions,
482    ) -> AttemptFailureDecision {
483        let error = failure
484            .as_error()
485            .expect("HTTP retry attempts do not configure non-HTTP attempt failures");
486        if !Self::is_retryable_error(error, policy_options) {
487            return AttemptFailureDecision::Abort;
488        }
489
490        let base_delay = delay_options.delay_for_attempt(context.attempt());
491        let sleep_delay = Self::retry_sleep_delay(base_delay, context.retry_after_hint());
492        AttemptFailureDecision::RetryAfter(sleep_delay)
493    }
494
495    /// Adds retry-attempt exhaustion context to the last attempt error.
496    ///
497    /// # Parameters
498    /// - `error`: Last retryable attempt error.
499    /// - `attempts`: Number of attempts already made.
500    /// - `max_attempts`: Configured maximum attempts.
501    ///
502    /// # Returns
503    /// The same error with retry exhaustion details appended to its message.
504    fn map_retry_attempts_exhausted(mut error: HttpError, attempts: u32, max_attempts: u32) -> HttpError {
505        error.message = format!(
506            "{} (retry attempts exhausted: {attempts}/{max_attempts})",
507            error.message
508        );
509        error
510    }
511
512    /// Builds the error returned when retry policy stops early.
513    ///
514    /// # Parameters
515    /// - `error`: Attempt error that the retry policy chose not to retry.
516    /// - `attempts`: Number of attempts already made.
517    /// - `started_at`: Start instant of the retry flow.
518    ///
519    /// # Returns
520    /// [`HttpError::retry_aborted`] with the original [`HttpError`] chained as
521    /// source for callers that need the underlying status or transport error.
522    fn map_retry_aborted(error: HttpError, attempts: u32, started_at: Instant) -> HttpError {
523        let elapsed = started_at.elapsed();
524        let summary = error.message.clone();
525        HttpError::retry_aborted(format!(
526            "HTTP retry aborted after {attempts} attempt(s) in {elapsed:?}: {summary}"
527        ))
528        .with_source(error)
529    }
530
531    /// Builds the error when retry max-duration is exhausted.
532    ///
533    /// # Parameters
534    /// - `started_at`: Start instant of the retry flow.
535    /// - `max_duration`: Configured max-duration budget.
536    /// - `last_error`: Last captured retryable attempt error, if any.
537    ///
538    /// # Returns
539    /// Augments the last failure when present, otherwise a dedicated
540    /// max-duration error with no underlying attempt error.
541    fn map_retry_max_duration_exceeded(
542        started_at: Instant,
543        max_duration: Duration,
544        last_error: Option<HttpError>,
545    ) -> HttpError {
546        let elapsed = started_at.elapsed();
547        let max_duration_text = format!("{max_duration:?}");
548        match last_error {
549            Some(mut error) => {
550                error.message = format!(
551                    "{} (retry max duration exceeded: {elapsed:?}/{max_duration_text})",
552                    error.message
553                );
554                error
555            }
556            None => HttpError::retry_max_elapsed_exceeded(format!(
557                "HTTP retry max duration exceeded before a retryable error was captured: {elapsed:?}/{max_duration_text}"
558            )),
559        }
560    }
561
562    /// Maps a [`qubit_retry::RetryError`] into this crate's HTTP error model.
563    ///
564    /// # Parameters
565    /// - `error`: Terminal retry error from `qubit-retry`.
566    /// - `started_at`: Monotonic start instant of the HTTP retry flow.
567    /// - `max_duration`: Optional HTTP total retry budget.
568    /// - `max_attempts`: Configured maximum HTTP attempts.
569    ///
570    /// # Returns
571    /// A rich [`HttpError`] preserving the last attempt error when available.
572    fn map_retry_error(
573        error: RetryError<HttpError>,
574        started_at: Instant,
575        max_duration: Option<Duration>,
576        max_attempts: u32,
577    ) -> HttpError {
578        let attempts = error.attempts();
579        let reason = error.reason();
580        let (_, last_failure, _) = error.into_parts();
581        let last_error = last_failure.and_then(AttemptFailure::into_error);
582
583        match reason {
584            RetryErrorReason::AttemptsExceeded => {
585                let error = last_error.expect("HTTP retry attempts exceeded should preserve last error");
586                Self::map_retry_attempts_exhausted(error, attempts, max_attempts)
587            }
588            RetryErrorReason::MaxOperationElapsedExceeded | RetryErrorReason::MaxTotalElapsedExceeded => {
589                let max_duration = max_duration.expect("HTTP retry elapsed limit requires max_duration");
590                Self::map_retry_max_duration_exceeded(started_at, max_duration, last_error)
591            }
592            RetryErrorReason::Aborted => {
593                let error = last_error.expect("HTTP retry abort should preserve last error");
594                if error.kind == crate::HttpErrorKind::Cancelled {
595                    error
596                } else {
597                    Self::map_retry_aborted(error, attempts, started_at)
598                }
599            }
600            RetryErrorReason::UnsupportedOperation | RetryErrorReason::WorkerStillRunning => HttpError::other(format!(
601                "HTTP retry executor failed after {attempts} attempt(s): {reason:?}"
602            )),
603        }
604    }
605
606    /// Builds a cancellation error for retry wait cancellation.
607    ///
608    /// # Parameters
609    /// - `message`: Human-readable cancellation reason.
610    /// - `method`: Request method to attach.
611    /// - `url`: Optional resolved request URL to attach.
612    ///
613    /// # Returns
614    /// [`HttpErrorKind::Cancelled`](crate::HttpErrorKind::Cancelled) with request
615    /// context.
616    fn retry_cancelled_error(message: &str, method: &http::Method, url: Option<&url::Url>) -> HttpError {
617        let mut error = HttpError::cancelled(message).with_method(method);
618        if let Some(url) = url {
619            error = error.with_url(url);
620        }
621        error
622    }
623
624    /// Opens an SSE stream and reconnects automatically on retryable stream
625    /// failures.
626    ///
627    /// Reconnect behavior:
628    /// - retryable transport/read failures trigger reconnects;
629    /// - optional reconnect on clean EOF (`reconnect_on_eof`);
630    /// - `Last-Event-ID` is set from the latest parsed SSE last-event-id state;
631    /// - optional use of SSE `retry:` as next reconnect delay.
632    ///
633    /// # Parameters
634    /// - `request`: SSE request template reused on reconnect.
635    /// - `options`: Reconnect limits and delay policy.
636    ///
637    /// # Returns
638    /// SSE message stream yielding messages from one or more reconnect sessions.
639    ///
640    /// # Errors
641    /// Stream items are `Result`; `Err` covers per-item failures such as:
642    /// - initial stream-open failures when not reconnectable or retries exhausted;
643    /// - SSE protocol errors (non-reconnectable by default);
644    /// - transport/read errors after reconnect budget is exhausted.
645    ///
646    /// # Side effects
647    /// Performs repeated HTTP requests and reads on reconnect; may sleep between
648    /// attempts according to reconnect options.
649    pub fn execute_sse_with_reconnect(&self, request: HttpRequest, options: SseReconnectOptions) -> SseMessageStream {
650        SseReconnectRunner::new(self.clone(), request, options).run()
651    }
652}
653
654impl std::fmt::Debug for HttpClient {
655    /// Formats the client for debugging (exposes options and injectors; omits
656    /// the backend client).
657    ///
658    /// # Parameters
659    /// - `f`: Destination formatter.
660    ///
661    /// # Returns
662    /// `fmt::Result` from writing the debug struct.
663    ///
664    /// # Errors
665    /// Returns an error if formatting to `f` fails.
666    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
667        f.debug_struct("HttpClient")
668            .field("options", &self.options)
669            .field("injectors", &self.injectors)
670            .field("async_injectors", &self.async_injectors)
671            .field("request_interceptors", &self.request_interceptors)
672            .field("response_interceptors", &self.response_interceptors)
673            .finish_non_exhaustive()
674    }
675}