jinxapi_github/v1_1_4/request/
gists_update.rs

1//! Update a gist
2//! 
3//! Allows you to update or delete a gist file and rename gist files. Files from the previous version of the gist that aren't explicitly changed during an edit are unchanged.
4//! 
5//! [API method documentation](https://docs.github.com/rest/reference/gists/#update-a-gist)
6
7pub struct Content<Body>
8{
9    body: Body,
10    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
11}
12
13impl<Body> Content<Body> {
14    pub fn new(body: Body) -> Self {
15        Self { body, content_type_value: None }
16    }
17
18    #[must_use]
19    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
20        self.content_type_value = Some(content_type.into());
21        self
22    }
23
24    fn content_type(&self) -> Option<&[u8]> {
25        self.content_type_value.as_deref()
26    }
27
28    fn into_body(self) -> Body {
29        self.body
30    }
31}
32
33fn url_string(
34    base_url: &str,
35    p_gist_id: &str,
36) -> Result<String, crate::v1_1_4::ApiError> {
37    let trimmed = if base_url.is_empty() {
38        "https://api.github.com"
39    } else {
40        base_url.trim_end_matches('/')
41    };
42    let mut url = String::with_capacity(trimmed.len() + 24);
43    url.push_str(trimmed);
44    url.push_str("/gists/");
45    ::querylizer::Simple::extend(&mut url, &p_gist_id, false, &::querylizer::encode_path)?;
46    Ok(url)
47}
48
49#[cfg(feature = "hyper")]
50pub fn http_builder(
51    base_url: &str,
52    p_gist_id: &str,
53    h_user_agent: &str,
54    h_accept: ::std::option::Option<&str>,
55) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
56    let url = url_string(
57        base_url,
58        p_gist_id,
59    )?;
60    let mut builder = ::http::request::Request::patch(url);
61    builder = builder.header(
62        "User-Agent",
63        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
64    );
65    if let Some(value) = &h_accept {
66        builder = builder.header(
67            "Accept",
68            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
69        );
70    }
71    Ok(builder)
72}
73
74#[cfg(feature = "hyper")]
75pub fn hyper_request(
76    mut builder: ::http::request::Builder,
77    content: Content<::hyper::Body>,
78) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
79{
80    if let Some(content_type) = content.content_type() {
81        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
82    }
83    Ok(builder.body(content.into_body())?)
84}
85
86#[cfg(feature = "hyper")]
87impl From<::hyper::Body> for Content<::hyper::Body> {
88    fn from(body: ::hyper::Body) -> Self {
89        Self::new(body)
90    }
91}
92
93#[cfg(feature = "reqwest")]
94pub fn reqwest_builder(
95    base_url: &str,
96    p_gist_id: &str,
97    h_user_agent: &str,
98    h_accept: ::std::option::Option<&str>,
99) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
100    let url = url_string(
101        base_url,
102        p_gist_id,
103    )?;
104    let reqwest_url = ::reqwest::Url::parse(&url)?;
105    let mut request = ::reqwest::Request::new(::reqwest::Method::PATCH, reqwest_url);
106    let headers = request.headers_mut();
107    headers.append(
108        "User-Agent",
109        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
110    );
111    if let Some(value) = &h_accept {
112        headers.append(
113            "Accept",
114            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
115        );
116    }
117    Ok(request)
118}
119
120#[cfg(feature = "reqwest")]
121pub fn reqwest_request(
122    mut builder: ::reqwest::Request,
123    content: Content<::reqwest::Body>,
124) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
125    if let Some(content_type) = content.content_type() {
126        builder.headers_mut().append(
127            ::reqwest::header::HeaderName::from_static("content-type"),
128            ::reqwest::header::HeaderValue::try_from(content_type)?,
129        );
130    }
131    *builder.body_mut() = Some(content.into_body());
132    Ok(builder)
133}
134
135#[cfg(feature = "reqwest")]
136impl From<::reqwest::Body> for Content<::reqwest::Body> {
137    fn from(body: ::reqwest::Body) -> Self {
138        Self::new(body)
139    }
140}
141
142#[cfg(feature = "reqwest-blocking")]
143pub fn reqwest_blocking_builder(
144    base_url: &str,
145    p_gist_id: &str,
146    h_user_agent: &str,
147    h_accept: ::std::option::Option<&str>,
148) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
149    let url = url_string(
150        base_url,
151        p_gist_id,
152    )?;
153    let reqwest_url = ::reqwest::Url::parse(&url)?;
154    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PATCH, reqwest_url);
155    let headers = request.headers_mut();
156    headers.append(
157        "User-Agent",
158        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
159    );
160    if let Some(value) = &h_accept {
161        headers.append(
162            "Accept",
163            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
164        );
165    }
166    Ok(request)
167}
168
169#[cfg(feature = "reqwest-blocking")]
170pub fn reqwest_blocking_request(
171    mut builder: ::reqwest::blocking::Request,
172    content: Content<::reqwest::blocking::Body>,
173) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
174    if let Some(content_type) = content.content_type() {
175        builder.headers_mut().append(
176            ::reqwest::header::HeaderName::from_static("content-type"),
177            ::reqwest::header::HeaderValue::try_from(content_type)?,
178        );
179    }
180    *builder.body_mut() = Some(content.into_body());
181    Ok(builder)
182}
183
184#[cfg(feature = "reqwest-blocking")]
185impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
186    fn from(body: ::reqwest::blocking::Body) -> Self {
187        Self::new(body)
188    }
189}
190
191/// Types for body parameter in [`super::gists_update`]
192pub mod body {
193    #[allow(non_snake_case)]
194    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
195    pub struct Json<'a> {
196        /// Description of the gist
197        /// 
198        /// # Example
199        /// 
200        /// ```json
201        /// "Example Ruby script"
202        /// ```
203        #[serde(skip_serializing_if = "Option::is_none", default)]
204        pub description: ::std::option::Option<::std::borrow::Cow<'a, str>>,
205
206        #[serde(skip_serializing_if = "Option::is_none", default)]
207        pub files: ::std::option::Option<::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::std::option::Option<crate::v1_1_4::request::gists_update::body::json::Files<'a>>>>,
208
209        #[serde(flatten)]
210        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
211    }
212
213    /// Types for fields in [`Json`]
214    pub mod json {
215        #[allow(non_snake_case)]
216        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
217        pub struct Files<'a> {
218            /// The new content of the file
219            #[serde(skip_serializing_if = "Option::is_none", default)]
220            pub content: ::std::option::Option<::std::borrow::Cow<'a, str>>,
221
222            /// The new filename for the file
223            #[serde(skip_serializing_if = "Option::is_none", default, deserialize_with = "crate::v1_1_4::support::deserialize_some")]
224            pub filename: ::std::option::Option<::std::option::Option<::std::borrow::Cow<'a, str>>>,
225
226            #[serde(flatten)]
227            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
228        }
229    }
230
231    #[cfg(feature = "hyper")]
232    impl<'a> TryFrom<&::std::option::Option<crate::v1_1_4::request::gists_update::body::Json<'a>>> for super::Content<::hyper::Body> {
233        type Error = crate::v1_1_4::ApiError;
234
235        fn try_from(value: &::std::option::Option<crate::v1_1_4::request::gists_update::body::Json<'a>>) -> Result<Self, Self::Error> {
236            Ok(
237                Self::new(::serde_json::to_vec(value)?.into())
238                .with_content_type(&b"application/json"[..])
239            )
240        }
241    }
242
243    #[cfg(feature = "reqwest")]
244    impl<'a> TryFrom<&::std::option::Option<crate::v1_1_4::request::gists_update::body::Json<'a>>> for super::Content<::reqwest::Body> {
245        type Error = crate::v1_1_4::ApiError;
246
247        fn try_from(value: &::std::option::Option<crate::v1_1_4::request::gists_update::body::Json<'a>>) -> Result<Self, Self::Error> {
248            Ok(
249                Self::new(::serde_json::to_vec(value)?.into())
250                .with_content_type(&b"application/json"[..])
251            )
252        }
253    }
254
255    #[cfg(feature = "reqwest-blocking")]
256    impl<'a> TryFrom<&::std::option::Option<crate::v1_1_4::request::gists_update::body::Json<'a>>> for super::Content<::reqwest::blocking::Body> {
257        type Error = crate::v1_1_4::ApiError;
258
259        fn try_from(value: &::std::option::Option<crate::v1_1_4::request::gists_update::body::Json<'a>>) -> Result<Self, Self::Error> {
260            Ok(
261                Self::new(::serde_json::to_vec(value)?.into())
262                .with_content_type(&b"application/json"[..])
263            )
264        }
265    }
266}