Skip to main content

io_msgraph/v1/rest/users/messages/
get_raw.rs

1//! Get the raw MIME content of a Microsoft Graph message
2//! (`GET /me/messages/{id}/$value`).
3//!
4//! Unlike the other coroutines, the `$value` endpoint returns the raw
5//! RFC 5322 message rather than JSON, so this runs the HTTP send
6//! directly and yields the body bytes.
7//!
8//! <https://learn.microsoft.com/en-us/graph/outlook-get-mime-message>
9
10use 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
32/// Gets the raw RFC 5322 MIME content of a Microsoft Graph message.
33pub struct MsgraphMessageGetRaw {
34    send: Http11Send,
35}
36
37impl MsgraphMessageGetRaw {
38    /// Gets the raw MIME content of the message `id`.
39    pub fn new(auth: &HttpAuthBearer, user_id: &str, id: &str) -> Result<Self, MsgraphSendError> {
40        debug!("prepare microsoft graph message raw retrieval");
41        trace!("id: {id:?}");
42
43        let user = user_path(user_id)?;
44        let url = Url::parse(MSGRAPH_API_BASE)?
45            .join(&format!("{user}/messages/{}/$value", path_segment(id)?))?;
46
47        let request = HttpRequest::get(url.clone())
48            .header("Authorization", auth.to_authorization())
49            .body(Vec::new());
50
51        Ok(Self {
52            send: Http11Send::new(request),
53        })
54    }
55}
56
57impl MsgraphCoroutine for MsgraphMessageGetRaw {
58    type Yield = MsgraphYield;
59    type Return = Result<MsgraphSendOutput<Vec<u8>>, MsgraphSendError>;
60
61    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
62        match self.send.resume(arg) {
63            HttpCoroutineState::Yielded(HttpSendYield::WantsRead) => {
64                MsgraphCoroutineState::Yielded(MsgraphYield::WantsRead)
65            }
66            HttpCoroutineState::Yielded(HttpSendYield::WantsWrite(bytes)) => {
67                MsgraphCoroutineState::Yielded(MsgraphYield::WantsWrite(bytes))
68            }
69            HttpCoroutineState::Yielded(HttpSendYield::WantsRedirect { .. }) => {
70                MsgraphCoroutineState::Complete(Err(MsgraphSendError::UnexpectedRedirect))
71            }
72            HttpCoroutineState::Complete(Err(err)) => {
73                MsgraphCoroutineState::Complete(Err(err.into()))
74            }
75            HttpCoroutineState::Complete(Ok(HttpSendOutput {
76                response,
77                keep_alive,
78                ..
79            })) => {
80                if response.status.is_success() {
81                    debug!("message raw retrieved");
82                    MsgraphCoroutineState::Complete(Ok(MsgraphSendOutput {
83                        response: response.body,
84                        headers: response.headers,
85                        keep_alive,
86                    }))
87                } else {
88                    let err = MsgraphApiError::of("GET", &response, keep_alive);
89                    MsgraphCoroutineState::Complete(Err(err.into()))
90                }
91            }
92        }
93    }
94}