Skip to main content

serenade_http_client/
dyn_client.rs

1//! Object-safe outbound client for DI (`Arc<dyn DynHttpClient>`).
2
3use std::future::Future;
4use std::pin::Pin;
5
6use crate::{ClientRequest, ClientResponse, HttpClient, HttpClientError, MockHttpClient};
7
8/// Object-safe sibling of [`HttpClient`] for container storage.
9pub trait DynHttpClient: Send + Sync {
10    /// Sends `request` and returns the response.
11    fn send_boxed(
12        &self,
13        request: ClientRequest,
14    ) -> Pin<Box<dyn Future<Output = Result<ClientResponse, HttpClientError>> + Send + '_>>;
15}
16
17impl DynHttpClient for MockHttpClient {
18    fn send_boxed(
19        &self,
20        request: ClientRequest,
21    ) -> Pin<Box<dyn Future<Output = Result<ClientResponse, HttpClientError>> + Send + '_>> {
22        Box::pin(HttpClient::send(self, request))
23    }
24}
25
26#[cfg(feature = "reqwest")]
27impl DynHttpClient for crate::ReqwestHttpClient {
28    fn send_boxed(
29        &self,
30        request: ClientRequest,
31    ) -> Pin<Box<dyn Future<Output = Result<ClientResponse, HttpClientError>> + Send + '_>> {
32        Box::pin(HttpClient::send(self, request))
33    }
34}