jinxapi_github/v1_1_4/request/
git_create_tree.rs

1//! Create a tree
2//! 
3//! The tree creation API accepts nested entries. If you specify both a tree and a nested path modifying that tree, this endpoint will overwrite the contents of the tree with the new path contents, and create a new tree structure.
4//! 
5//! If you use this endpoint to add, delete, or modify the file contents in a tree, you will need to commit the tree and then update a branch to point to the commit. For more information see "[Create a commit](https://docs.github.com/rest/reference/git#create-a-commit)" and "[Update a reference](https://docs.github.com/rest/reference/git#update-a-reference)."
6//! 
7//! [API method documentation](https://docs.github.com/rest/reference/git#create-a-tree)
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
35fn url_string(
36    base_url: &str,
37    p_owner: &str,
38    p_repo: &str,
39) -> Result<String, crate::v1_1_4::ApiError> {
40    let trimmed = if base_url.is_empty() {
41        "https://api.github.com"
42    } else {
43        base_url.trim_end_matches('/')
44    };
45    let mut url = String::with_capacity(trimmed.len() + 36);
46    url.push_str(trimmed);
47    url.push_str("/repos/");
48    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
49    url.push('/');
50    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
51    url.push_str("/git/trees");
52    Ok(url)
53}
54
55#[cfg(feature = "hyper")]
56pub fn http_builder(
57    base_url: &str,
58    p_owner: &str,
59    p_repo: &str,
60    h_user_agent: &str,
61    h_accept: ::std::option::Option<&str>,
62) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
63    let url = url_string(
64        base_url,
65        p_owner,
66        p_repo,
67    )?;
68    let mut builder = ::http::request::Request::post(url);
69    builder = builder.header(
70        "User-Agent",
71        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
72    );
73    if let Some(value) = &h_accept {
74        builder = builder.header(
75            "Accept",
76            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
77        );
78    }
79    Ok(builder)
80}
81
82#[cfg(feature = "hyper")]
83pub fn hyper_request(
84    mut builder: ::http::request::Builder,
85    content: Content<::hyper::Body>,
86) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
87{
88    if let Some(content_type) = content.content_type() {
89        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
90    }
91    Ok(builder.body(content.into_body())?)
92}
93
94#[cfg(feature = "hyper")]
95impl From<::hyper::Body> for Content<::hyper::Body> {
96    fn from(body: ::hyper::Body) -> Self {
97        Self::new(body)
98    }
99}
100
101#[cfg(feature = "reqwest")]
102pub fn reqwest_builder(
103    base_url: &str,
104    p_owner: &str,
105    p_repo: &str,
106    h_user_agent: &str,
107    h_accept: ::std::option::Option<&str>,
108) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
109    let url = url_string(
110        base_url,
111        p_owner,
112        p_repo,
113    )?;
114    let reqwest_url = ::reqwest::Url::parse(&url)?;
115    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
116    let headers = request.headers_mut();
117    headers.append(
118        "User-Agent",
119        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
120    );
121    if let Some(value) = &h_accept {
122        headers.append(
123            "Accept",
124            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
125        );
126    }
127    Ok(request)
128}
129
130#[cfg(feature = "reqwest")]
131pub fn reqwest_request(
132    mut builder: ::reqwest::Request,
133    content: Content<::reqwest::Body>,
134) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
135    if let Some(content_type) = content.content_type() {
136        builder.headers_mut().append(
137            ::reqwest::header::HeaderName::from_static("content-type"),
138            ::reqwest::header::HeaderValue::try_from(content_type)?,
139        );
140    }
141    *builder.body_mut() = Some(content.into_body());
142    Ok(builder)
143}
144
145#[cfg(feature = "reqwest")]
146impl From<::reqwest::Body> for Content<::reqwest::Body> {
147    fn from(body: ::reqwest::Body) -> Self {
148        Self::new(body)
149    }
150}
151
152#[cfg(feature = "reqwest-blocking")]
153pub fn reqwest_blocking_builder(
154    base_url: &str,
155    p_owner: &str,
156    p_repo: &str,
157    h_user_agent: &str,
158    h_accept: ::std::option::Option<&str>,
159) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
160    let url = url_string(
161        base_url,
162        p_owner,
163        p_repo,
164    )?;
165    let reqwest_url = ::reqwest::Url::parse(&url)?;
166    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
167    let headers = request.headers_mut();
168    headers.append(
169        "User-Agent",
170        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
171    );
172    if let Some(value) = &h_accept {
173        headers.append(
174            "Accept",
175            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
176        );
177    }
178    Ok(request)
179}
180
181#[cfg(feature = "reqwest-blocking")]
182pub fn reqwest_blocking_request(
183    mut builder: ::reqwest::blocking::Request,
184    content: Content<::reqwest::blocking::Body>,
185) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
186    if let Some(content_type) = content.content_type() {
187        builder.headers_mut().append(
188            ::reqwest::header::HeaderName::from_static("content-type"),
189            ::reqwest::header::HeaderValue::try_from(content_type)?,
190        );
191    }
192    *builder.body_mut() = Some(content.into_body());
193    Ok(builder)
194}
195
196#[cfg(feature = "reqwest-blocking")]
197impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
198    fn from(body: ::reqwest::blocking::Body) -> Self {
199        Self::new(body)
200    }
201}
202
203/// Types for body parameter in [`super::git_create_tree`]
204pub mod body {
205    #[allow(non_snake_case)]
206    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
207    pub struct Json<'a> {
208        /// Objects (of `path`, `mode`, `type`, and `sha`) specifying a tree structure.
209        pub tree: ::std::borrow::Cow<'a, [crate::v1_1_4::request::git_create_tree::body::json::Tree<'a>]>,
210
211        /// The SHA1 of an existing Git tree object which will be used as the base for the new tree. If provided, a new Git tree object will be created from entries in the Git tree object pointed to by `base_tree` and entries defined in the `tree` parameter. Entries defined in the `tree` parameter will overwrite items from `base_tree` with the same `path`. If you're creating new changes on a branch, then normally you'd set `base_tree` to the SHA1 of the Git tree object of the current latest commit on the branch you're working on.
212        /// If not provided, GitHub will create a new Git tree object from only the entries defined in the `tree` parameter. If you create a new commit pointing to such a tree, then all files which were a part of the parent commit's tree and were not defined in the `tree` parameter will be listed as deleted by the new commit.
213        #[serde(skip_serializing_if = "Option::is_none", default)]
214        pub base_tree: ::std::option::Option<::std::borrow::Cow<'a, str>>,
215
216        #[serde(flatten)]
217        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
218    }
219
220    /// Types for fields in [`Json`]
221    pub mod json {
222        #[allow(non_snake_case)]
223        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
224        pub struct Tree<'a> {
225            /// The file referenced in the tree.
226            #[serde(skip_serializing_if = "Option::is_none", default)]
227            pub path: ::std::option::Option<::std::borrow::Cow<'a, str>>,
228
229            /// The file mode; one of `100644` for file (blob), `100755` for executable (blob), `040000` for subdirectory (tree), `160000` for submodule (commit), or `120000` for a blob that specifies the path of a symlink.
230            #[serde(skip_serializing_if = "Option::is_none", default)]
231            pub mode: ::std::option::Option<::std::borrow::Cow<'a, str>>,
232
233            /// Either `blob`, `tree`, or `commit`.
234            #[serde(skip_serializing_if = "Option::is_none", default)]
235            pub r#type: ::std::option::Option<::std::borrow::Cow<'a, str>>,
236
237            /// The SHA1 checksum ID of the object in the tree. Also called `tree.sha`. If the value is `null` then the file will be deleted.  
238            ///   
239            /// **Note:** Use either `tree.sha` or `content` to specify the contents of the entry. Using both `tree.sha` and `content` will return an error.
240            #[serde(skip_serializing_if = "Option::is_none", default, deserialize_with = "crate::v1_1_4::support::deserialize_some")]
241            pub sha: ::std::option::Option<::std::option::Option<::std::borrow::Cow<'a, str>>>,
242
243            /// The content you want this file to have. GitHub will write this blob out and use that SHA for this entry. Use either this, or `tree.sha`.  
244            ///   
245            /// **Note:** Use either `tree.sha` or `content` to specify the contents of the entry. Using both `tree.sha` and `content` will return an error.
246            #[serde(skip_serializing_if = "Option::is_none", default)]
247            pub content: ::std::option::Option<::std::borrow::Cow<'a, str>>,
248
249            #[serde(flatten)]
250            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
251        }
252    }
253
254    #[cfg(feature = "hyper")]
255    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
256        type Error = crate::v1_1_4::ApiError;
257
258        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
259            Ok(
260                Self::new(::serde_json::to_vec(value)?.into())
261                .with_content_type(&b"application/json"[..])
262            )
263        }
264    }
265
266    #[cfg(feature = "reqwest")]
267    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
268        type Error = crate::v1_1_4::ApiError;
269
270        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
271            Ok(
272                Self::new(::serde_json::to_vec(value)?.into())
273                .with_content_type(&b"application/json"[..])
274            )
275        }
276    }
277
278    #[cfg(feature = "reqwest-blocking")]
279    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
280        type Error = crate::v1_1_4::ApiError;
281
282        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
283            Ok(
284                Self::new(::serde_json::to_vec(value)?.into())
285                .with_content_type(&b"application/json"[..])
286            )
287        }
288    }
289}