tmdb_cli/libs/handlers/
client.rs

1use std::env;
2
3use super::movies::Movies;
4
5pub struct Client {
6  pub movies: Movies,
7}
8
9impl Client {
10  /// Creates a new client with a token
11  ///
12  /// # Arguments
13  ///
14  /// # `token` - The token to authenticate to tmdb with
15  ///
16  /// # Examples
17  ///
18  /// ```
19  /// use tmdb_cli::Client;
20  ///
21  /// let tmdb = Client::new("TMDB_TOKEN".into());
22  /// ```
23  pub fn new (token: String) -> Self {
24    // default tmdb host
25    let host = "https://api.themoviedb.org";
26    let movies = Movies::new(host, &token);
27    Client {movies}
28  }
29
30  /// Creates a new client with a token pulled from the environment
31  ///
32  /// # Examples
33  ///
34  /// ```
35  /// use tmdb_cli::Client;
36  ///
37  /// let tmdb = Client::from_env();
38  /// ```
39  pub fn from_env() -> Self {
40    // get the tmdb token from our environment variables
41    let token = match env::var("TMDB_TOKEN") {
42      Ok(token) => token,
43      Err(e) => panic!("Failed to get token from environment with {}", e),
44    };
45    Self::new(token)
46  }
47}