Skip to content

Commit

Permalink
users: Introduce namespace for the /users/ route
Browse files Browse the repository at this point in the history
* Create module `api/users.rs`

* Add a handler for `GET /users/{username}/gists`
  This is technically documented under the [gists api][1], but is
  grouped with other functions for handling routes under `/users/`.

[1] https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#list-gists-for-a-user
  • Loading branch information
envp committed May 20, 2023
1 parent ce77cd2 commit bb8245f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pub mod ratelimit;
pub mod repos;
pub mod search;
pub mod teams;
pub mod users;
pub mod workflows;
105 changes: 105 additions & 0 deletions src/api/users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//! The `users` API: `/user`, `/users`
//!
//! Endpoints under this deal with querying public, and private information
//! about specific users.
//!
//! [Official Documentation][docs]
//!
//! [docs]: https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28
use chrono::{DateTime, Utc};

use crate::{models::gists::Gist, Octocrab, Page};

/// Handler for GitHub's users API.
///
/// Created with [`Octocrab::users`]
pub struct UsersHandler<'octo> {
crab: &'octo Octocrab,
}

impl<'octo> UsersHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab) -> Self {
Self { crab }
}

/// List gists for the given username, allowing for pagination.
///
/// See [GitHub API Documentation][docs] for details on `GET /users/{username}/gists`
///
/// # Examples
///
/// * Fetch 10 recent gists for the user with login "foouser":
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// octocrab::instance()
/// .users()
/// .list_user_gists("foouser")
/// .page(1u32)
/// .per_page(10u8)
/// .send()
/// .await?;
/// # }
/// ```
///
/// [docs]: https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#list-gists-for-a-user
pub fn list_user_gists(&self, username: impl AsRef<str>) -> ListGistsForUserBuilder<'octo> {
ListGistsForUserBuilder::new(self.crab, username.as_ref().to_string())
}
}

/// Handles query data for the `GET /users/{username}/gists` endpoint.
#[derive(Debug, serde::Serialize)]
pub struct ListGistsForUserBuilder<'octo> {
#[serde(skip)]
crab: &'octo Octocrab,

#[serde(skip)]
/// Username for which to retrieve gists
username: String,

#[serde(skip_serializing_if = "Option::is_none")]
since: Option<DateTime<Utc>>,

#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,

#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}

impl<'octo> ListGistsForUserBuilder<'octo> {
pub fn new(crab: &'octo Octocrab, username: String) -> Self {
Self {
crab,
username,
since: None,
per_page: None,
page: None,
}
}

pub fn since(mut self, last_updated: DateTime<Utc>) -> Self {
self.since = Some(last_updated);
self
}

pub fn per_page(mut self, count: u8) -> Self {
self.per_page = Some(count);
self
}

pub fn page(mut self, number: u32) -> Self {
self.page = Some(number);
self
}

pub async fn send(self) -> crate::Result<Page<Gist>> {
self.crab
.get(
format!("/users/{username}/gists", username = self.username),
Some(&self),
)
.await
}
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ use models::{AppId, InstallationId, InstallationToken};
pub use self::{
api::{
actions, activity, apps, checks, commits, current, events, gists, gitignore, issues,
licenses, markdown, orgs, pulls, ratelimit, repos, search, teams, workflows,
licenses, markdown, orgs, pulls, ratelimit, repos, search, teams, users, workflows,
},
error::{Error, GitHubError},
from_response::FromResponse,
Expand Down Expand Up @@ -1006,6 +1006,12 @@ impl Octocrab {
pub fn ratelimit(&self) -> ratelimit::RateLimitHandler {
ratelimit::RateLimitHandler::new(self)
}

/// Creates a [`users::UsersHandler`] that allows you to access
/// GitHub's Users API.
pub fn users(&self) -> users::UsersHandler {
users::UsersHandler::new(self)
}
}

/// # GraphQL API.
Expand Down

0 comments on commit bb8245f

Please sign in to comment.