Skip to main content

agentmail/client/
domains.rs

1use crate::client::NoBody;
2use crate::client::scope::{Domains, Scoped};
3use crate::{Error, Page, types::*, util::urlish};
4
5impl<S: Domains> Scoped<'_, S> {
6    /// POST `{scope}/domains`, add a sending domain. The response carries the
7    /// DNS [`VerificationRecord`]s to publish.
8    pub async fn create_domain(&self, domain: CreateDomain) -> Result<Domain, Error> {
9        self.client
10            .request(
11                reqwest::Method::POST,
12                &format!("{}/domains", self.base()),
13                &[],
14                Some(&domain),
15            )
16            .await
17    }
18
19    /// GET `{scope}/domains`, one page.
20    pub async fn list_domains(&self, page: Page) -> Result<DomainList, Error> {
21        self.client
22            .request(
23                reqwest::Method::GET,
24                &format!("{}/domains", self.base()),
25                &page.query(),
26                None::<&NoBody>,
27            )
28            .await
29    }
30
31    /// Every domain, draining pagination.
32    pub async fn list_all_domains(&self) -> Result<Vec<Domain>, Error> {
33        let mut out = Vec::new();
34        let mut token = None;
35        loop {
36            let resp = self
37                .list_domains(Page {
38                    limit: None,
39                    page_token: token,
40                })
41                .await?;
42            let next = resp.next_page_token;
43            out.extend(resp.domains);
44            match next {
45                Some(t) => token = Some(t),
46                None => return Ok(out),
47            }
48        }
49    }
50
51    /// GET `{scope}/domains/{domain_id}`.
52    pub async fn get_domain(&self, domain_id: &str) -> Result<Domain, Error> {
53        self.client
54            .request(
55                reqwest::Method::GET,
56                &format!("{}/domains/{}", self.base(), urlish(domain_id)),
57                &[],
58                None::<&NoBody>,
59            )
60            .await
61    }
62
63    /// PATCH `{scope}/domains/{domain_id}`, toggle feedback and subdomain
64    /// settings.
65    pub async fn update_domain(
66        &self,
67        domain_id: &str,
68        update: UpdateDomain,
69    ) -> Result<Domain, Error> {
70        self.client
71            .request(
72                reqwest::Method::PATCH,
73                &format!("{}/domains/{}", self.base(), urlish(domain_id)),
74                &[],
75                Some(&update),
76            )
77            .await
78    }
79
80    /// DELETE `{scope}/domains/{domain_id}`.
81    pub async fn delete_domain(&self, domain_id: &str) -> Result<(), Error> {
82        self.client
83            .request(
84                reqwest::Method::DELETE,
85                &format!("{}/domains/{}", self.base(), urlish(domain_id)),
86                &[],
87                None::<&NoBody>,
88            )
89            .await
90    }
91
92    /// POST `{scope}/domains/{domain_id}/verify`, ask the API to re-check the
93    /// DNS records; poll [`Scoped::get_domain`] for the resulting status.
94    pub async fn verify_domain(&self, domain_id: &str) -> Result<(), Error> {
95        self.client
96            .request(
97                reqwest::Method::POST,
98                &format!("{}/domains/{}/verify", self.base(), urlish(domain_id)),
99                &[],
100                None::<&NoBody>,
101            )
102            .await
103    }
104
105    /// GET `{scope}/domains/{domain_id}/zone-file`, the DNS records as a
106    /// ready-to-import zone file (plain text, not JSON).
107    pub async fn get_domain_zone_file(&self, domain_id: &str) -> Result<String, Error> {
108        self.client
109            .request_text(
110                reqwest::Method::GET,
111                &format!("{}/domains/{}/zone-file", self.base(), urlish(domain_id)),
112            )
113            .await
114    }
115}