Skip to main content

anypost/resources/
domains.rs

1use std::sync::Arc;
2
3use serde::Serialize;
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use crate::resources::{enc, to_value};
8use crate::response::{Page, Response};
9use crate::transport::Method;
10use crate::types::params::ListParams;
11
12/// Operations on the `/domains` endpoints.
13pub struct Domains {
14    http: Arc<HttpClient>,
15}
16
17impl Domains {
18    pub(crate) fn new(http: Arc<HttpClient>) -> Self {
19        Self { http }
20    }
21
22    /// List the team's domains, newest-first. Returns one page.
23    pub async fn list(&self, params: ListParams) -> Result<Page> {
24        self.http
25            .list(
26                "/domains",
27                vec![
28                    ("limit", params.limit.map(|n| n.to_string())),
29                    ("after", params.after),
30                ],
31            )
32            .await
33    }
34
35    /// List every domain, walking all pages.
36    pub async fn list_all(&self, params: ListParams) -> Result<Vec<Response>> {
37        self.http
38            .list_all(
39                "/domains",
40                vec![("limit", params.limit.map(|n| n.to_string()))],
41            )
42            .await
43    }
44
45    /// Add a sending domain. The returned domain is `pending` until verified.
46    pub async fn create(&self, body: impl Serialize) -> Result<Response> {
47        self.http
48            .request_object(Method::Post, "/domains", Some(to_value(body)?), false, None)
49            .await
50    }
51
52    /// Retrieve a single domain by id.
53    pub async fn get(&self, id: &str) -> Result<Response> {
54        self.http
55            .request_object(
56                Method::Get,
57                &format!("/domains/{}", enc(id)),
58                None,
59                false,
60                None,
61            )
62            .await
63    }
64
65    /// Update a domain's tracking configuration. The domain `name` is immutable.
66    pub async fn update(&self, id: &str, body: impl Serialize) -> Result<Response> {
67        self.http
68            .request_object(
69                Method::Patch,
70                &format!("/domains/{}", enc(id)),
71                Some(to_value(body)?),
72                false,
73                None,
74            )
75            .await
76    }
77
78    /// Permanently delete a domain and its DKIM keys.
79    pub async fn delete(&self, id: &str) -> Result<()> {
80        self.http
81            .request_empty(Method::Delete, &format!("/domains/{}", enc(id)))
82            .await
83    }
84
85    /// Trigger a verification check. Always returns the current domain — read
86    /// `status` and `verification_failure`; a still-`pending` domain does not
87    /// raise. Safe to poll while DNS propagates.
88    pub async fn verify(&self, id: &str) -> Result<Response> {
89        self.http
90            .request_object(
91                Method::Post,
92                &format!("/domains/{}/verify", enc(id)),
93                None,
94                false,
95                None,
96            )
97            .await
98    }
99}