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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! The gist API
use snafu::ResultExt;
use http::StatusCode;
use crate::Octocrab;
use crate::params::gists::File;
/// Handler for GitHub's gist API.
///
/// Created with [`Octocrab::gists`].
pub struct GistHandler<'octo> {
crab: &'octo Octocrab,
}
impl<'octo> GistHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab) -> Self {
Self { crab }
}
/// Check if a gist has been starred by the currently authenticated user.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// assert!(octocrab::instance().gists().check_is_starred("id").await?);
/// # Ok(())
/// # }
/// ```
pub async fn check_is_starred(&self, id: &str) -> crate::Result<bool> {
let url = self.crab.absolute_url(format!("/gists/{}/star", id))?;
let resp = self.crab._get(url, None::<&()>).await?;
match resp.status() {
StatusCode::NO_CONTENT => Ok(true),
StatusCode::NOT_FOUND => Ok(false),
_ => {
crate::map_github_error(resp).await?;
unreachable!()
}
}
}
/// Create a new gist.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// use octocrab::params::gists::File;
/// let gitignore = octocrab::instance()
/// .gists()
/// .create(&[
/// File::new("hello_world.rs", "fn main() {\n println!(\"Hello World!\");\n}")
/// ])
/// // Optional Parameters
/// .description("Hello World in Rust")
/// .public(false)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create<'files>(&self, files: &'files [File]) -> CreateGistBuilder<'octo, 'files> {
CreateGistBuilder::new(files)
}
/// Star a gist.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// assert!(octocrab::instance().gists().star("id").await?);
/// # Ok(())
/// # }
/// ```
pub async fn star(&self, id: &str) -> crate::Result<bool> {
let url = self.crab.absolute_url(format!("/gists/{}/star", id))?;
let resp = self.crab._put(url, None::<&()>).await?;
match resp.status() {
StatusCode::NO_CONTENT => Ok(true),
StatusCode::NOT_FOUND => Ok(false),
_ => {
crate::map_github_error(resp).await?;
unreachable!()
}
}
}
/// Get a single gist.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let gitignore = octocrab::instance().gitignore().get("C").await?;
/// # Ok(())
/// # }
/// ```
pub async fn get(&self, name: impl AsRef<str>) -> crate::Result<String> {
let route = format!("/gitignore/templates/{name}", name = name.as_ref());
let request = self
.crab
.client
.get(self.crab.absolute_url(route)?)
.header(reqwest::header::ACCEPT, crate::format_media_type("raw"));
self.crab
.execute(request)
.await?
.text()
.await
.context(crate::error::Http)
}
}