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
pub extern crate hyper;
pub use error::{Error, Result};

use hyper::client::connect::{Connect, HttpConnector};
use interfacer_http::http::{Request, Response};
use interfacer_http::{async_trait, Helper, HttpClient};
mod error;

#[derive(Clone)]
pub struct Client<C> {
    inner: hyper::Client<C, hyper::Body>,
    helper: Helper,
}

impl Client<HttpConnector> {
    pub fn new() -> Self {
        Self {
            inner: Default::default(),
            helper: Default::default(),
        }
    }
}

impl<C> Client<C> {
    pub fn base_on(client: hyper::Client<C, hyper::Body>) -> Self {
        Self {
            inner: client,
            helper: Default::default(),
        }
    }

    pub fn with_helper(self, helper: Helper) -> Self {
        Self { helper, ..self }
    }
}

#[async_trait]
impl<C> HttpClient for Client<C>
where
    C: Connect + Sync + 'static,
    C::Transport: 'static,
    C::Future: 'static,
{
    type Err = Error;
    async fn request(&self, req: Request<Vec<u8>>) -> Result<Response<Vec<u8>>> {
        let (parts, body) = req.into_parts();
        let (parts, mut body) = self
            .inner
            .request(Request::from_parts(parts, body.into()))
            .await?
            .into_parts();
        let mut data = Vec::new();
        while let Some(chunk) = body.next().await {
            data.extend_from_slice(&chunk?);
        }
        Ok(Response::from_parts(parts, data))
    }

    fn helper(&self) -> &Helper {
        &self.helper
    }
}