jinxapi_github/v1_1_4/request/
git_create_blob.rs

1//! Create a blob
2//! 
3//! [API method documentation](https://docs.github.com/rest/reference/git#create-a-blob)
4
5pub struct Content<Body>
6{
7    body: Body,
8    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
9}
10
11impl<Body> Content<Body> {
12    pub fn new(body: Body) -> Self {
13        Self { body, content_type_value: None }
14    }
15
16    #[must_use]
17    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
18        self.content_type_value = Some(content_type.into());
19        self
20    }
21
22    fn content_type(&self) -> Option<&[u8]> {
23        self.content_type_value.as_deref()
24    }
25
26    fn into_body(self) -> Body {
27        self.body
28    }
29}
30
31fn url_string(
32    base_url: &str,
33    p_owner: &str,
34    p_repo: &str,
35) -> Result<String, crate::v1_1_4::ApiError> {
36    let trimmed = if base_url.is_empty() {
37        "https://api.github.com"
38    } else {
39        base_url.trim_end_matches('/')
40    };
41    let mut url = String::with_capacity(trimmed.len() + 36);
42    url.push_str(trimmed);
43    url.push_str("/repos/");
44    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
45    url.push('/');
46    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
47    url.push_str("/git/blobs");
48    Ok(url)
49}
50
51#[cfg(feature = "hyper")]
52pub fn http_builder(
53    base_url: &str,
54    p_owner: &str,
55    p_repo: &str,
56    h_user_agent: &str,
57    h_accept: ::std::option::Option<&str>,
58) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
59    let url = url_string(
60        base_url,
61        p_owner,
62        p_repo,
63    )?;
64    let mut builder = ::http::request::Request::post(url);
65    builder = builder.header(
66        "User-Agent",
67        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
68    );
69    if let Some(value) = &h_accept {
70        builder = builder.header(
71            "Accept",
72            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
73        );
74    }
75    Ok(builder)
76}
77
78#[cfg(feature = "hyper")]
79pub fn hyper_request(
80    mut builder: ::http::request::Builder,
81    content: Content<::hyper::Body>,
82) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
83{
84    if let Some(content_type) = content.content_type() {
85        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
86    }
87    Ok(builder.body(content.into_body())?)
88}
89
90#[cfg(feature = "hyper")]
91impl From<::hyper::Body> for Content<::hyper::Body> {
92    fn from(body: ::hyper::Body) -> Self {
93        Self::new(body)
94    }
95}
96
97#[cfg(feature = "reqwest")]
98pub fn reqwest_builder(
99    base_url: &str,
100    p_owner: &str,
101    p_repo: &str,
102    h_user_agent: &str,
103    h_accept: ::std::option::Option<&str>,
104) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
105    let url = url_string(
106        base_url,
107        p_owner,
108        p_repo,
109    )?;
110    let reqwest_url = ::reqwest::Url::parse(&url)?;
111    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
112    let headers = request.headers_mut();
113    headers.append(
114        "User-Agent",
115        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
116    );
117    if let Some(value) = &h_accept {
118        headers.append(
119            "Accept",
120            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
121        );
122    }
123    Ok(request)
124}
125
126#[cfg(feature = "reqwest")]
127pub fn reqwest_request(
128    mut builder: ::reqwest::Request,
129    content: Content<::reqwest::Body>,
130) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
131    if let Some(content_type) = content.content_type() {
132        builder.headers_mut().append(
133            ::reqwest::header::HeaderName::from_static("content-type"),
134            ::reqwest::header::HeaderValue::try_from(content_type)?,
135        );
136    }
137    *builder.body_mut() = Some(content.into_body());
138    Ok(builder)
139}
140
141#[cfg(feature = "reqwest")]
142impl From<::reqwest::Body> for Content<::reqwest::Body> {
143    fn from(body: ::reqwest::Body) -> Self {
144        Self::new(body)
145    }
146}
147
148#[cfg(feature = "reqwest-blocking")]
149pub fn reqwest_blocking_builder(
150    base_url: &str,
151    p_owner: &str,
152    p_repo: &str,
153    h_user_agent: &str,
154    h_accept: ::std::option::Option<&str>,
155) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
156    let url = url_string(
157        base_url,
158        p_owner,
159        p_repo,
160    )?;
161    let reqwest_url = ::reqwest::Url::parse(&url)?;
162    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
163    let headers = request.headers_mut();
164    headers.append(
165        "User-Agent",
166        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
167    );
168    if let Some(value) = &h_accept {
169        headers.append(
170            "Accept",
171            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
172        );
173    }
174    Ok(request)
175}
176
177#[cfg(feature = "reqwest-blocking")]
178pub fn reqwest_blocking_request(
179    mut builder: ::reqwest::blocking::Request,
180    content: Content<::reqwest::blocking::Body>,
181) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
182    if let Some(content_type) = content.content_type() {
183        builder.headers_mut().append(
184            ::reqwest::header::HeaderName::from_static("content-type"),
185            ::reqwest::header::HeaderValue::try_from(content_type)?,
186        );
187    }
188    *builder.body_mut() = Some(content.into_body());
189    Ok(builder)
190}
191
192#[cfg(feature = "reqwest-blocking")]
193impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
194    fn from(body: ::reqwest::blocking::Body) -> Self {
195        Self::new(body)
196    }
197}
198
199/// Types for body parameter in [`super::git_create_blob`]
200pub mod body {
201    #[allow(non_snake_case)]
202    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
203    pub struct Json<'a> {
204        /// The new blob's content.
205        pub content: ::std::borrow::Cow<'a, str>,
206
207        /// The encoding used for `content`. Currently, `"utf-8"` and `"base64"` are supported.
208        #[serde(skip_serializing_if = "Option::is_none", default)]
209        pub encoding: ::std::option::Option<::std::borrow::Cow<'a, str>>,
210
211        #[serde(flatten)]
212        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
213    }
214
215    #[cfg(feature = "hyper")]
216    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
217        type Error = crate::v1_1_4::ApiError;
218
219        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
220            Ok(
221                Self::new(::serde_json::to_vec(value)?.into())
222                .with_content_type(&b"application/json"[..])
223            )
224        }
225    }
226
227    #[cfg(feature = "reqwest")]
228    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::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-blocking")]
240    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::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}