-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
current: Add
CurrentAuthHandler::list_gists_for_authenticated_user
(#…
…328) * current: Add function `list_gists_for_authenticated_user` Add a new function to `CurrentAuthHandler` that allows paginating through the authenticated user's gists. * gists: bugfix: `content`, `truncated` are optional The aren't always sent as a part of the response, per the [schema][1]. [1]: https://docs.github.com/en/rest/gists/gists?apiVersion=latest#list-gists-for-the-authenticated-user * examples: Add example 'list_gists_for_token_holder' This example demonstrates using the gists API to paginate through the gists of the authenticated user and printing them in a tabular form.
- Loading branch information
Showing
3 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use octocrab::Octocrab; | ||
|
||
#[tokio::main] | ||
async fn main() -> octocrab::Result<()> { | ||
let token = std::env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN env variable is required"); | ||
|
||
let octocrab = Octocrab::builder().personal_token(token).build()?; | ||
let current_user_name = octocrab.current().user().await?.login; | ||
let mut current_gist_page = octocrab | ||
.current() | ||
.list_gists_for_authenticated_user() | ||
.per_page(1) | ||
.send() | ||
.await?; | ||
|
||
let mut gists = current_gist_page.take_items(); | ||
while let Ok(Some(mut new_page)) = octocrab.get_page(¤t_gist_page.next).await { | ||
gists.extend(new_page.take_items()); | ||
current_gist_page = new_page; | ||
} | ||
|
||
println!( | ||
"User '{username}' has {count} gists:", | ||
username = current_user_name, | ||
count = gists.len() | ||
); | ||
println!("id | url | [files...] | description"); | ||
for gist in gists { | ||
println!( | ||
"{id} | {url} | [{files}] | {description}", | ||
id = gist.id, | ||
url = gist.html_url.to_string(), | ||
files = gist.files.into_keys().collect::<Vec<_>>().join(", "), | ||
description = gist | ||
.description | ||
.unwrap_or("<No description>".into()) | ||
.escape_default(), | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters