tetrio_api/http/clients/
reqwest_client.rs

1
2#![cfg(feature = "reqwest_http_client")]
3
4use bytes::Bytes;
5use http::Request;
6
7use async_trait::async_trait;
8use reqwest::Request as Reqwest;
9
10use crate::http::{cached_client::CachedClient, caches::{moka::MokaCache, noop_cache::NoopCache, redis_cache::RedisCache}, error::ErrorTrait};
11
12use super::http_client::HttpClient;
13
14
15/// A reqwest based http client
16pub struct ReqwestClient {
17    client: reqwest::Client
18}
19
20impl Default for ReqwestClient {
21    fn default() -> Self {
22        Self { client: Default::default() }
23    }
24}
25
26#[async_trait]
27impl HttpClient for ReqwestClient {
28    type HttpError = reqwest::Error;
29    async fn execute(&self, request: Request<Vec<u8>>) -> Result<Bytes, Self::HttpError> {
30        let req = Reqwest::try_from(request)?;
31        self.client.execute(req).await?.bytes().await
32    }
33
34}
35
36pub type UncachedReqwestClient = CachedClient<ReqwestClient, NoopCache>;
37pub type UncachedReqwestError = <UncachedReqwestClient as ErrorTrait>::Error;
38
39#[cfg(feature = "in_memory_cache")]
40/// A reqwest based http client using an in-memory cache
41pub type InMemoryReqwestClient = CachedClient<ReqwestClient, MokaCache>;
42#[cfg(feature = "in_memory_cache")]
43pub type InMemoryReqwestError = <InMemoryReqwestClient as ErrorTrait>::Error;
44
45#[cfg(feature = "redis_cache")]
46/// A reqwest based http client using a redis cache
47pub type RedisReqwestClient<'a> = CachedClient<ReqwestClient, RedisCache<'a>>;
48#[cfg(feature = "redis_cache")]
49pub type RedisReqwestError<'a> = <RedisReqwestClient<'a> as ErrorTrait>::Error;