Skip to main content

io_msgraph/v1/rest/users/
get.rs

1//! Get a Microsoft Graph user (`GET /me`, `GET /users/{id}`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/user-get>
4
5use io_http::rfc6750::bearer::HttpAuthBearer;
6use log::{debug, trace};
7use url::Url;
8
9use crate::{
10    coroutine::*,
11    msgraph_try,
12    v1::{
13        rest::users::MsgraphUser,
14        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
15    },
16};
17
18/// Gets a Microsoft Graph user profile.
19pub struct MsgraphUserGet {
20    send: MsgraphSend<MsgraphUser>,
21}
22
23impl MsgraphUserGet {
24    /// Gets the profile of `user_id` (`me`, a user id or a principal
25    /// name).
26    pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, MsgraphSendError> {
27        debug!("prepare microsoft graph user retrieval");
28        trace!("user_id: {user_id:?}");
29
30        let url = Url::parse(MSGRAPH_API_BASE)?.join(&user_path(user_id))?;
31        let send = MsgraphSend::get(auth, url);
32
33        Ok(Self { send })
34    }
35}
36
37impl MsgraphCoroutine for MsgraphUserGet {
38    type Yield = MsgraphYield;
39    type Return = Result<MsgraphSendOutput<MsgraphUser>, MsgraphSendError>;
40
41    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
42        let out = msgraph_try!(&mut self.send, arg);
43        debug!("user retrieved");
44        trace!("out: {out:?}");
45        MsgraphCoroutineState::Complete(Ok(out))
46    }
47}