Skip to main content

agentmail/client/
threads.rs

1use crate::client::NoBody;
2use crate::{Client, Error, Page, types::*, util::urlish};
3
4impl Client {
5    /// GET /v0/inboxes/{inbox_id}/threads (first page; see
6    /// [`Client::list_threads_page`]).
7    pub async fn list_threads(&self, inbox_id: &str) -> Result<ThreadList, Error> {
8        self.list_threads_page(inbox_id, Page::default()).await
9    }
10
11    /// GET /v0/inboxes/{inbox_id}/threads with pagination. Feed
12    /// [`ThreadList::next_page_token`] back in as [`Page::page_token`] until it
13    /// comes back `None`.
14    pub async fn list_threads_page(&self, inbox_id: &str, page: Page) -> Result<ThreadList, Error> {
15        self.request(
16            reqwest::Method::GET,
17            &format!("/v0/inboxes/{}/threads", urlish(inbox_id)),
18            &page.query(),
19            None::<&NoBody>,
20        )
21        .await
22    }
23
24    /// GET /v0/inboxes/{inbox_id}/threads with filters and pagination. Pass
25    /// [`ThreadListFilters`] with any fields set to narrow results.
26    pub async fn list_threads_filtered(
27        &self,
28        inbox_id: &str,
29        filters: ThreadListFilters,
30    ) -> Result<ThreadList, Error> {
31        self.request(
32            reqwest::Method::GET,
33            &format!("/v0/inboxes/{}/threads", urlish(inbox_id)),
34            &filters.query(),
35            None::<&NoBody>,
36        )
37        .await
38    }
39
40    /// GET /v0/inboxes/{inbox_id}/threads/search, full-text search over the
41    /// inbox's threads. The matched fragments come back in
42    /// [`Thread::highlights`].
43    pub async fn search_threads(&self, inbox_id: &str, query: &str) -> Result<ThreadList, Error> {
44        self.search_threads_page(inbox_id, query, ThreadListFilters::default())
45            .await
46    }
47
48    /// GET /v0/inboxes/{inbox_id}/threads/search with filters and pagination.
49    pub async fn search_threads_page(
50        &self,
51        inbox_id: &str,
52        query: &str,
53        filters: ThreadListFilters,
54    ) -> Result<ThreadList, Error> {
55        let mut q = filters.query();
56        q.push(("q", query.to_string()));
57        self.request(
58            reqwest::Method::GET,
59            &format!("/v0/inboxes/{}/threads/search", urlish(inbox_id)),
60            &q,
61            None::<&NoBody>,
62        )
63        .await
64    }
65
66    /// GET /v0/inboxes/{inbox_id}/threads/{thread_id}, the full thread with its
67    /// messages.
68    pub async fn get_thread(&self, inbox_id: &str, thread_id: &str) -> Result<Thread, Error> {
69        self.request(
70            reqwest::Method::GET,
71            &format!(
72                "/v0/inboxes/{}/threads/{}",
73                urlish(inbox_id),
74                urlish(thread_id),
75            ),
76            &[],
77            None::<&NoBody>,
78        )
79        .await
80    }
81
82    /// PATCH /v0/inboxes/{inbox_id}/threads/{thread_id}, add and/or remove
83    /// labels across the thread. Returns the thread id and its labels after the
84    /// change.
85    pub async fn update_thread(
86        &self,
87        inbox_id: &str,
88        thread_id: &str,
89        update: UpdateThread,
90    ) -> Result<UpdatedThread, Error> {
91        self.request(
92            reqwest::Method::PATCH,
93            &format!(
94                "/v0/inboxes/{}/threads/{}",
95                urlish(inbox_id),
96                urlish(thread_id),
97            ),
98            &[],
99            Some(&update),
100        )
101        .await
102    }
103
104    /// DELETE /v0/inboxes/{inbox_id}/threads/{thread_id}.
105    pub async fn delete_thread(&self, inbox_id: &str, thread_id: &str) -> Result<(), Error> {
106        self.request(
107            reqwest::Method::DELETE,
108            &format!(
109                "/v0/inboxes/{}/threads/{}",
110                urlish(inbox_id),
111                urlish(thread_id),
112            ),
113            &[],
114            None::<&NoBody>,
115        )
116        .await
117    }
118}