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

use super::movies::Movies;

pub struct Client {
  pub movies: Movies,
}

impl Client {
  /// Creates a new client with a token
  ///
  /// # Arguments
  ///
  /// # `token` - The token to authenticate to tmdb with
  ///
  /// # Examples
  ///
  /// ```
  /// use tmdb_cli::Client;
  ///
  /// let tmdb = Client::new("TMDB_TOKEN".into());
  /// ```
  pub fn new (token: String) -> Self {
    // default tmdb host
    let host = "https://api.themoviedb.org";
    let movies = Movies::new(host, &token);
    Client {movies}
  }

  /// Creates a new client with a token pulled from the environment
  ///
  /// # Examples
  ///
  /// ```
  /// use tmdb_cli::Client;
  ///
  /// let tmdb = Client::from_env();
  /// ```
  pub fn from_env() -> Self {
    // get the tmdb token from our environment variables
    let token = match env::var("TMDB_TOKEN") {
      Ok(token) => token,
      Err(e) => panic!("Failed to get token from environment with {}", e),
    };
    Self::new(token)
  }
}