Skip to content

Commit

Permalink
gists+examples: Extend GistsHandler by adding star(...)
Browse files Browse the repository at this point in the history
The `star(...)` function takes a `gist_id` and 'stars' it. Has no
observable effect on the gist if it is already starred.
  • Loading branch information
envp committed Apr 9, 2023
1 parent 775936c commit 2ee5c90
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/star_a_gist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 gist_id = if let Some(gist_id) = std::env::args().nth(1) {
gist_id
} else {
eprintln!("error: Need to pass gist id on argv");
std::process::exit(1);
};

let octocrab = Octocrab::builder().personal_token(token).build()?;
octocrab.gists().star(gist_id).await?;
Ok(())
}
24 changes: 24 additions & 0 deletions src/api/gists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ impl<'octo> GistsHandler<'octo> {
pub fn list_commits(&self, gist_id: impl Into<String>) -> list_commits::ListCommitsBuilder {
list_commits::ListCommitsBuilder::new(self, gist_id.into())
}

/// Star the given gist. See [GitHub API Documentation][docs] more
/// information about response data.
///
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let gist = octocrab::instance()
/// .gists()
/// .star("00000000000000000000000000000000")
/// .await?;
/// # Ok(())
/// # }
///
/// [docs]: https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#star-a-gist
/// ```
pub async fn star(&self, gist_id: impl AsRef<str>) -> Result<()> {
let gist_id = gist_id.as_ref();
// PUT returns the HTTP body of the response, map it to `()` on success
// Callers of the library don't care about the HTTP response body
self.crab
._put(format!("/gists/{gist_id}/star"), None::<&()>)
.await
.map(|_| ())
}
}

#[derive(Debug)]
Expand Down

0 comments on commit 2ee5c90

Please sign in to comment.