jinxapi_github/v1_1_4/request/
gists_create.rs

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