io_msgraph/v1/rest/users/messages/attachments/
get_raw.rs1use alloc::{format, vec::Vec};
11
12use io_http::{
13 coroutine::{HttpCoroutine, HttpCoroutineState},
14 rfc6750::bearer::HttpAuthBearer,
15 rfc9110::{
16 request::HttpRequest,
17 send::{HttpSendOutput, HttpSendYield},
18 },
19 rfc9112::send::Http11Send,
20};
21use log::{debug, trace};
22use url::Url;
23
24use crate::{
25 coroutine::*,
26 v1::send::{
27 MSGRAPH_API_BASE, MsgraphApiError, MsgraphSendError, MsgraphSendOutput, path_segment,
28 user_path,
29 },
30};
31
32pub struct MsgraphAttachmentGetRaw {
34 send: Http11Send,
35}
36
37impl MsgraphAttachmentGetRaw {
38 pub fn new(
41 auth: &HttpAuthBearer,
42 user_id: &str,
43 message_id: &str,
44 attachment_id: &str,
45 ) -> Result<Self, MsgraphSendError> {
46 debug!("prepare microsoft graph attachment raw retrieval");
47 trace!("message_id: {message_id:?}");
48 trace!("attachment_id: {attachment_id:?}");
49
50 let user = user_path(user_id)?;
51 let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!(
52 "{user}/messages/{}/attachments/{}/$value",
53 path_segment(message_id)?,
54 path_segment(attachment_id)?
55 ))?;
56
57 let request = HttpRequest::get(url.clone())
58 .header("Authorization", auth.to_authorization())
59 .body(Vec::new());
60
61 Ok(Self {
62 send: Http11Send::new(request),
63 })
64 }
65}
66
67impl MsgraphCoroutine for MsgraphAttachmentGetRaw {
68 type Yield = MsgraphYield;
69 type Return = Result<MsgraphSendOutput<Vec<u8>>, MsgraphSendError>;
70
71 fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
72 match self.send.resume(arg) {
73 HttpCoroutineState::Yielded(HttpSendYield::WantsRead) => {
74 MsgraphCoroutineState::Yielded(MsgraphYield::WantsRead)
75 }
76 HttpCoroutineState::Yielded(HttpSendYield::WantsWrite(bytes)) => {
77 MsgraphCoroutineState::Yielded(MsgraphYield::WantsWrite(bytes))
78 }
79 HttpCoroutineState::Yielded(HttpSendYield::WantsRedirect { .. }) => {
80 MsgraphCoroutineState::Complete(Err(MsgraphSendError::UnexpectedRedirect))
81 }
82 HttpCoroutineState::Complete(Err(err)) => {
83 MsgraphCoroutineState::Complete(Err(err.into()))
84 }
85 HttpCoroutineState::Complete(Ok(HttpSendOutput {
86 response,
87 keep_alive,
88 ..
89 })) => {
90 if response.status.is_success() {
91 debug!("attachment raw retrieved");
92 MsgraphCoroutineState::Complete(Ok(MsgraphSendOutput {
93 response: response.body,
94 headers: response.headers,
95 keep_alive,
96 }))
97 } else {
98 let err = MsgraphApiError::of("GET", &response, keep_alive);
99 MsgraphCoroutineState::Complete(Err(err.into()))
100 }
101 }
102 }
103 }
104}