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
use std::fmt::Formatter;
use std::str::FromStr;
use std::sync::{Arc, OnceLock};

use http::Method;
use http::Uri;
use hyper::client::HttpConnector;
use hyper_rustls::HttpsConnector;

use crate::middleware::{Middleware, MiddlewareStack};
use crate::RequestBuilder;

static DEFAULT_HTTPS_CONNECTOR: OnceLock<HttpsConnector<HttpConnector>> = OnceLock::new();

fn default_https_connector() -> &'static HttpsConnector<HttpConnector> {
    DEFAULT_HTTPS_CONNECTOR.get_or_init(|| hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build())
}

static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

#[derive(Clone)]
pub struct Client {
    base_url: Option<String>,
    default_headers: Vec<(String, String)>,
    pub(crate) middlewares: MiddlewareStack,
    pub(crate) inner: hyper::Client<HttpsConnector<HttpConnector>, hyper::Body>,
}

/**
what are the options?
1. `ServiceClient` provides a `OauthMiddleware`.
2. We want a way to pass in a partial middlewares list.
3. but the order is funky. we'd want something like
    - recorder
    - oauth
    - retry
    - follow
*/
impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Client {{ base_url: {:?} }}", self.base_url)
    }
}

impl Client {
    #[must_use]
    pub fn new() -> Self {
        let https = default_https_connector().clone();
        Client {
            base_url: None,
            default_headers: vec![("User-Agent".to_string(), APP_USER_AGENT.to_string())],
            middlewares: Vec::new(),
            inner: hyper::Client::builder().build(https),
        }
    }

    /// Set a `base_url` so you can pass relative paths instead of full URLs.
    #[must_use]
    pub fn base_url(mut self, base_url: &str) -> Self {
        self.base_url = Some(base_url.to_string());
        self
    }

    #[must_use]
    pub fn with_middleware<T: Middleware + 'static>(mut self, middleware: T) -> Self {
        self.middlewares.push(Arc::new(middleware));
        self
    }

    #[must_use]
    /// Set a custom TLS connector to use for making requests.
    pub fn with_tls_connector(mut self, connector: HttpsConnector<HttpConnector>) -> Self {
        self.inner = hyper::Client::builder().build(connector);
        self
    }

    #[must_use]
    pub fn no_default_headers(mut self) -> Self {
        self.default_headers = Vec::new();
        self
    }

    #[must_use]
    pub fn default_headers<S: AsRef<str>, I: Iterator<Item = (S, S)>>(mut self, headers: I) -> Self {
        self.default_headers.extend(headers.map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string())));
        self
    }

    #[must_use]
    pub fn default_header<S: AsRef<str>>(mut self, key: S, value: S) -> Self {
        self.default_headers.push((key.as_ref().to_string(), value.as_ref().to_string()));
        self
    }

    #[must_use]
    fn build_uri(&self, uri_or_path: &str) -> Uri {
        if let Ok(uri) = Uri::from_str(uri_or_path) {
            if uri.scheme().is_some() && uri.host().is_some() {
                return uri;
            }
        }
        let uri = self.base_url.as_ref().map_or_else(|| uri_or_path.to_string(), |s| s.clone() + uri_or_path);
        Uri::from_str(&uri).unwrap()
    }

    #[must_use]
    pub fn get(&self, url_or_path: &str) -> RequestBuilder<Client> {
        self.request(Method::GET, url_or_path)
    }

    #[must_use]
    pub fn post(&self, uri_or_path: &str) -> RequestBuilder<Client> {
        self.request(Method::POST, uri_or_path)
    }

    #[must_use]
    pub fn delete(&self, uri_or_path: &str) -> RequestBuilder {
        self.request(Method::DELETE, uri_or_path)
    }

    #[must_use]
    pub fn put(&self, uri_or_path: &str) -> RequestBuilder {
        self.request(Method::PUT, uri_or_path)
    }

    #[must_use]
    pub fn patch(&self, uri_or_path: &str) -> RequestBuilder {
        self.request(Method::PATCH, uri_or_path)
    }

    #[must_use]
    pub fn request(&self, method: Method, uri_or_path: &str) -> RequestBuilder {
        let uri = self.build_uri(uri_or_path);
        RequestBuilder::new(self, method, uri)
            .headers(self.default_headers.iter().map(|(k, v)| (k.as_str(), v.as_str())))
            .set_middlewares(self.middlewares.clone())
    }
}

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

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::middleware::{Recorder, RecorderMode};
    use crate::ResponseExt;

    use super::*;

    #[tokio::test]
    async fn test_make_request() {
        let client = Client::new()
            .base_url("https://www.jsonip.com")
            .no_default_headers()
            .default_headers(vec![("User-Agent", "test-client")].into_iter())
            .with_middleware(Recorder::new().mode(RecorderMode::ForceNoRequests));

        let res = client.get("/").send().await.unwrap().json::<HashMap<String, String>>().await.unwrap();
        let res = serde_json::to_value(res).unwrap();
        assert_eq!(
            res,
            serde_json::json!({"ip":"70.107.97.117","geo-ip":"https://getjsonip.com/#plus","API Help":"https://getjsonip.com/#docs"})
        );
    }
}