nadeo_api_rs/
oauth.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
use std::collections::HashMap;

use serde_json::Value;

use crate::{auth::OAuthToken, client::NadeoApiClient};

pub trait OAuthApiClient: NadeoApiClient {
    /// For adding API requests, use `get_oauth_permit_and_token`.
    async fn get_oauth_token(&self) -> Result<OAuthToken, String>;

    async fn get_oauth_permit_and_token(
        &self,
    ) -> Result<(OAuthToken, tokio::sync::SemaphorePermit), String> {
        let permit = self.rate_limit().await;
        // wait for permit before getting the token in case of refresh
        let token = self.get_oauth_token().await?;
        Ok((token, permit))
    }

    /// Get Display Names for a list of Uuids
    ///
    /// <https://webservices.openplanet.dev/oauth/reference/accounts/id-to-name>
    ///
    /// calls `GET 	https://api.trackmania.com/api/display-names?accountId[]={accountId}`
    async fn get_display_names<T>(
        &self,
        account_ids: &[T],
    ) -> Result<HashMap<String, String>, String>
    where
        T: Into<String> + Clone,
    {
        match account_ids.len() {
            0 => return Ok(HashMap::new()),
            x if x > 50 => return Err("Too many account ids (max 50)".to_string()),
            _ => (),
        };

        let mut account_ids = account_ids
            .iter()
            .cloned()
            .map(Into::into)
            .collect::<Vec<String>>()
            .join("&accountId[]=");
        account_ids.insert_str(0, "accountId[]=");

        let (token, permit) = self.get_oauth_permit_and_token().await?;

        let rb = self
            .oauth_get(&format!("display-names?{}", account_ids), &token, &permit)
            .await;

        let resp = rb.send().await.map_err(|e| e.to_string())?;
        let j: Value = resp.json().await.map_err(|e| e.to_string())?;
        drop(permit);

        // map of WSID -> display name
        let obj = j.as_object().ok_or(format!("Not a json obj: {:?}", &j))?;
        Ok(obj
            .iter()
            .map(|(k, v)| (k.clone(), v.as_str().unwrap_or(k).to_string()))
            .collect())
    }

    // todo: <https://webservices.openplanet.dev/oauth/reference/accounts/name-to-id>	https://api.trackmania.com/api/display-names/account-ids?displayName[]={accountName}
}

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

    use super::*;
    use crate::{
        auth::{NadeoClient, OAuthCredentials},
        prelude::UserAgentDetails,
        test_helpers::{get_test_creds, get_test_email},
        user_agent_auto,
    };

    // works
    #[ignore]
    #[tokio::test]
    async fn test_get_display_names() {
        let creds = get_test_creds();
        let client = NadeoClient::create(creds, user_agent_auto!(&get_test_email()), 10)
            .await
            .unwrap();
        let client_id =
            env::var("NADEO_API_TEST_OAUTH_ID").expect("NADEO_API_TEST_OAUTH_ID not set");
        let client_secret =
            env::var("NADEO_API_TEST_OAUTH_SECRET").expect("NADEO_API_TEST_OAUTH_SECRET not set");
        let client = client
            .with_oauth(OAuthCredentials::new(&client_id, &client_secret))
            .unwrap();
        let names_hm = client
            .get_display_names(&vec![
                "5b4d42f4-c2de-407d-b367-cbff3fe817bc",
                "0a2d1bc0-4aaa-4374-b2db-3d561bdab1c9",
            ])
            .await
            .unwrap();
        println!("{:?}", names_hm);
        assert_eq!(names_hm.len(), 2);
        assert_eq!(
            names_hm.get("5b4d42f4-c2de-407d-b367-cbff3fe817bc"),
            Some(&"tooInfinite".to_string())
        );
        assert_eq!(
            names_hm.get("0a2d1bc0-4aaa-4374-b2db-3d561bdab1c9"),
            Some(&"XertroV".to_string())
        );
    }
}