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
use failure::Error;
use futures::{
    future::{self, Either},
    Future,
};
use reqwest::r#async::Client;
use serde::Deserialize;

use super::check_status;
use crate::request::Request;

/// Async adapter powered by asynchronous reqwest's [Client](reqwest::async::Client).
pub struct AsyncAdapter {
    client: Client,
}
impl AsyncAdapter {
    /// Creates new async asynchronous with default [Client](reqwest::async::Client).
    pub fn new() -> Self {
        let client = Client::new();

        AsyncAdapter { client }
    }
    /// Send a request.
    pub fn send<R: Request<'static>>(
        &self,
        request: R,
    ) -> impl Future<Item = R::ResponseValue, Error = Error>
    where
        R::ResponseValue: for<'de> Deserialize<'de>,
    {
        let url = match request.build() {
            Ok(url) => url,
            Err(error) => return Either::B(future::err(error)),
        };

        let fut = self
            .client
            .get(url)
            .send()
            .from_err()
            .and_then(|mut response| {
                if let Some(error) = check_status(response.status()) {
                    return Either::B(future::err(error));
                }

                Either::A(response.json().from_err())
            });

        Either::A(fut)
    }
}

impl From<Client> for AsyncAdapter {
    fn from(client: Client) -> Self {
        AsyncAdapter { client }
    }
}

impl Default for AsyncAdapter {
    fn default() -> Self {
        Self::new()
    }
}