Skip to main content

agentmail/client/
drafts.rs

1use crate::client::NoBody;
2use crate::client::scope::{Drafts, InboxScope, Scoped};
3use crate::{Error, Page, types::*, util::urlish};
4
5impl<S: Drafts> Scoped<'_, S> {
6    /// GET `{scope}/drafts`, one page. Drafts are readable at every scope but
7    /// only created and sent from an [`Client::inbox`](crate::Client::inbox).
8    pub async fn list_drafts(&self, page: Page) -> Result<DraftList, Error> {
9        self.client
10            .request(
11                reqwest::Method::GET,
12                &format!("{}/drafts", self.base()),
13                &page.query(),
14                None::<&NoBody>,
15            )
16            .await
17    }
18
19    /// Every draft, draining pagination.
20    pub async fn list_all_drafts(&self) -> Result<Vec<Draft>, Error> {
21        let mut out = Vec::new();
22        let mut token = None;
23        loop {
24            let resp = self
25                .list_drafts(Page {
26                    limit: None,
27                    page_token: token,
28                })
29                .await?;
30            let next = resp.next_page_token;
31            out.extend(resp.drafts);
32            match next {
33                Some(t) => token = Some(t),
34                None => return Ok(out),
35            }
36        }
37    }
38
39    /// GET `{scope}/drafts/{draft_id}`.
40    pub async fn get_draft(&self, draft_id: &str) -> Result<Draft, Error> {
41        self.client
42            .request(
43                reqwest::Method::GET,
44                &format!("{}/drafts/{}", self.base(), urlish(draft_id)),
45                &[],
46                None::<&NoBody>,
47            )
48            .await
49    }
50
51    /// GET `{scope}/drafts/{draft_id}/attachments/{attachment_id}`.
52    pub async fn get_draft_attachment(
53        &self,
54        draft_id: &str,
55        attachment_id: &str,
56    ) -> Result<Attachment, Error> {
57        self.client
58            .request(
59                reqwest::Method::GET,
60                &format!(
61                    "{}/drafts/{}/attachments/{}",
62                    self.base(),
63                    urlish(draft_id),
64                    urlish(attachment_id),
65                ),
66                &[],
67                None::<&NoBody>,
68            )
69            .await
70    }
71}
72
73impl Scoped<'_, InboxScope<'_>> {
74    /// POST `/v0/inboxes/{inbox_id}/drafts`, create a draft. Supply
75    /// `in_reply_to` to create a reply draft (with `reply_all` to address the
76    /// whole thread) or `forward_of` to create a forward draft.
77    pub async fn create_draft(&self, draft: CreateDraft) -> Result<Draft, Error> {
78        self.client
79            .request(
80                reqwest::Method::POST,
81                &format!("{}/drafts", self.base()),
82                &[],
83                Some(&draft),
84            )
85            .await
86    }
87
88    /// PATCH `/v0/inboxes/{inbox_id}/drafts/{draft_id}`, edit an existing draft.
89    pub async fn update_draft(&self, draft_id: &str, update: UpdateDraft) -> Result<Draft, Error> {
90        self.client
91            .request(
92                reqwest::Method::PATCH,
93                &format!("{}/drafts/{}", self.base(), urlish(draft_id)),
94                &[],
95                Some(&update),
96            )
97            .await
98    }
99
100    /// DELETE `/v0/inboxes/{inbox_id}/drafts/{draft_id}`.
101    pub async fn delete_draft(&self, draft_id: &str) -> Result<(), Error> {
102        self.client
103            .request(
104                reqwest::Method::DELETE,
105                &format!("{}/drafts/{}", self.base(), urlish(draft_id)),
106                &[],
107                None::<&NoBody>,
108            )
109            .await
110    }
111
112    /// POST `/v0/inboxes/{inbox_id}/drafts/{draft_id}/send`, send a draft. The
113    /// draft is deleted after a successful send.
114    pub async fn send_draft(&self, draft_id: &str) -> Result<SentMessage, Error> {
115        self.client
116            .request(
117                reqwest::Method::POST,
118                &format!("{}/drafts/{}/send", self.base(), urlish(draft_id)),
119                &[],
120                None::<&NoBody>,
121            )
122            .await
123    }
124}