Skip to main content

serenade_http_client/
client.rs

1//! [`HttpClient`] trait.
2
3use crate::{ClientRequest, ClientResponse, HttpClientError};
4
5/// Outbound HTTP client (Symfony `HttpClientInterface` analogue).
6pub trait HttpClient: Send + Sync {
7    /// Sends `request` and returns the response.
8    ///
9    /// # Errors
10    ///
11    /// Returns [`HttpClientError`] on transport or construction failure.
12    fn send(
13        &self,
14        request: ClientRequest,
15    ) -> impl std::future::Future<Output = Result<ClientResponse, HttpClientError>> + Send;
16
17    /// Convenience GET.
18    ///
19    /// # Errors
20    ///
21    /// Propagates [`Self::send`].
22    fn get(
23        &self,
24        url: impl Into<String> + Send,
25    ) -> impl std::future::Future<Output = Result<ClientResponse, HttpClientError>> + Send {
26        async move { self.send(ClientRequest::get(url)).await }
27    }
28
29    /// Convenience POST with optional body bytes.
30    ///
31    /// # Errors
32    ///
33    /// Propagates [`Self::send`].
34    fn post(
35        &self,
36        url: impl Into<String> + Send,
37        body: impl Into<Vec<u8>> + Send,
38    ) -> impl std::future::Future<Output = Result<ClientResponse, HttpClientError>> + Send {
39        async move { self.send(ClientRequest::post(url).body(body)).await }
40    }
41}