Skip to main content

better_fetch/backend/
reqwest.rs

1use async_trait::async_trait;
2use reqwest::Client;
3
4use super::exec::send_reqwest;
5use super::{HttpBackend, HttpRequest, HttpResponse};
6use crate::Result;
7
8/// Reqwest-backed HTTP backend.
9#[derive(Debug, Clone)]
10pub struct ReqwestBackend {
11    client: Client,
12}
13
14impl ReqwestBackend {
15    pub fn new(client: Client) -> Self {
16        Self { client }
17    }
18
19    pub fn client(&self) -> &Client {
20        &self.client
21    }
22}
23
24impl Default for ReqwestBackend {
25    fn default() -> Self {
26        Self::new(Client::new())
27    }
28}
29
30#[async_trait]
31impl HttpBackend for ReqwestBackend {
32    async fn execute(&self, request: HttpRequest) -> Result<HttpResponse> {
33        send_reqwest(&self.client, request).await
34    }
35}