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