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::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
17    },
18};
19
20/// Gets a Microsoft Graph contact.
21pub struct MsgraphContactGet {
22    send: MsgraphSend<MsgraphContact>,
23}
24
25impl MsgraphContactGet {
26    /// Gets the contact `id`, `$expand`ing the given navigation clause
27    /// when one is passed.
28    ///
29    /// Graph omits extended properties from responses unless the
30    /// request expands them (e.g. a filtered extended-property
31    /// expansion).
32    pub fn new(
33        auth: &HttpAuthBearer,
34        user_id: &str,
35        id: &str,
36        expand: Option<&str>,
37    ) -> Result<Self, MsgraphSendError> {
38        debug!("prepare microsoft graph contact retrieval");
39        trace!("id: {id:?}");
40        trace!("expand: {expand:?}");
41
42        let user = user_path(user_id);
43        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/contacts/{id}"))?;
44
45        if let Some(expand) = expand {
46            url.query_pairs_mut().append_pair("$expand", expand);
47        }
48
49        let send = MsgraphSend::get(auth, url);
50
51        Ok(Self { send })
52    }
53}
54
55impl MsgraphCoroutine for MsgraphContactGet {
56    type Yield = MsgraphYield;
57    type Return = Result<MsgraphSendOutput<MsgraphContact>, MsgraphSendError>;
58
59    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
60        let out = msgraph_try!(&mut self.send, arg);
61        debug!("contact retrieved");
62        trace!("out: {out:?}");
63        MsgraphCoroutineState::Complete(Ok(out))
64    }
65}