dnsimple/dnsimple/
identity.rs

1use serde::{Deserialize, Serialize};
2
3use crate::dnsimple::{Client, DNSimpleResponse, Endpoint};
4use crate::errors::DNSimpleError;
5
6/// Represents a User
7#[derive(Debug, Deserialize, Serialize)]
8pub struct User {
9    /// The ID of the user in DNSimple
10    pub id: u64,
11    /// The users email
12    pub email: String,
13    /// When the user was created in DNSimple
14    pub created_at: String,
15    /// When the user was last updated in DNSimple
16    pub updated_at: String,
17}
18
19/// Represents an Account
20#[derive(Debug, Deserialize, Serialize)]
21pub struct Account {
22    /// The account ID in DNSimple
23    pub id: u64,
24    /// The account email
25    pub email: String,
26    /// The identifier of the plan the account is subscribed to
27    pub plan_identifier: String,
28    /// When the account was created in DNSimple
29    pub created_at: String,
30    /// When the account was last updated in DNSimple
31    pub updated_at: String,
32}
33
34/// Represents the structure holding a User and Account structs.
35#[derive(Debug, Deserialize, Serialize)]
36pub struct WhoamiData {
37    /// The account, if present
38    pub account: Option<Account>,
39    /// The user, if present
40    pub user: Option<User>,
41}
42
43struct IdentityEndpoint;
44
45impl Endpoint for IdentityEndpoint {
46    type Output = WhoamiData;
47}
48
49/// The Identity Service handles the identity (whoami) endpoint of the DNSimple API.
50///
51/// See [API Documentation: identity](https://developer.dnsimple.com/v2/identity/)
52pub struct Identity<'a> {
53    pub client: &'a Client,
54}
55
56impl Identity<'_> {
57    /// Retrieves the details about the current authenticated entity used to access the API.
58    ///
59    /// # Examples
60    ///
61    /// ```no_run
62    /// use dnsimple::dnsimple::{Client, new_client};
63    ///
64    /// let client = new_client(true, String::from("AUTH_TOKEN"));
65    /// let response = client.identity().whoami().unwrap().data.unwrap();
66    /// let account = response.account.unwrap();
67    ///
68    /// ```
69    pub fn whoami(&self) -> Result<DNSimpleResponse<WhoamiData>, DNSimpleError> {
70        self.client.get::<IdentityEndpoint>("/whoami", None)
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use crate::dnsimple::identity;
77
78    #[test]
79    fn user_fields() {
80        let user = identity::User {
81            id: 12,
82            email: String::from("testing@dnsimple.com"),
83            created_at: String::from("some_time_ago"),
84            updated_at: String::from("recently"),
85        };
86
87        assert_eq!("testing@dnsimple.com", user.email)
88    }
89
90    #[test]
91    fn account_fields() {
92        let account = identity::Account {
93            id: 14,
94            email: String::from("account@dnsimple.com"),
95            plan_identifier: String::from("testing_plan"),
96            created_at: String::from("some_time_ago"),
97            updated_at: String::from("recently"),
98        };
99
100        assert_eq!("testing_plan", account.plan_identifier)
101    }
102}