agentmail/client/
drafts.rs1use crate::client::NoBody;
2use crate::client::scope::{Drafts, InboxScope, Scoped};
3use crate::{Error, Page, types::*, util::urlish};
4
5impl<S: Drafts> Scoped<'_, S> {
6 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 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 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 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 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 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 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 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}