fakeyou_api/
fakeyou.rs

1use crate::FAKEYOU_API_URL;
2use reqwest::blocking::Client;
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Debug, Serialize, Deserialize, Default)]
6pub struct Auth {
7    pub api_key: Option<String>,
8}
9
10impl Auth {
11    pub fn new(api_key: &str) -> Auth {
12        Auth {
13            api_key: Some(api_key.to_string()),
14        }
15    }
16
17    pub fn from_env() -> Result<Self, String> {
18        let api_key =
19            std::env::var("FAKEYOU_API_KEY").map_err(|_| "Missing FAKEYOU_API_KEY".to_string())?;
20        Ok(Self {
21            api_key: Some(api_key),
22        })
23    }
24}
25
26#[derive(Clone, Debug)]
27pub struct FakeYou {
28    pub auth: Auth,
29    pub api_url: String,
30    pub(crate) client: Client,
31}
32
33impl Default for FakeYou {
34    fn default() -> Self {
35        FakeYou::new(Auth::default(), FAKEYOU_API_URL)
36    }
37}
38
39impl FakeYou {
40    pub fn new(auth: Auth, api_url: &str) -> FakeYou {
41        FakeYou {
42            auth,
43            api_url: api_url.to_string(),
44            client: Client::new(),
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn new_test_fakeyou() {
55        let auth = Auth::default();
56        let fakeyou = FakeYou::new(auth, "https://api.fakeyou.com/");
57
58        assert_eq!(fakeyou.api_url, "https://api.fakeyou.com/");
59    }
60}