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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! Contains [`HttpClient`].

#[cfg(all(target_family = "wasm", feature = "workers"))]
use std::{io::Write, num::NonZeroU16};

use async_trait::async_trait;
#[cfg(all(target_family = "wasm", feature = "workers"))]
use base64::write::EncoderWriter as Base64Encoder;
#[cfg(feature = "native")]
use reqwest::{
    header,
    header::{HeaderMap, HeaderValue},
    RequestBuilder,
};
use serde::{de::DeserializeOwned, Serialize};
#[cfg(feature = "native")]
use tokio::time::Duration;
#[cfg(all(target_family = "wasm", feature = "workers"))]
use worker::{Fetch, Headers, Method, Request, RequestInit};

use github_rest::{
    methods::prelude::{EndPoints, Methods},
    GithubRestError,
};

use crate::github::Authorization;

const USER_AGENT_PARSE_ERROR: &str = "HttpClient: Parsing user agent";
const ACCEPT_HEADER_PARSE_ERROR: &str = "HttpClient: Parsing accept header";

/// An implementer of the [`Requester`] trait. This is all most users will need,
/// however it may be helpful to look at the trait implementation details here
/// if you're writing your own implementation.
///
/// [`Requester`]: github_rest::Requester
pub struct HttpClient {
    #[cfg(feature = "native")]
    client: reqwest::Client,
    #[cfg(all(target_family = "wasm", feature = "workers"))]
    user_agent: Option<String>,
    auth: Option<Authorization>,
}

impl HttpClient {
    /// Creates a new `HttpClient`.
    #[cfg(feature = "native")]
    pub fn new(auth: Option<Authorization>, user_agent: Option<String>) -> Self {
        let mut headers = HeaderMap::new();

        let user_agent = match user_agent {
            Some(s) => s,
            None => "Octocat-rs".to_owned(),
        };

        headers.insert(
            header::USER_AGENT,
            HeaderValue::from_str(user_agent.as_str()).expect(USER_AGENT_PARSE_ERROR),
        );
        headers.insert(
            header::ACCEPT,
            HeaderValue::from_str("application/vnd.github.v3+json").expect(ACCEPT_HEADER_PARSE_ERROR),
        );

        Self {
            client: reqwest::ClientBuilder::new()
                .default_headers(headers)
                .timeout(Duration::from_secs(30))
                .build()
                .unwrap(),
            auth,
        }
    }

    /// Creates a new `HttpClient` with no authorization.
    pub fn new_none() -> Self {
        Self::new(None, None)
    }

    /// Creates a new `HttpClient`.
    #[cfg(all(target_family = "wasm", feature = "workers"))]
    pub fn new(auth: Option<Authorization>, user_agent: Option<String>) -> Self {
        HttpClient { user_agent, auth }
    }

    /// Updates the authorization used by the current client.
    pub fn set_auth(&mut self, auth: Authorization) {
        self.auth = Some(auth);
    }

    /// Set the user agent used by the current client.
    #[cfg(feature = "native")]
    pub fn set_ua(&mut self, user_agent: &str) {
        let mut headers = HeaderMap::new();

        headers.insert(
            header::USER_AGENT,
            HeaderValue::from_str(user_agent).expect(USER_AGENT_PARSE_ERROR),
        );
        headers.insert(
            header::ACCEPT,
            HeaderValue::from_str("application/vnd.github.v3+json").expect(ACCEPT_HEADER_PARSE_ERROR),
        );

        self.client = reqwest::ClientBuilder::new()
            .default_headers(headers)
            .timeout(Duration::from_secs(30))
            .build()
            .unwrap();
    }

    /// Set the user agent used by the current client.
    #[cfg(all(target_family = "wasm", feature = "workers"))]
    pub fn set_ua(&mut self, user_agent: String) {
        self.user_agent = Some(user_agent);
    }

    #[cfg(feature = "native")]
    fn http_auth(&self, req: RequestBuilder) -> RequestBuilder {
        if let Some(auth) = &self.auth {
            match auth {
                Authorization::PersonalToken { username, token } => req.basic_auth(username, Some(token)),
            }
        } else {
            req
        }
    }
}

#[async_trait]
impl github_rest::Requester for HttpClient {
    /// Returns the API response as a [`String`].
    #[cfg(feature = "native")]
    async fn raw_req<T, V>(&self, url: EndPoints, query: Option<&T>, body: Option<V>) -> Result<String, GithubRestError>
    where
        T: Serialize + ?Sized + Send + Sync,
        V: Into<Self::Body> + Send,
    {
        let req = {
            let path = format!("https://api.github.com{}", url.path());

            let mut req = self.http_auth(match url.method() {
                Methods::Get => self.client.get(path),
                Methods::Post => self.client.post(path),
                Methods::Put => self.client.put(path),
                Methods::Patch => self.client.patch(path),
                Methods::Delete => self.client.delete(path),
            });

            if let Some(query) = query {
                req = req.query(query);
            }

            if let Some(body) = body {
                req = req.body(body);
            }
            
            req
        };

        let res = req.send().await?;

        match res.status().as_u16() {
            200..=299 => {}
            401 => {
                return Err(GithubRestError::NotAuthorized(res.text().await?));
            }
            _ => {
                return Err(GithubRestError::ResponseError(res.status(), res.text().await?));
            }
        }

        let txt = res.text().await?;

        Ok(txt)
    }

    /// Returns the API response as a [`String`].
    #[cfg(all(target_family = "wasm", feature = "workers"))]
    async fn raw_req<T, V>(&self, url: EndPoints, query: Option<&T>, body: Option<V>) -> Result<String, GithubRestError>
    where
        T: Serialize + ?Sized + Send + Sync,
        V: Into<Self::Body> + Send,
    {
        let mut path = format!("https://api.github.com{}", url.path());

        if let Some(q) = query {
            path.push_str(serde_urlencoded::to_string(q).expect("Invalid query").as_str());
        }

        futures::executor::block_on(async move {
            let headers = {
                let mut headers = Headers::new();

                headers
                    .append("accept", "application/vnd.github.v3+json")
                    .expect(ACCEPT_HEADER_PARSE_ERROR);

                if let Some(auth) = &self.auth {
                    match auth {
                        Authorization::PersonalToken { username, token } => {
                            let mut header_value = b"Basic ".to_vec();

                            {
                                let mut encoder =
                                    Base64Encoder::new(&mut header_value, &base64::engine::general_purpose::STANDARD);

                                write!(encoder, "{username}:").unwrap();
                                write!(encoder, "{token}").unwrap();
                            }

                            headers
                                .append(
                                    "authorization",
                                    std::str::from_utf8(&header_value).expect("Failed to parse header value"),
                                    )
                                .unwrap();
                        }
                    }
                }

                if let Some(ua) = &self.user_agent {
                    headers.append("user-agent", ua).expect(USER_AGENT_PARSE_ERROR);
                }

                headers
            };

            let init = {
                let mut init = RequestInit::new();
                // I don't want to know. To future generations: I am sorry. 
                init.with_method(BadWrapper::new(url.method()).into());

                init.with_headers(headers);

                if let Some(body) = body {
                    init.with_body(Some(body.into()));
                }

                init
            };

            let req = Request::new_with_init(path.as_str(), &init)?;

            let mut res = Fetch::Request(req).send().await?;

            match res.status_code() {
                200..=299 => {}
                401 => {
                    return Err(GithubRestError::NotAuthorized(res.text().await?));
                }
                _ => {
                    return Err(GithubRestError::ResponseError(
                        NonZeroU16::new(res.status_code()).unwrap(),
                        res.text().await?,
                    ));
                }
            }

            Ok(res.text().await?)
        })
    }

    /// A function for performing HTTP requests utilizing the [`EndPoints`]
    /// enum.
    ///
    /// Usage example:
    ///
    /// ```rust
    /// # use github_rest::{methods::GetCommitsBody, model::commits::Commits, Requester};
    /// # use github_api_octocat::end_points::EndPoints;
    /// # use octocat_rs::HttpClient;
    /// #
    /// HttpClient::new_none()
    ///     .req::<GetCommitsBody, String, Commits>(
    ///         EndPoints::GetReposownerrepoCommits("octocat-rs".to_owned(), "octocat-rs".to_owned()),
    ///             None,
    ///             None,
    ///         )
    ///         .await;
    /// ```
    ///
    ///
    /// [`EndPoints`]: github_api_octocat::end_points::EndPoints
    async fn req<T, V, A: DeserializeOwned>(
        &self,
        url: EndPoints,
        query: Option<&T>,
        body: Option<V>,
    ) -> Result<A, GithubRestError>
    where
        T: Serialize + ?Sized + Send + Sync,
        V: Into<Self::Body> + Send,
    {
        let r = self.raw_req(url, query, body).await?;
        Ok(serde_json::from_str(&r)?)
    }
}

/// I would like to apologize to the world for this crime against nature. 
#[cfg(all(target_family = "wasm", feature = "workers"))]
struct BadWrapper<T> {
    pub(crate) inner: T,
}

#[cfg(all(target_family = "wasm", feature = "workers"))]
impl<T> BadWrapper<T> {
    fn new(inner: T) -> Self {
        BadWrapper { inner }
    }
}

#[cfg(all(target_family = "wasm", feature = "workers"))]
impl From<BadWrapper<Methods>> for Method {
    fn from(v: BadWrapper<Methods>) -> Self {
        match v.inner {
            Methods::Get => Method::Get,
            Methods::Post => Method::Post,
            Methods::Patch => Method::Patch,
            Methods::Delete => Method::Delete,
            Methods::Put => Method::Put,
        }
    }
}