Skip to main content

better_fetch/backend/
reqwest.rs

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