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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#[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)";

    fn build_pants() -> Pants {
        dotenv::dotenv().ok();

        Pants::new(
            USER_AGENT,
            &env::var("ACCESS_TOKEN").unwrap(),
            env::var("REFRESH_TOKEN").unwrap(),
            &env::var("CLIENT_ID").unwrap(),
            &env::var("CLIENT_SECRET").unwrap(),
        )
    }

    // Accounts
    #[test]
    fn me() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.me()) {
            Ok(response) => println!("Successfully got response on first invocation: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_karma() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.me_karma()) {
            Ok(response) => println!("Response to me_karma is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_prefs() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.me_prefs()) {
            Ok(response) => println!("Response to me_prefs is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_trophies() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.me_trophies()) {
            Ok(response) => println!("Response to me_trophies is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_friends() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.prefs_friends()) {
            Ok(response) => println!("Response to prefs_friends is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_blocked() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.prefs_blocked()) {
            Ok(response) => println!("Response to prefs_blocked is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_messaging() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.prefs_messaging()) {
            Ok(response) => println!("Response to prefs_messaging is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_trusted() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.prefs_trusted()) {
            Ok(response) => println!("Response to prefs_trusted is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_friends() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.me_friends()) {
            Ok(response) => println!("Response to me_friends is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_blocked() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.me_blocked()) {
            Ok(response) => println!("Response to me_blocked is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    // Listings

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

        let mut pants = build_pants();

        match tokio_test::block_on(pants.trending_subreddits()) {
            Ok(response) => println!("Response to trending_subreddits is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn best() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.best()) {
            Ok(response) => println!("Response to best is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn hot() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.hot()) {
            Ok(response) => println!("Response to hot is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn subreddit_hot() {
        let mut pants = build_pants();

        match tokio_test::block_on(pants.subreddit_hot("testingground4bots")) {
            Ok(response) => println!("Response to hot is: {:#?}", response),
            Err(e) => println!("An error ocurred: {}", e),
        };
    }
}

use reqwest::Client;
use std::collections::HashMap;
mod api_sections;
mod generated_api_sections;
mod shared_models;

use shared_models::models;

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

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

    // ACCOUNT
    pub async fn me(&mut self) -> Result<shared_models::account::MeResponse, reqwest::Error> {
        api_sections::account::wrapper_get_api_v1_me(&self.client, &self.client_configuration, &mut self.refresh_token)
            .await
    }

    pub async fn me_karma(&mut self) -> Result<shared_models::account::MeKarmaResponse, reqwest::Error> {
        api_sections::account::wrapper_get_api_v1_me_karma(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn me_prefs(&mut self) -> Result<shared_models::account::MePrefsResponse, reqwest::Error> {
        api_sections::account::wrapper_get_api_v1_me_prefs(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn me_trophies(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::account::wrapper_get_api_v1_me_trophies(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn prefs_friends(&mut self) -> Result<Vec<shared_models::account::PrefsFriendsResponse>, reqwest::Error> {
        api_sections::account::wrapper_get_prefs_friends(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn prefs_blocked(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::account::wrapper_get_prefs_blocked(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn prefs_messaging(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::account::wrapper_get_prefs_messaging(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn prefs_trusted(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::account::wrapper_get_prefs_trusted(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn me_friends(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::account::wrapper_get_api_v1_me_friends(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn me_blocked(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::account::wrapper_get_api_v1_me_blocked(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    // Listings

    pub async fn trending_subreddits(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::listing::wrapper_get_api_trending_subreddits(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
        )
        .await
    }

    pub async fn best(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::listing::wrapper_get_best(&self.client, &self.client_configuration, &mut self.refresh_token).await
    }

    pub async fn hot(&mut self) -> Result<serde_json::Value, reqwest::Error> {
        api_sections::listing::wrapper_get_hot(&self.client, &self.client_configuration, &mut self.refresh_token).await
    }

    pub async fn subreddit_hot(&mut self, subreddit: &str) -> Result<serde_json::Value, reqwest::Error> {
        let mut parameters = HashMap::new();
        parameters.insert("subreddit".to_string(), subreddit.to_string());
        api_sections::listing::wrapper_get_r_subreddit_hot(
            &self.client,
            &self.client_configuration,
            &mut self.refresh_token,
            &parameters,
        )
        .await
    }

    // OTHER
    pub async fn refresh_access_token(
        &self,
        refresh_token: &str,
    ) -> Result<api_sections::oauth::RefreshToken, reqwest::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,
            refresh_token,
            &self.client_configuration.client_id,
            &self.client_configuration.client_password,
        )
        .await;

        Ok(refresh_token)
    }
}