#[derive(Clone, Debug)]
pub struct Config {
pub auth: String,
pub user_agent: String,
pub base_url: String,
}
impl Default for Config {
fn default() -> Self {
Self {
auth: match std::env::var("REPLICATE_API_TOKEN") {
Ok(token) => token,
Err(_) => String::new(),
},
user_agent: format!("replicate-rust/{}", env!("CARGO_PKG_VERSION")),
base_url: String::from("https://api.replicate.com/v1"),
}
}
}
impl Config {
pub fn check_auth(&self) {
if self.auth.is_empty() {
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()}}`.
You can find your API key on https://replicate.com");
std::process::exit(1);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let config = Config::default();
assert_eq!(config.auth, String::new());
assert_eq!(
config.user_agent,
format!("replicate-rust/{}", env!("CARGO_PKG_VERSION"))
);
assert_eq!(config.base_url, "https://api.replicate.com/v1");
}
#[test]
fn test_check_auth() {
let config = Config {
auth: "Test".to_string(),
..Default::default()
};
config.check_auth();
}
}