Skip to main content

io_gmail/v1/rest/users/
get_profile.rs

1//! Get a Gmail user profile (`users.getProfile`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users/getProfile>
4
5use alloc::{format, string::String};
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    gmail_try,
15    v1::send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
16};
17
18/// Aggregated mailbox profile of a Gmail user.
19#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
20#[serde(rename_all = "camelCase")]
21pub struct GmailProfile {
22    /// The email address of the user.
23    pub email_address: String,
24    /// The total number of messages in the mailbox.
25    #[serde(default)]
26    pub messages_total: Option<u64>,
27    /// The total number of threads in the mailbox.
28    #[serde(default)]
29    pub threads_total: Option<u64>,
30    /// The id of the current history record of the mailbox.
31    #[serde(default)]
32    pub history_id: Option<String>,
33}
34
35/// I/O-free coroutine getting the Gmail user profile (`users.getProfile`).
36pub struct GmailProfileGet {
37    send: GmailSend<GmailProfile>,
38}
39
40impl GmailProfileGet {
41    /// Builds the `users.getProfile` request for the given user id
42    /// (the mailbox owner, usually `me`).
43    pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, GmailSendError> {
44        debug!("prepare gmail profile retrieval");
45        trace!("user_id: {user_id:?}");
46
47        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/profile"))?;
48        let send = GmailSend::get(auth, url);
49
50        Ok(Self { send })
51    }
52}
53
54impl GmailCoroutine for GmailProfileGet {
55    type Yield = GmailYield;
56    type Return = Result<GmailSendOutput<GmailProfile>, GmailSendError>;
57
58    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
59        let out = gmail_try!(&mut self.send, arg);
60        debug!("profile retrieved");
61        trace!("out: {out:?}");
62        GmailCoroutineState::Complete(Ok(out))
63    }
64}