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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! This crate is used to query the [Reddit API].
//!
//! First, create a [`Config`] struct. Then, use it to create an
//! [`Api`] struct, which exposes several methods for querying
//! the API, including the [`models`] module, which contains structs
//! that map to Reddit objects, simplifying the interaction with the
//! Reddit API.
//!
//! [Reddit API]: https://www.reddit.com/dev/api
//! [`Config`]: struct.Config.html
//! [`Api`]: struct.Api.html
//! [`models`]: models/index.html
//!
//! # Example
//!
//! ```rust,no_run,ignore
//! use redbot::{Api, Config, Value};
//!
//! fn main() {
//!     let config = Config::load_config("config.json").expect("Could not load confiog");
//!     let mut api = Api::new(config);
//!     api.do_login().expect("Could not perform login");
//!
//!     let mut resp = match api.query("GET", "api/v1/me/karma", None, None) {
//!         Ok(resp) => resp,
//!         Err(err) => panic!(err),
//!     };
//!     let karma_breakdown: Value = match resp.json() {
//!         Ok(data) => data,
//!         Err(err) => panic!(err),
//!     };
//!
//!     println!("{:?}", karma_breakdown);
//! }
//! ```

#![warn(missing_docs)]

use log::debug;

use reqwest::Method;
use reqwest::{
    self,
    header::{HeaderMap, HeaderValue, AUTHORIZATION, USER_AGENT},
};
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::{fs::File, io::prelude::*};

pub mod query_listing;
use query_listing::QueryListingRequest;

pub mod errors;
use errors::ApiError;
pub mod models;
use models::{subreddit::Subreddit, user::User};

const RATE_LIMIT_HEADER_NAMES: [&str; 3] = [
    "X-Ratelimit-Used",
    "X-Ratelimit-Remaining",
    "X-Ratelimit-Reset",
];

/// Program configuration - contains the required values
/// to communicate with the Reddit OAuth API for a token.
///
/// The `username` and `password` fields are the same login
/// strings that you'd use to log into the account on the
/// Reddit website. The `user_agent` field is for setting
/// the 'User Agent' header value to use when communicating
/// with the API, as per the [API usage requirements].
/// The `client_id` and `client_secret` fields are for a
/// 'script' type application that you create on the [Reddit
/// website].
///
/// [API usage requirements]: https://github.com/reddit-archive/reddit/wiki/API#rules
/// [Reddit website]: https://www.reddit.com/prefs/apps/
#[derive(Debug, Deserialize, PartialEq, Clone)]
#[cfg_attr(test, derive(Default))]
pub struct Config {
    /// Account username
    pub username: String,
    /// Account password
    pub password: String,
    /// User agent to use
    pub user_agent: String,
    /// App client id
    pub client_id: String,
    /// App client secret
    pub client_secret: String,
}

impl Config {

    /// Attempt to load the configuration from a file.
    ///
    /// # Arguments
    ///
    /// * `path` - relative path to the file
    ///
    /// # Examples
    ///
    /// A file called 'config.json' is has the content:
    ///
    /// ```json
    /// {
    ///   "username": "my-bot-account",
    ///   "password": "hunter2",
    ///   "user_agent": "linux:reddit-rust:v.0.0.1 (bot by /u/my-main-account)",
    ///   "client_id": "foo",
    ///   "client_secret": "bar"
    /// }
    /// ```
    ///
    /// Retrieve the config with:
    ///
    /// ```rust,no_run,ignore
    /// let config = Config::load_config("config.json")?;
    /// ```
    pub fn load_config(path: &str) -> Result<Self, ApiError> {
        let mut file = File::open(path)?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;
        let c = serde_json::from_str::<Config>(&contents)?;
        Ok(c)
    }
}

/// Reddit API access. This is the struct that you'll be using to
/// interact with the API.
pub struct Api {
    config: Config,
    client: reqwest::Client,
    access_token: Option<AccessTokenResponse>,
    /// The account's whoami info
    pub whoami: Option<Value>,
}

impl Api {
    /// Create a new API client.
    ///
    /// # Arguments
    ///
    /// * `config` - the configuration
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let config = Config::load_config().expect("Could not load config");
    /// let mut api = Api::new(config);
    /// ```
    pub fn new(config: Config) -> Self {
        debug!("New API object created");
        Api {
            config,
            client: reqwest::Client::new(),
            access_token: None,
            whoami: None,
        }
    }

    /// Uses the values from the config to get an access token
    /// from the OAuth endpoint, and stores it in the struct.
    ///
    /// This method should be called after creating the struct,
    /// and before attempting to query any inforamtion from the API.
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// if let Err(err) = api.do_login() {
    ///     panic!("Could not get an access token: {}", err);
    /// }
    /// ```
    pub fn do_login(&mut self) -> Result<(), ApiError> {
        // urls
        #[cfg(not(test))]
        let url = "https://www.reddit.com";
        #[cfg(test)]
        let url = &mockito::server_url();

        debug!("Performing login");
        let mut form = HashMap::new();
        form.insert("grant_type", "password");
        form.insert("username", &self.config.username);
        form.insert("password", &self.config.password);
        let mut resp = self
            .client
            .post(&format!("{}/api/v1/access_token", url))
            .header("User-Agent", self.config.user_agent.clone())
            .basic_auth(&self.config.client_id, Some(&self.config.client_secret))
            .form(&form)
            .send()?;
        debug!("Login response code = {}", resp.status().as_str());
        let data = resp.json::<AccessTokenResponse>()?;
        debug!("Access token is {}", data.token);
        self.access_token = Some(data);
        let whoami = self.get_whoami()?;
        debug!("Returned whoami is {:?}", whoami);
        self.whoami = Some(whoami);
        Ok(())
    }

    /// Returns the account's username from the 'api/v1/me' endpoint.
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let whoami = match api.get_whoami() {
    ///     Ok(data) => data,
    ///     Err(err) => panic!(err),
    /// };
    /// ```
    pub fn get_whoami(&self) -> Result<Value, ApiError> {
        let mut resp = self.query("GET", "api/v1/me", None, None)?;
        let data: Value = resp.json()?;
        Ok(data)
    }

    /// Returns the username from the stored whoami data.
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let username = match api.get_username() {
    ///     Ok(data) => data,
    ///     Err(err) => panic!(err),
    /// };
    /// ```
    pub fn get_username(&self) -> Option<String> {
        Some(self.whoami.as_ref()?["name"].as_str().unwrap().to_owned())
    }

    /// Generate headers for the request.
    /// Always includes the User Agent header, and includes
    /// the OAuth token if available.
    fn get_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(
            USER_AGENT,
            HeaderValue::from_str(&self.config.user_agent).unwrap(),
        );
        if self.access_token.is_some() {
            let auth_header = HeaderValue::from_str(&format!(
                "bearer {}",
                self.access_token.as_ref().unwrap().token
            ))
            .unwrap();
            headers.insert(AUTHORIZATION, auth_header);
        }
        headers
    }

    /// Macros and replacements for the URL path and the
    /// appending to the root OAuth API URL.
    fn reformat_path(&self, path: &str) -> String {
        // urls
        #[cfg(not(test))]
        let url = "https://oauth.reddit.com";
        #[cfg(test)]
        let url = &mockito::server_url();

        let path = if path.contains("{username}") {
            debug!("Replacing 'username' macro");
            path.replace("{username}", &self.get_username().unwrap())
        } else {
            path.to_owned()
        };
        format!("{}/{}", url, path)
    }

    /// Processing of the response headers.
    fn process_response_headers(&self, headers: &HeaderMap) {
        for header_name in &RATE_LIMIT_HEADER_NAMES {
            if let Some(value) = headers.get(*header_name) {
                debug!(">> Header {}: {}", header_name, value.to_str().unwrap());
            }
        }
    }

    /// Query the Reddit API.
    ///
    /// # Arguments
    ///
    /// * `method` - A string representing an HTTP method, capable of being parsed by
    /// [reqwest], i.e. "GET", "POST", etc.
    /// * `path` - A relative URL path (everything after reddit.com/)
    /// * `query` - An optional collection of query parameters
    /// * `form_data` - An optional collection of form data to submit
    ///
    /// [reqwest: https://docs.rs/reqwest/latest/reqwest/struct.Method.html#method.from_bytes
    ///
    /// # Examples
    ///
    /// Get data from an endpoint:
    /// ```rust,no_run,ignore
    /// match api.query("GET", "some/endpoint", None, None) {
    ///     Ok(data) => println!("{}", data),
    ///     Err(err) => panic!(err),
    /// };
    /// ```
    ///
    /// Post data to an endpoint:
    ///
    /// ```rust,no_run,ignore
    /// let mut post_data = HashMap::new();
    /// post_data.insert("id", "t3_aaaaaa");
    /// match api.query("POST", "api/save", None, Some(post_data)) {
    ///     Ok(data) => println!("{}", data),
    ///     Err(err) => panic!(err),
    /// }
    /// ```
    pub fn query(
        &self,
        method: &str,
        path: &str,
        query: Option<Vec<(&str, &str)>>,
        form_data: Option<HashMap<&str, &str>>,
    ) -> Result<reqwest::Response, ApiError> {
        let method = Method::from_bytes(method.as_bytes()).unwrap();
        let path = self.reformat_path(path);
        let req = self
            .client
            .request(method, &path)
            .headers(self.get_headers());
        let req = match query {
            Some(q) => req.query(&q),
            None => req,
        };
        debug!("{:?}", req);
        let resp = match form_data {
            Some(fd) => req.form(&fd).send()?,
            None => req.send()?,
        };
        let status = resp.status();
        if status.is_client_error() || status.is_server_error() {
            return Err(ApiError::from(format!("Error code {}", status.as_str(),)));
        }
        self.process_response_headers(&resp.headers());
        Ok(resp)
    }

    /// Query the Reddit API via a listing endpoint.
    ///
    /// Information on listing endpoints can be found in the
    /// [offial docs].
    ///
    /// [offial docs]: https://www.reddit.com/dev/api#listings
    ///
    /// # Arguments
    ///
    /// * `ql` - A [`QueryListingRequest`] struct
    ///
    /// [`QueryListingRequest`]: query_listing/struct.QueryListingRequest.html
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let ql = QueryListingRequest::new("r/rust/hot", 1, 1);
    /// let data: Vec<Value> = api.query_listing(ql).unwrap();
    /// println!("{:?}", data);
    /// ```
    pub fn query_listing(&self, ql: QueryListingRequest) -> Result<Vec<Value>, ApiError> {
        debug!("Listing request call: {:?}", ql);
        let method = Method::GET;
        let path = self.reformat_path(&ql.path);
        let headers = self.get_headers();

        let req = self.client.request(method, &path).headers(headers);
        let mut all_resp: Vec<Value> = Vec::new();
        let mut after = match ql.after {
            Some(a) => a.to_owned(),
            None => String::new(),
        };
        let mut count = ql.count;

        for _ in 0..ql.requests {
            let req = req.try_clone().unwrap();
            let req = if ql.params.is_empty() {
                req.query(ql.params)
            } else {
                req
            };
            let mut listing_parms = vec![("limit", ql.limit.to_string())];
            if !after.is_empty() {
                listing_parms.push(("after", after));
            }
            if count > 0 {
                listing_parms.push(("count", format!("{}", count)));
            }
            if ql.show_all {
                listing_parms.push(("show", "all".to_owned()));
            }
            let req = req.query(&listing_parms);
            let mut resp = req.send()?;
            if resp.status().is_client_error() || resp.status().is_server_error() {
                return Err(ApiError::from(format!(
                    "Error code {}",
                    resp.status().as_str()
                )));
            }
            let data: Value = resp.json()?;
            after = data["data"]["after"].as_str().unwrap().to_owned();
            for item in data["data"]["children"].as_array().unwrap() {
                count += 1;
                all_resp.push(item.clone());
            }
        }
        Ok(all_resp)
    }

    /// Search for subreddits matching the parameter.
    ///
    /// # Arguments
    ///
    /// * `name` - subreddit (partial) name
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let names = match api.search_for_subreddit("rust") {
    ///     Ok(names) => names,
    ///     Err(err) => panic!(err),
    /// }
    /// ```
    pub fn search_for_subreddit(&self, name: &str) -> Result<Vec<Subreddit>, ApiError> {
        let mut resp = self.query(
            "GET",
            "api/search_reddit_names",
            Some(vec![("query", name), ("exact", "false")]),
            None,
        )?;
        let data: Value = resp.json()?;
        Ok(data["names"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .map(|e| Subreddit {
                api: &self,
                name: e.to_owned(),
            })
            .collect::<Vec<Subreddit>>())
    }

    /// Get a subreddit by its name.
    ///
    /// # Arguments
    ///
    /// * `name` - subreddit name
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let subreddit = match api.get_subreddit("rust") {
    ///     Ok(sr) => sr,
    ///     Err(err) => panic!(err),
    /// }
    /// ```
    pub fn get_subreddit(&self, name: &str) -> Result<Subreddit, ApiError> {
        let matching = self.search_for_subreddit(name)?;
        for sr in matching {
            if sr.name == name {
                return Ok(sr);
            }
        }
        Err(ApiError::from(String::from("Subreddit not found")))
    }

    /// Get a user by their name.
    ///
    /// Queries the user's "about" page to verify valid username.
    ///
    /// # Arguments
    ///
    /// * `name` - username
    ///
    /// # Examples
    ///
    /// ```rust,no_run,ignore
    /// let user = match api.get_user("some-username") {
    ///     Ok(u) => u,
    ///     Err(err) => panic!(err),
    /// }
    /// ```
    pub fn get_user(&self, name: &str) -> Result<User, ApiError> {
        let mut resp = self.query("GET", &format!("user/{}/about", name), None, None)?;
        let data: Value = resp.json()?;
        Ok(User {
            api: self,
            about: data,
        })
    }
}

/// the program's API access information.
#[derive(Debug, Deserialize, PartialEq)]
struct AccessTokenResponse {
    #[serde(alias = "access_token")]
    token: String,
    token_type: String,
    expires_in: u64,
    scope: String,
}

#[cfg(test)]
mod tests {
    use super::{AccessTokenResponse, Api, Config, QueryListingRequest};
    use mockito::mock;
    use std::fs::File;
    use std::io::Write;
    use tempfile;

    fn get_config() -> Config {
        std::default::Default::default()
    }

    fn get_api() -> Api {
        let config = get_config();
        Api::new(config)
    }

    fn get_sample_atr() -> String {
        String::from(
            "{\"access_token\":\"aaaaa\",\"token_type\":\"bbbbb\", \
             \"expires_in\":10000,\"scope\":\"ccccc\"}",
        )
    }

    #[test]
    fn load_config_from_disk() {
        let original_content = "{\"username\":\"a\",\"password\":\"b\", \
                                \"user_agent\":\"c\",\"client_id\":\"d\",\"client_secret\":\"e\"}";
        let dir = tempfile::tempdir().unwrap();
        let file_name = "reddit_api-config.json";
        let file_path = dir.path().join(file_name);
        let mut file = File::create(&file_path).unwrap();
        writeln!(file, "{}", original_content).unwrap();

        let config = Config::load_config(&file_path.as_os_str().to_str().unwrap()).unwrap();

        assert_eq!(config.username, "a");
        assert_eq!(config.password, "b");
        assert_eq!(config.user_agent, "c");
        assert_eq!(config.client_id, "d");
        assert_eq!(config.client_secret, "e");
    }

    #[test]
    fn access_token_response_serialize() {
        let atr: AccessTokenResponse = serde_json::from_str(&get_sample_atr()).unwrap();

        assert_eq!(atr.token, String::from("aaaaa"));
        assert_eq!(atr.token_type, String::from("bbbbb"));
        assert_eq!(atr.expires_in, 10000);
        assert_eq!(atr.scope, String::from("ccccc"));
    }

    #[test]
    fn new_api() {
        let config = get_config();
        let api = get_api();

        assert_eq!(api.config, config);
        assert_eq!(api.access_token, None);
        assert_eq!(api.whoami, None);
    }

    #[test]
    fn do_login() {
        let _m1 = mock("POST", "/api/v1/access_token")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(get_sample_atr())
            .create();
        let _m2 = mock("GET", "/api/v1/me")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body("{\"name\":\"test-name\"}")
            .create();

        let mut api = get_api();
        api.do_login().unwrap();
        let username = api.get_username().unwrap();

        assert_eq!(username, "test-name");
        _m1.assert();
        _m2.assert();
    }

    #[test]
    fn query_listing() {
        let body = "{\"data\":{\"kind\":\"Listing\",\"after\":\"t3_ccccc\",\"children\": \
                    [{\"data\":{\"id\":\"aaaaa\"},\"kind\":\"t3\"},{\"data\":{\"id\":\"bbbbb\"}, \
                    \"kind\":\"t3\"},{\"data\":{\"id\":\"ccccc\"},\"kind\":\"t3\"}]}}";
        let _m1 = mock("GET", "/some/endpoint?limit=3&show=all")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(body)
            .create();
        let ql = QueryListingRequest::new("some/endpoint", 3, 1);
        let values = get_api().query_listing(ql).unwrap();

        assert_eq!(values.len(), 3);
        _m1.assert();
    }

    #[test]
    fn search_for_subreddit() {
        let body = "{\"names\":[\"rust1\",\"rust2\",\"rust3\"]}";
        let _m1 = mock("GET", "/api/search_reddit_names?query=rust&exact=false")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(body)
            .create();
        let api = get_api();
        let srs = api.search_for_subreddit("rust").unwrap();

        assert_eq!(srs.len(), 3);
        _m1.assert();
    }

    #[test]
    fn get_subreddit() {
        let body = "{\"names\":[\"rust\",\"rust1\",\"rust2\"]}";
        let _m1 = mock("GET", "/api/search_reddit_names?query=rust1&exact=false")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(body)
            .create();
        let api = get_api();
        let sr = api.get_subreddit("rust1").unwrap();

        assert_eq!(sr.name, "rust1");
        _m1.assert();
    }
}