github_stats/
user.rs

1//! For getting user information.
2
3use serde::Deserialize;
4
5use crate::Result;
6
7/// Represents that stats of a [Github] user.
8///
9/// [Github]: https://github.com/
10#[derive(Debug, Deserialize)]
11pub struct User {
12    login: String,
13    id: u64,
14    node_id: String,
15    avatar_url: String,
16    gravatar_id: String,
17    html_url: String,
18    url: String,
19    followers_url: String,
20    following_url: String,
21    gists_url: String,
22    starred_url: String,
23    subscriptions_url: String,
24    organizations_url: String,
25    repos_url: String,
26    events_url: String,
27    received_events_url: String,
28    r#type: String,
29    site_admin: bool,
30}
31
32impl User {
33    /// Creates a new `User`
34    ///
35    /// # Example
36    ///
37    /// ```no_run
38    /// use github_stats::User;
39    ///
40    /// let user = User::new("rust-lang", "<my user agent>");
41    /// ```
42    pub async fn new(user: &str, user_agent: &str) -> Result<Self> {
43        const URL: &str = "https://api.github.com/users";
44        let url = format!("{}/{}", URL, user);
45        let user: User = reqwest::Client::builder()
46            .user_agent(user_agent)
47            .build()?
48            .get(&url)
49            .send()
50            .await?
51            .json()
52            .await?;
53
54        Ok(user)
55    }
56    pub fn login(&self) -> &str {
57        &self.login
58    }
59    pub fn id(&self) -> u64 {
60        self.id
61    }
62    pub fn node_id(&self) -> &str {
63        &self.node_id
64    }
65    pub fn avatar_url(&self) -> &str {
66        &self.avatar_url
67    }
68    pub fn gravatar_id(&self) -> &str {
69        &self.gravatar_id
70    }
71    /// Actual link to the user's page.
72    pub fn html_url(&self) -> &str {
73        &self.html_url
74    }
75    pub fn url(&self) -> &str {
76        &self.url
77    }
78    pub fn followers_url(&self) -> &str {
79        &self.followers_url
80    }
81    pub fn following_url(&self) -> &str {
82        &self.following_url
83    }
84    pub fn gists_url(&self) -> &str {
85        &self.gists_url
86    }
87    pub fn starred_url(&self) -> &str {
88        &self.starred_url
89    }
90    pub fn subscriptions_url(&self) -> &str {
91        &self.subscriptions_url
92    }
93    pub fn organizations_url(&self) -> &str {
94        &self.organizations_url
95    }
96    pub fn repos_url(&self) -> &str {
97        &self.repos_url
98    }
99    pub fn events_url(&self) -> &str {
100        &self.events_url
101    }
102    pub fn received_events_url(&self) -> &str {
103        &self.received_events_url
104    }
105    /// *Use `r#type` to avoid conflict with `type` keyword.*
106    pub fn r#type(&self) -> &str {
107        &self.r#type
108    }
109    pub fn site_admin(&self) -> bool {
110        self.site_admin
111    }
112}