Skip to main content

agentmail/client/
lists.rs

1use crate::client::NoBody;
2use crate::client::scope::{Lists, Scoped};
3use crate::{Error, Page, types::*, util::urlish};
4
5impl<S: Lists> Scoped<'_, S> {
6    /// GET `{scope}/lists/{direction}/{kind}`, one page of entries.
7    pub async fn list_entries(
8        &self,
9        direction: ListDirection,
10        kind: ListKind,
11        page: Page,
12    ) -> Result<ListEntries, Error> {
13        self.client
14            .request(
15                reqwest::Method::GET,
16                &format!(
17                    "{}/lists/{}/{}",
18                    self.base(),
19                    direction.as_path(),
20                    kind.as_path()
21                ),
22                &page.query(),
23                None::<&NoBody>,
24            )
25            .await
26    }
27
28    /// Every entry in the list, draining pagination.
29    pub async fn list_all_entries(
30        &self,
31        direction: ListDirection,
32        kind: ListKind,
33    ) -> Result<Vec<ListEntry>, Error> {
34        let mut out = Vec::new();
35        let mut token = None;
36        loop {
37            let resp = self
38                .list_entries(
39                    direction,
40                    kind,
41                    Page {
42                        limit: None,
43                        page_token: token,
44                    },
45                )
46                .await?;
47            let next = resp.next_page_token;
48            out.extend(resp.entries);
49            match next {
50                Some(t) => token = Some(t),
51                None => return Ok(out),
52            }
53        }
54    }
55
56    /// POST `{scope}/lists/{direction}/{kind}`, add an address or domain.
57    pub async fn create_list_entry(
58        &self,
59        direction: ListDirection,
60        kind: ListKind,
61        entry: CreateListEntry,
62    ) -> Result<ListEntry, Error> {
63        self.client
64            .request(
65                reqwest::Method::POST,
66                &format!(
67                    "{}/lists/{}/{}",
68                    self.base(),
69                    direction.as_path(),
70                    kind.as_path()
71                ),
72                &[],
73                Some(&entry),
74            )
75            .await
76    }
77
78    /// GET `{scope}/lists/{direction}/{kind}/{entry}`.
79    pub async fn get_list_entry(
80        &self,
81        direction: ListDirection,
82        kind: ListKind,
83        entry: &str,
84    ) -> Result<ListEntry, Error> {
85        self.client
86            .request(
87                reqwest::Method::GET,
88                &format!(
89                    "{}/lists/{}/{}/{}",
90                    self.base(),
91                    direction.as_path(),
92                    kind.as_path(),
93                    urlish(entry),
94                ),
95                &[],
96                None::<&NoBody>,
97            )
98            .await
99    }
100
101    /// DELETE `{scope}/lists/{direction}/{kind}/{entry}`.
102    pub async fn delete_list_entry(
103        &self,
104        direction: ListDirection,
105        kind: ListKind,
106        entry: &str,
107    ) -> Result<(), Error> {
108        self.client
109            .request(
110                reqwest::Method::DELETE,
111                &format!(
112                    "{}/lists/{}/{}/{}",
113                    self.base(),
114                    direction.as_path(),
115                    kind.as_path(),
116                    urlish(entry),
117                ),
118                &[],
119                None::<&NoBody>,
120            )
121            .await
122    }
123}