1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
pub use http_api_client;
pub use isahc;

use std::time::Duration;

pub use http_api_client::Client;
#[cfg(any(
    feature = "with-sleep-via-tokio",
    feature = "with-sleep-via-futures-timer",
    feature = "with-sleep-via-async-io"
))]
pub use http_api_client::RetryableClient;
use http_api_client::{async_trait, Body, Request, Response};
use isahc::{
    config::Configurable as _, AsyncReadResponseExt as _, Error as IsahcError,
    HttpClient as IsahcHttpClient,
};

#[derive(Debug, Clone)]
pub struct IsahcClient {
    pub http_client: IsahcHttpClient,
    pub body_buf_default_capacity: usize,
}

impl IsahcClient {
    pub fn new() -> Result<Self, IsahcError> {
        Ok(Self::with(
            IsahcHttpClient::builder()
                .connect_timeout(Duration::from_secs(5))
                .timeout(Duration::from_secs(30))
                .build()?,
        ))
    }

    pub fn with(http_client: IsahcHttpClient) -> Self {
        Self {
            http_client,
            body_buf_default_capacity: 4 * 1024,
        }
    }
}

#[async_trait]
impl Client for IsahcClient {
    type RespondError = IsahcError;

    async fn respond(&self, request: Request<Body>) -> Result<Response<Body>, Self::RespondError> {
        let res = self.http_client.send_async(request).await?;
        let (head, body) = res.into_parts();

        let mut body_buf = Vec::with_capacity(
            body.len()
                .unwrap_or_else(|| self.body_buf_default_capacity as u64) as usize,
        );

        let mut res = Response::from_parts(head, body);
        res.copy_to(&mut body_buf).await?;

        let (head, _) = res.into_parts();
        let res = Response::from_parts(head, body_buf);

        Ok(res)
    }
}

#[cfg(all(
    feature = "with-sleep-via-tokio",
    not(feature = "with-sleep-via-futures-timer"),
    not(feature = "with-sleep-via-async-io")
))]
#[async_trait]
impl RetryableClient for IsahcClient {
    async fn sleep(&self, dur: Duration) {
        tokio::time::sleep(dur).await;
    }
}

#[cfg(all(
    not(feature = "with-sleep-via-tokio"),
    feature = "with-sleep-via-futures-timer",
    not(feature = "with-sleep-via-async-io")
))]
#[async_trait]
impl RetryableClient for IsahcClient {
    async fn sleep(&self, dur: Duration) {
        futures_timer::Delay::new(dur).await;
    }
}

#[cfg(all(
    not(feature = "with-sleep-via-tokio"),
    not(feature = "with-sleep-via-futures-timer"),
    feature = "with-sleep-via-async-io"
))]
#[async_trait]
impl RetryableClient for IsahcClient {
    async fn sleep(&self, dur: Duration) {
        async_io::Timer::after(dur).await;
    }
}