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
use std::borrow::Cow;

use async_trait::async_trait;
use http::{header, Method, Request};
use log::debug;
use serde::de::DeserializeOwned;
use crate::types::Root;

use super::{
    ApiError,
    AsyncClient,
    Client, error::BodyError, query::{self, AsyncQuery, Query},
};

pub trait Endpoint {
    fn method(&self) -> Method;
    fn endpoint(&self) -> Cow<'static, str>;
    fn set_query_parameters(&self, url: &mut url::Url) -> Result<(), BodyError> {
        let old_query: Vec<(String, String)> = url.query_pairs().into_owned().collect();
        debug!("old query: {:?}", old_query);
        let query = self.query_parameters()?;
        debug!("new query: {}", query);
        if !query.is_empty() {
            url.set_query(Some(query.as_ref()));
            if !old_query.is_empty() {
                url.query_pairs_mut().extend_pairs(old_query);
            }
        }
        Ok(())
    }
    fn query_parameters(&self) -> Result<Cow<'static, str>, BodyError> {
        Ok(Cow::default())
    }
    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
        Ok(None)
    }

    //NOTE: Move this into a type/trait?
    /// If this endpoint requires a valid API key
    fn requires_authentication(&self) -> bool {
        false
    }
}

impl<E, T, C> Query<T, C> for E
where
    E: Endpoint,
    T: DeserializeOwned,
    C: Client,
{
    fn query(&self, client: &C) -> Result<T, super::ApiError<C::Error>> {
        if self.requires_authentication() && !client.has_api_key() {
            return Err(ApiError::RequiresAuthentication);
        }
        let mut url = client.rest_endpoint(&self.endpoint())?;
        self.set_query_parameters(&mut url)?;

        let req = Request::builder()
            .method(self.method())
            .uri(query::url_to_http_uri(url));
        let (req, data) = if let Some((mime, data)) = self.body()? {
            let req = req.header(header::CONTENT_TYPE, mime);
            (req, data)
        } else {
            (req, Vec::new())
        };
        let rsp = client.rest(req, data)?;
        let status = rsp.status();
        let value = serde_json::from_slice(rsp.body())?;
        if !status.is_success() {
            return Err(ApiError::from_speedrun_api(value));
        }

        serde_json::from_value::<Root<T>>(value)
            .map(|root| root.data)
            .map_err(ApiError::data_type::<T>)
    }
}

#[async_trait]
impl<E, T, C> AsyncQuery<T, C> for E
where
    E: Endpoint + Sync,
    T: DeserializeOwned + 'static,
    C: AsyncClient + Sync,
{
    async fn query_async(&self, client: &C) -> Result<T, ApiError<C::Error>> {
        if self.requires_authentication() && !client.has_api_key() {
            return Err(ApiError::RequiresAuthentication);
        }
        let mut url = client.rest_endpoint(&self.endpoint())?;
        self.set_query_parameters(&mut url)?;

        let req = Request::builder()
            .method(self.method())
            .uri(query::url_to_http_uri(url));
        let (req, data) = if let Some((mime, data)) = self.body()? {
            let req = req.header(header::CONTENT_TYPE, mime);
            (req, data)
        } else {
            (req, Vec::new())
        };

        let rsp = client.rest_async(req, data).await?;
        let status = rsp.status();
        let value = serde_json::from_slice(rsp.body())?;
        if !status.is_success() {
            return Err(ApiError::from_speedrun_api(value));
        }

        serde_json::from_value::<Root<T>>(value)
            .map(|value| value.data)
            .map_err(ApiError::data_type::<T>)
    }
}