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
#[cfg(test)]
mod tests {
    use super::*;
    use std::env;

    const USER_AGENT: &str =
        "Microsoft Windows 10 Home:ca.technicallyrural.testapp:0.0.1 (by /u/ample_bird)";

    #[test]
    fn it_works() {
        dotenv::dotenv().ok();

        let mut pants = Pants::new(
            USER_AGENT,
            &env::var("ACCESS_TOKEN").unwrap(),
            &env::var("REFRESH_TOKEN").unwrap(),
            &env::var("CLIENT_ID").unwrap(),
            &env::var("CLIENT_SECRET").unwrap(),
        );

        // let refresh_token = tokio_test::block_on(pants.refresh_access_token()).unwrap();
        // println!("{:#?}", refresh_token);

        // pants.client_configuration.refresh_token = refresh_token.access_token;

        match tokio_test::block_on(pants.me()) {
            Ok(_) => println!("Everything was great!"),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }
}

use reqwest::Client;
mod api_sections;
mod generated_api_sections;
mod shared_models;

use shared_models::models;

pub struct Pants {
    client: Client,
    client_configuration: models::ClientConfiguration,
}

impl Pants {
    pub fn new(
        user_agent: &str,
        access_token: &str,
        refresh_token: &str,
        client_id: &str,
        client_password: &str,
    ) -> Pants {
        Pants {
            client: Client::new(),
            client_configuration: models::ClientConfiguration::new(
                user_agent,
                access_token,
                refresh_token,
                client_id,
                client_password,
            ),
        }
    }

    pub async fn me(self) -> Result<(), Box<dyn std::error::Error>> {
        let client = reqwest::Client::builder()
            .user_agent(&(self.client_configuration.user_agent))
            .build()
            .unwrap();

        println!("Built client, going to invoke API");

        let result =
            generated_api_sections::account::api_v1_me(client, self.client_configuration).await?;

        // Ok(result)
        Ok(())
    }

    pub async fn refresh_access_token(
        &self,
    ) -> Result<api_sections::oauth::RefreshToken, Box<dyn std::error::Error>> {
        let client = reqwest::Client::builder()
            .user_agent(&self.client_configuration.user_agent)
            .build()
            .unwrap();

        let refresh_token = api_sections::oauth::refresh_access_token(
            client,
            &self.client_configuration.refresh_token,
            &self.client_configuration.client_id,
            &self.client_configuration.client_password,
        )
        .await?;

        Ok(refresh_token)
    }
}