Skip to main content

agentmail/client/
domains.rs

1use crate::client::NoBody;
2use crate::{Client, Error, Page, types::*, util::urlish};
3
4impl Client {
5    /// POST /v0/domains, add a sending domain. The response carries the DNS
6    /// [`VerificationRecord`]s to publish.
7    pub async fn create_domain(&self, domain: CreateDomain) -> Result<Domain, Error> {
8        self.request(reqwest::Method::POST, "/v0/domains", &[], Some(&domain))
9            .await
10    }
11
12    /// GET /v0/domains (first page; see [`Client::list_domains_page`]).
13    pub async fn list_domains(&self) -> Result<DomainList, Error> {
14        self.list_domains_page(Page::default()).await
15    }
16
17    /// GET /v0/domains with pagination. Feed [`DomainList::next_page_token`]
18    /// back in as [`Page::page_token`] until it comes back `None`.
19    pub async fn list_domains_page(&self, page: Page) -> Result<DomainList, Error> {
20        self.request(
21            reqwest::Method::GET,
22            "/v0/domains",
23            &page.query(),
24            None::<&NoBody>,
25        )
26        .await
27    }
28
29    /// GET /v0/domains/{domain_id}
30    pub async fn get_domain(&self, domain_id: &str) -> Result<Domain, Error> {
31        self.request(
32            reqwest::Method::GET,
33            &format!("/v0/domains/{}", urlish(domain_id)),
34            &[],
35            None::<&NoBody>,
36        )
37        .await
38    }
39
40    /// PATCH /v0/domains/{domain_id}, toggle feedback and subdomain settings.
41    pub async fn update_domain(
42        &self,
43        domain_id: &str,
44        update: UpdateDomain,
45    ) -> Result<Domain, Error> {
46        self.request(
47            reqwest::Method::PATCH,
48            &format!("/v0/domains/{}", urlish(domain_id)),
49            &[],
50            Some(&update),
51        )
52        .await
53    }
54
55    /// DELETE /v0/domains/{domain_id}
56    pub async fn delete_domain(&self, domain_id: &str) -> Result<(), Error> {
57        self.request(
58            reqwest::Method::DELETE,
59            &format!("/v0/domains/{}", urlish(domain_id)),
60            &[],
61            None::<&NoBody>,
62        )
63        .await
64    }
65
66    /// POST /v0/domains/{domain_id}/verify, ask the API to re-check the DNS
67    /// records. Returns once the check is queued; poll [`Client::get_domain`]
68    /// for the resulting status.
69    pub async fn verify_domain(&self, domain_id: &str) -> Result<(), Error> {
70        self.request(
71            reqwest::Method::POST,
72            &format!("/v0/domains/{}/verify", urlish(domain_id)),
73            &[],
74            None::<&NoBody>,
75        )
76        .await
77    }
78
79    /// GET /v0/domains/{domain_id}/zone-file, the DNS records as a ready-to-import
80    /// zone file (plain text, not JSON).
81    pub async fn get_domain_zone_file(&self, domain_id: &str) -> Result<String, Error> {
82        self.request_text(
83            reqwest::Method::GET,
84            &format!("/v0/domains/{}/zone-file", urlish(domain_id)),
85        )
86        .await
87    }
88}