tower_api_client/
client.rs

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::error::{Error, Result};
use crate::pagination::{PaginatedRequest, PaginationStream};
use crate::request::{Request, RequestData};
use base64::{engine::general_purpose::STANDARD, Engine};
use futures::prelude::*;
use hyper::header::{HeaderMap, HeaderName, HeaderValue};
use hyper::{
    body::{to_bytes, Body},
    client::HttpConnector,
    http::request::Builder,
    Client as HyperClient,
};
use hyper_tls::HttpsConnector;
use log::debug;
use secrecy::Secret;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::pin::Pin;
use std::task::{Context, Poll};
use tower::Service;

#[derive(Clone)]
enum Authorization {
    Bearer(Secret<String>),
    Basic(String, Option<Secret<String>>),
    Query(HashMap<String, Secret<String>>),
    Header(HeaderMap<HeaderValue>),
}

/// The main client used for making requests.
///
/// `Client` stores an async Reqwest client as well as the associated
/// base url and possible authorization details for the REST server.
#[derive(Clone)]
pub struct Client {
    inner: HyperClient<HttpsConnector<HttpConnector>, Body>,
    base_url: String,
    default_headers: HeaderMap<HeaderValue>,
    auth: Option<Authorization>,
}

impl<R: Request + 'static> Service<R> for Client {
    type Response = R::Response;
    type Error = Error;
    type Future = Pin<Box<dyn Send + Future<Output = Result<Self::Response>>>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, request: R) -> Self::Future {
        let this = self.clone();
        Box::pin(async move { this.send(request).await })
    }
}

impl Client {
    /// Create a new `Client`.
    pub fn new<S: ToString>(base_url: S) -> Self {
        let connector = HttpsConnector::new();
        let client = HyperClient::builder().build(connector);

        Self::from_hyper(client, base_url)
    }

    /// Create a new `Client` from an existing Hyper Client.
    pub fn from_hyper<S: ToString>(
        inner: HyperClient<HttpsConnector<HttpConnector>>,
        base_url: S,
    ) -> Self {
        Self {
            inner,
            base_url: base_url.to_string(),
            default_headers: HeaderMap::default(),
            auth: None,
        }
    }

    /// Enable bearer authentication for the client
    pub fn bearer_auth<S: ToString>(mut self, token: S) -> Self {
        self.auth = Some(Authorization::Bearer(Secret::new(token.to_string())));
        self
    }

    /// Enable basic authentication for the client
    pub fn basic_auth<T: Into<Option<S>>, S: ToString>(mut self, user: S, pass: T) -> Self {
        self.auth = Some(Authorization::Basic(
            user.to_string(),
            pass.into().map(|x| Secret::new(x.to_string())),
        ));
        self
    }

    /// Enable query authentication for the client
    pub fn query_auth<S: ToString>(mut self, pairs: Vec<(S, S)>) -> Self {
        let pairs = pairs
            .into_iter()
            .map(|(k, v)| (k.to_string(), Secret::new(v.to_string())))
            .collect();
        self.auth = Some(Authorization::Query(pairs));
        self
    }

    /// Enable custom header authentication for the client
    pub fn header_auth<S: ToString>(mut self, pairs: Vec<(S, S)>) -> Self {
        let mut map = HeaderMap::new();
        for (k, v) in pairs {
            let k = k.to_string();
            let v = v.to_string();
            let mut header_value = HeaderValue::from_str(&v).expect("Failed to create HeaderValue");
            header_value.set_sensitive(true);
            map.insert(
                HeaderName::try_from(&k).expect("Failed to create HeaderName"),
                header_value,
            );
        }
        self.auth = Some(Authorization::Header(map));
        self
    }

    pub fn default_headers(mut self, default_headers: HeaderMap<HeaderValue>) -> Self {
        self.default_headers = default_headers;
        self
    }

    fn send_raw<R>(&self, req: hyper::Request<Body>) -> impl std::future::Future<Output = Result<R>>
    where
        R: for<'de> serde::Deserialize<'de>,
    {
        debug!("Sending request: {:?}", req);
        self.inner
            .request(req)
            .map_err(From::from)
            .and_then(|mut res| async move {
                let status = res.status();
                let body = to_bytes(res.body_mut()).await?;
                if status.is_success() {
                    serde_json::from_slice(&body).map_err(From::from)
                } else if status.is_client_error() {
                    Err(Error::ClientError(status, String::from_utf8(body.into())?))
                } else {
                    Err(Error::ServerError(status, String::from_utf8(body.into())?))
                }
            })
    }

    fn format_request<R: Request>(&self, request: &R) -> Result<hyper::Request<Body>> {
        let endpoint = request.endpoint();
        let endpoint = endpoint.trim_matches('/');
        let url = format!("{}/{}", self.base_url, endpoint);

        let mut headers = self.default_headers.clone();
        headers.extend(request.headers());
        let mut req = Builder::new().uri(&url).method(R::METHOD);
        for header in headers {
            req = req.header(header.0.expect("Always has a header name"), header.1);
        }

        req = {
            use secrecy::ExposeSecret;
            match &self.auth {
                None => req,
                Some(Authorization::Bearer(token)) => {
                    let mut header_value =
                        HeaderValue::from_str(&format!("Bearer {}", token.expose_secret()))
                            .expect("Failed to create HeaderValue");
                    header_value.set_sensitive(true);
                    req.header("authorization", header_value)
                }
                Some(Authorization::Basic(user, pass)) => {
                    let creds = format!(
                        "{}:{}",
                        user,
                        pass.as_ref()
                            .map(|x| x.expose_secret())
                            .unwrap_or(&String::new())
                    );
                    let encoded = STANDARD.encode(creds);
                    let mut header_value = HeaderValue::from_str(&format!("Basic {}", encoded))
                        .expect("Failed to create HeaderValue");
                    header_value.set_sensitive(true);
                    req.header("authorization", header_value)
                }
                Some(Authorization::Query(pairs)) => {
                    let pairs: HashMap<_, _> =
                        pairs.iter().map(|(k, v)| (k, v.expose_secret())).collect();
                    let query = serde_qs::to_string(&pairs)?;
                    let url = if url.contains('?') {
                        format!("{}&{}", url, query)
                    } else {
                        format!("{}?{}", url, query)
                    };
                    req.uri(url)
                }
                Some(Authorization::Header(pairs)) => {
                    for (k, v) in pairs {
                        req = req.header(k, v);
                    }
                    req
                }
            }
        };

        let body = match request.data() {
            RequestData::Empty => Body::empty(),
            RequestData::Form(data) => {
                req = req
                    .header("content-type", "application/x-www-form-urlencoded")
                    .uri(url);
                let body = serde_urlencoded::to_string(data)?;
                Body::from(body)
            }
            RequestData::Json(data) => {
                req = req.header("content-type", "application/json").uri(url);
                let bytes = serde_json::to_vec(&data)?;
                Body::from(bytes)
            }
            RequestData::Query(data) => {
                let query = serde_qs::to_string(data)?;
                let url = if url.contains('?') {
                    format!("{}&{}", url, query)
                } else {
                    format!("{}?{}", url, query)
                };
                req = req.uri(url);
                Body::empty()
            }
        };

        req.body(body).map_err(From::from)
    }

    /// Send a single `Request`
    pub(crate) async fn send<R: Request>(&self, request: R) -> Result<R::Response> {
        let req = self.format_request(&request)?;
        self.send_raw(req).await
    }
}

pub trait ServiceExt<R, T>: Service<R> {
    fn paginate(self, request: R) -> PaginationStream<Self, T, R>
    where
        T: Clone,
        R: Request<Response = <Self as Service<R>>::Response>,
        R: PaginatedRequest<PaginationData = T>,
        Self: Sized,
    {
        PaginationStream::new(self, request)
    }
}

impl<P, T, Request> ServiceExt<Request, P> for T
where
    T: ?Sized + Service<Request>,
    Request: PaginatedRequest<PaginationData = P>,
{
}