1use anyhow::Error;
2use rand::Rng;
3use serde::{Deserialize, Serialize};
4
5use crate::{http, MAIL_API_URL};
6use crate::http::Client;
7use crate::hydra::{Search, View, HydraCollection};
8
9#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Domain {
12 #[serde(rename = "@id")]
13 pub id: String,
14 #[serde(rename = "@type")]
15 pub type_field: String,
16 #[serde(rename = "@context")]
17 pub context: Option<String>,
18 #[serde(rename = "id")]
19 pub id2: String,
20 pub domain: String,
21 pub is_active: bool,
22 pub is_private: bool,
23 pub created_at: String,
24 pub updated_at: String,
25}
26
27impl HydraCollection<Domain> {
28 pub fn as_list(&self) -> Vec<String> {
29 self.members.iter().map(|domain| domain.domain.to_owned()).collect()
30 }
31}
32
33
34pub(crate) async fn domains() -> Result<HydraCollection<Domain>, Error> {
36 let client = Client::new()?.build()?;
37
38 log::debug!("Getting domains");
39
40 let response = client
41 .get(&format!("{}/domains", MAIL_API_URL))
42 .send()
43 .await?;
44
45 let code = response.status();
46
47 let response = response
48 .text()
49 .await?;
50
51 http::check_response_status(&code, &response).await?;
52
53 log::trace!("Retrieved domains: {}", response);
54 Ok(serde_json::from_str(&response)?)
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[tokio::test]
62 async fn test_domains() -> Result<(), Error> {
63 let domains = domains().await?;
64 assert!(domains.total_items > 0);
65
66 let first = domains.members.first().unwrap().clone();
67
68 let domains = domains.as_list();
69
70 assert!(domains.contains(&first.domain));
71 Ok(())
72 }
73}