replicate_rust/
config.rs

1//! The config module contains the Config struct, which is used to initialize configuration for the API.
2//! Currently contains the `API token`, the `user agent` and the `base url`.
3//!
4//!
5//! # Example
6//! ```
7//! use replicate_rust::{Replicate, config::Config};
8//!
9//! let config = Config {
10//!     auth : String::from("REPLICATE_API_TOKEN"),
11//!     ..Default::default()
12//! };
13//!
14//! let replicate = Replicate::new(config); // config OR Default::default()
15//! ```
16//! ### Note
17//!
18//! The Config struct implements the `Default` trait, so you can also use `Default::default()` to initialize the config.
19//!
20//! ```
21//! use replicate_rust::{Replicate, config::Config};
22//!
23//! let config = Config::default();
24//!
25//! let replicate = Replicate::new(config);
26//! ```    
27
28/// The Config struct is used to initialize configuration for the API. Currently contains the `API token`, the `user agent` and the `base url`.
29#[derive(Clone, Debug)]
30pub struct Config {
31    /// The API token to use for authentication.
32    pub auth: String,
33
34    /// The user agent to use for the API requests. Defaults to `replicate-rust/{version}`.
35    pub user_agent: String,
36
37    /// The base url to use for the API requests. Defaults to `https://api.replicate.com/v1`.
38    pub base_url: String,
39}
40
41// Default implementation for Client
42
43impl Default for Config {
44    /// Create a new Config struct with the default values.
45    fn default() -> Self {
46        Self {
47            auth: match std::env::var("REPLICATE_API_TOKEN") {
48                Ok(token) => token,
49                Err(_) => String::new(),
50            },
51            user_agent: format!("replicate-rust/{}", env!("CARGO_PKG_VERSION")),
52            base_url: String::from("https://api.replicate.com/v1"),
53        }
54    }
55}
56
57impl Config {
58    /// Check if auth is set and exit if not.
59    /// The auth token can be set in the environment variable `REPLICATE_API_TOKEN`.
60    /// Otherwise, it can be set in the `Config` struct.
61    pub fn check_auth(&self) {
62        // Check if auth is set.
63        if self.auth.is_empty() {
64            eprintln!("No API token provided. You need to set the REPLICATE_API_TOKEN environment variable or create a client with `Config {{auth: String::from('REPLICATE_API_TOKEN'), ..Default::default()}}`.
65
66You can find your API key on https://replicate.com");
67
68            std::process::exit(1);
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_default() {
79        let config = Config::default();
80
81        assert_eq!(config.auth, String::new());
82        assert_eq!(
83            config.user_agent,
84            format!("replicate-rust/{}", env!("CARGO_PKG_VERSION"))
85        );
86        assert_eq!(config.base_url, "https://api.replicate.com/v1");
87    }
88
89    // Check if auth is set. It is supposed to exit with code 1.
90    #[test]
91    fn test_check_auth() {
92        let config = Config {
93            auth: "Test".to_string(),
94            ..Default::default()
95        };
96        config.check_auth();
97    }
98}