http_sender/
api.rs

1use http::{Request, Response};
2use http_body::Body;
3use std::future::Future;
4use std::pin::Pin;
5
6/// HttpSend is used to send HTTP requests and receive responses.
7pub trait HttpSend<RequestBody>
8where
9    RequestBody: Body + 'static,
10{
11    type ResponseBody: Body + 'static;
12    type Error: std::error::Error;
13
14    fn send(
15        &self,
16        req: Request<RequestBody>,
17    ) -> impl Future<Output = Result<Response<Self::ResponseBody>, Self::Error>> + Send;
18}
19
20/// HttpSendDyn is the dynamic version of HttpSend that can generate dynamic Futures.
21pub trait HttpSendDyn<RequestBody>
22where
23    RequestBody: Body + 'static,
24{
25    type ResponseBody: Body + 'static;
26    type Error: std::error::Error;
27
28    #[allow(clippy::type_complexity)]
29    fn send_dyn(
30        &self,
31        req: Request<RequestBody>,
32    ) -> Pin<Box<dyn Future<Output = Result<Response<Self::ResponseBody>, Self::Error>> + Send + '_>>;
33}
34
35impl<T, RequestBody> HttpSendDyn<RequestBody> for T
36where
37    RequestBody: Body + 'static,
38    T: HttpSend<RequestBody>,
39{
40    type ResponseBody = T::ResponseBody;
41    type Error = T::Error;
42
43    fn send_dyn(
44        &self,
45        req: Request<RequestBody>,
46    ) -> Pin<Box<dyn Future<Output = Result<Response<Self::ResponseBody>, Self::Error>> + Send + '_>>
47    {
48        Box::pin(HttpSend::send(self, req))
49    }
50}