agentmail/client/
attachments.rs1use crate::client::NoBody;
2use crate::{Client, Error, types::*, util::urlish};
3
4impl Client {
5 pub async fn get_message_attachment(
9 &self,
10 inbox_id: &str,
11 message_id: &str,
12 attachment_id: &str,
13 ) -> Result<Attachment, Error> {
14 self.request(
15 reqwest::Method::GET,
16 &format!(
17 "/v0/inboxes/{}/messages/{}/attachments/{}",
18 urlish(inbox_id),
19 urlish(message_id),
20 urlish(attachment_id),
21 ),
22 &[],
23 None::<&NoBody>,
24 )
25 .await
26 }
27
28 pub async fn get_thread_attachment(
30 &self,
31 inbox_id: &str,
32 thread_id: &str,
33 attachment_id: &str,
34 ) -> Result<Attachment, Error> {
35 self.request(
36 reqwest::Method::GET,
37 &format!(
38 "/v0/inboxes/{}/threads/{}/attachments/{}",
39 urlish(inbox_id),
40 urlish(thread_id),
41 urlish(attachment_id),
42 ),
43 &[],
44 None::<&NoBody>,
45 )
46 .await
47 }
48
49 pub async fn get_draft_attachment(
51 &self,
52 inbox_id: &str,
53 draft_id: &str,
54 attachment_id: &str,
55 ) -> Result<Attachment, Error> {
56 self.request(
57 reqwest::Method::GET,
58 &format!(
59 "/v0/inboxes/{}/drafts/{}/attachments/{}",
60 urlish(inbox_id),
61 urlish(draft_id),
62 urlish(attachment_id),
63 ),
64 &[],
65 None::<&NoBody>,
66 )
67 .await
68 }
69
70 pub async fn download_attachment(&self, attachment: &Attachment) -> Result<Vec<u8>, Error> {
76 let url = attachment
77 .download_url
78 .as_deref()
79 .ok_or(Error::NoDownloadUrl)?;
80 let resp = self.http.get(url).send().await?;
82 let status = resp.status();
83 let bytes = resp.bytes().await?;
84 if !status.is_success() {
85 return Err(Error::Api {
86 status,
87 body: String::from_utf8_lossy(&bytes).into_owned(),
88 });
89 }
90 Ok(bytes.to_vec())
91 }
92}