io_msgraph/v1/rest/users/messages/
create_mime.rs1use alloc::{format, vec::Vec};
9
10use base64_codec::codec::Base64Codec;
11use io_http::rfc6750::bearer::HttpAuthBearer;
12use log::{debug, trace};
13use url::Url;
14
15use crate::{
16 coroutine::*,
17 msgraph_try,
18 v1::{
19 rest::users::messages::MsgraphMessage,
20 send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
21 },
22};
23
24pub struct MsgraphMessageCreateMime {
26 send: MsgraphSend<MsgraphMessage>,
27}
28
29impl MsgraphMessageCreateMime {
30 pub fn new(
33 auth: &HttpAuthBearer,
34 user_id: &str,
35 folder: Option<&str>,
36 raw: &[u8],
37 ) -> Result<Self, MsgraphSendError> {
38 debug!("prepare microsoft graph message creation (mime)");
39 trace!("folder: {folder:?}");
40 trace!("raw len: {}", raw.len());
41
42 let user = user_path(user_id);
43 let path = match folder {
44 Some(folder) => format!("{user}/mailFolders/{folder}/messages"),
45 None => format!("{user}/messages"),
46 };
47 let url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
48 let mut body = Vec::new();
49 Base64Codec::default().encode_vec(raw, &mut body);
50
51 let send = MsgraphSend::post_text(auth, url, body);
52
53 Ok(Self { send })
54 }
55}
56
57impl MsgraphCoroutine for MsgraphMessageCreateMime {
58 type Yield = MsgraphYield;
59 type Return = Result<MsgraphSendOutput<MsgraphMessage>, MsgraphSendError>;
60
61 fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
62 let out = msgraph_try!(&mut self.send, arg);
63 debug!("message created (mime)");
64 trace!("out: {out:?}");
65 MsgraphCoroutineState::Complete(Ok(out))
66 }
67}