File: list_gists_for_token_holder.rs

package info (click to toggle)
rust-octocrab 0.43.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,532 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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(&current_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,
            files = gist.files.into_keys().collect::<Vec<_>>().join(", "),
            description = gist
                .description
                .unwrap_or("<No description>".into())
                .escape_default(),
        );
    }

    Ok(())
}