Skip to content

Commit

Permalink
gists: Move list_user_gists to GistsHandler
Browse files Browse the repository at this point in the history
GitHub keeps this endpoint with other gist related endpoints so we will
do the same for minimizing surprise.
  • Loading branch information
envp committed May 21, 2023
1 parent 1c9e51c commit 22d6acc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 108 deletions.
1 change: 0 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ pub mod ratelimit;
pub mod repos;
pub mod search;
pub mod teams;
pub mod users;
pub mod workflows;
27 changes: 26 additions & 1 deletion src/api/gists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::Serialize;
use std::collections::BTreeMap;

pub use self::list_commits::ListCommitsBuilder;
pub use self::list_gists::{ListAllGistsBuilder, ListPublicGistsBuilder};
pub use self::list_gists::{ListAllGistsBuilder, ListPublicGistsBuilder, ListUserGistsBuilder};

use crate::{
models::gists::{Gist, GistRevision},
Expand Down Expand Up @@ -102,6 +102,31 @@ impl<'octo> GistsHandler<'octo> {
ListPublicGistsBuilder::new(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?;
/// # Ok(())
/// # }
/// ```
///
/// [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>) -> ListUserGistsBuilder<'octo> {
ListUserGistsBuilder::new(self.crab, username.as_ref().to_string())
}

/// Create a new gist.
///
/// ```no_run
Expand Down
56 changes: 56 additions & 0 deletions src/api/gists/list_gists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,59 @@ pub type ListAllGistsBuilder<'octo> = ListGistsBuilder<'octo, AllOrByAuth>;
///
/// Fetches all publicly available gists on the GitHub instance with pagination.
pub type ListPublicGistsBuilder<'octo> = ListGistsBuilder<'octo, PublicOnly>;

/// Handles query data for the `GET /users/{username}/gists` endpoint.
#[derive(Debug, serde::Serialize)]
pub struct ListUserGistsBuilder<'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> ListUserGistsBuilder<'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<crate::Page<Gist>> {
self.crab
.get(
format!("/users/{username}/gists", username = self.username),
Some(&self),
)
.await
}
}
106 changes: 0 additions & 106 deletions src/api/users.rs

This file was deleted.

0 comments on commit 22d6acc

Please sign in to comment.