Skip to main content

io_msgraph/v1/rest/users/contacts/
get.rs

1//! Get a Microsoft Graph contact (`GET /me/contacts/{id}`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/contact-get>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use url::Url;
10
11use crate::{
12    coroutine::*,
13    msgraph_try,
14    v1::{
15        rest::users::contacts::MsgraphContact,
16        send::{
17            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
18            user_path,
19        },
20    },
21};
22
23/// Gets a Microsoft Graph contact.
24pub struct MsgraphContactGet {
25    send: MsgraphSend<MsgraphContact>,
26}
27
28impl MsgraphContactGet {
29    /// Gets the contact `id`, `$expand`ing the given navigation clause
30    /// when one is passed.
31    ///
32    /// Graph omits extended properties from responses unless the
33    /// request expands them (e.g. a filtered extended-property
34    /// expansion).
35    pub fn new(
36        auth: &HttpAuthBearer,
37        user_id: &str,
38        id: &str,
39        expand: Option<&str>,
40    ) -> Result<Self, MsgraphSendError> {
41        debug!("prepare microsoft graph contact retrieval");
42        trace!("id: {id:?}");
43        trace!("expand: {expand:?}");
44
45        let user = user_path(user_id)?;
46        let mut url = Url::parse(MSGRAPH_API_BASE)?
47            .join(&format!("{user}/contacts/{}", path_segment(id)?))?;
48
49        if let Some(expand) = expand {
50            url.query_pairs_mut().append_pair("$expand", expand);
51        }
52
53        let send = MsgraphSend::get(auth, url);
54
55        Ok(Self { send })
56    }
57}
58
59impl MsgraphCoroutine for MsgraphContactGet {
60    type Yield = MsgraphYield;
61    type Return = Result<MsgraphSendOutput<MsgraphContact>, MsgraphSendError>;
62
63    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
64        let out = msgraph_try!(&mut self.send, arg);
65        debug!("contact retrieved");
66        trace!("out: {out:?}");
67        MsgraphCoroutineState::Complete(Ok(out))
68    }
69}