jinxapi_github/v1_1_4/request/
repos_create_using_template.rs

1//! Create a repository using a template
2//! 
3//! Creates a new repository using a repository template. Use the `template_owner` and `template_repo` route parameters to specify the repository to use as the template. The authenticated user must own or be a member of an organization that owns the repository. To check if a repository is available to use as a template, get the repository's information using the [Get a repository](https://docs.github.com/rest/reference/repos#get-a-repository) endpoint and check that the `is_template` key is `true`.
4//! 
5//! **OAuth scope requirements**
6//! 
7//! When using [OAuth](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/), authorizations must include:
8//! 
9//! *   `public_repo` scope or `repo` scope to create a public repository. Note: For GitHub AE, use `repo` scope to create an internal repository.
10//! *   `repo` scope to create a private repository
11//! 
12//! [API method documentation](https://docs.github.com/rest/reference/repos#create-a-repository-using-a-template)
13
14pub struct Content<Body>
15{
16    body: Body,
17    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
18}
19
20impl<Body> Content<Body> {
21    pub fn new(body: Body) -> Self {
22        Self { body, content_type_value: None }
23    }
24
25    #[must_use]
26    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
27        self.content_type_value = Some(content_type.into());
28        self
29    }
30
31    fn content_type(&self) -> Option<&[u8]> {
32        self.content_type_value.as_deref()
33    }
34
35    fn into_body(self) -> Body {
36        self.body
37    }
38}
39
40fn url_string(
41    base_url: &str,
42    p_template_owner: &str,
43    p_template_repo: &str,
44) -> Result<String, crate::v1_1_4::ApiError> {
45    let trimmed = if base_url.is_empty() {
46        "https://api.github.com"
47    } else {
48        base_url.trim_end_matches('/')
49    };
50    let mut url = String::with_capacity(trimmed.len() + 35);
51    url.push_str(trimmed);
52    url.push_str("/repos/");
53    ::querylizer::Simple::extend(&mut url, &p_template_owner, false, &::querylizer::encode_path)?;
54    url.push('/');
55    ::querylizer::Simple::extend(&mut url, &p_template_repo, false, &::querylizer::encode_path)?;
56    url.push_str("/generate");
57    Ok(url)
58}
59
60#[cfg(feature = "hyper")]
61pub fn http_builder(
62    base_url: &str,
63    p_template_owner: &str,
64    p_template_repo: &str,
65    h_user_agent: &str,
66    h_accept: ::std::option::Option<&str>,
67) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
68    let url = url_string(
69        base_url,
70        p_template_owner,
71        p_template_repo,
72    )?;
73    let mut builder = ::http::request::Request::post(url);
74    builder = builder.header(
75        "User-Agent",
76        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
77    );
78    if let Some(value) = &h_accept {
79        builder = builder.header(
80            "Accept",
81            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
82        );
83    }
84    Ok(builder)
85}
86
87#[cfg(feature = "hyper")]
88pub fn hyper_request(
89    mut builder: ::http::request::Builder,
90    content: Content<::hyper::Body>,
91) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
92{
93    if let Some(content_type) = content.content_type() {
94        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
95    }
96    Ok(builder.body(content.into_body())?)
97}
98
99#[cfg(feature = "hyper")]
100impl From<::hyper::Body> for Content<::hyper::Body> {
101    fn from(body: ::hyper::Body) -> Self {
102        Self::new(body)
103    }
104}
105
106#[cfg(feature = "reqwest")]
107pub fn reqwest_builder(
108    base_url: &str,
109    p_template_owner: &str,
110    p_template_repo: &str,
111    h_user_agent: &str,
112    h_accept: ::std::option::Option<&str>,
113) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
114    let url = url_string(
115        base_url,
116        p_template_owner,
117        p_template_repo,
118    )?;
119    let reqwest_url = ::reqwest::Url::parse(&url)?;
120    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
121    let headers = request.headers_mut();
122    headers.append(
123        "User-Agent",
124        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
125    );
126    if let Some(value) = &h_accept {
127        headers.append(
128            "Accept",
129            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
130        );
131    }
132    Ok(request)
133}
134
135#[cfg(feature = "reqwest")]
136pub fn reqwest_request(
137    mut builder: ::reqwest::Request,
138    content: Content<::reqwest::Body>,
139) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
140    if let Some(content_type) = content.content_type() {
141        builder.headers_mut().append(
142            ::reqwest::header::HeaderName::from_static("content-type"),
143            ::reqwest::header::HeaderValue::try_from(content_type)?,
144        );
145    }
146    *builder.body_mut() = Some(content.into_body());
147    Ok(builder)
148}
149
150#[cfg(feature = "reqwest")]
151impl From<::reqwest::Body> for Content<::reqwest::Body> {
152    fn from(body: ::reqwest::Body) -> Self {
153        Self::new(body)
154    }
155}
156
157#[cfg(feature = "reqwest-blocking")]
158pub fn reqwest_blocking_builder(
159    base_url: &str,
160    p_template_owner: &str,
161    p_template_repo: &str,
162    h_user_agent: &str,
163    h_accept: ::std::option::Option<&str>,
164) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
165    let url = url_string(
166        base_url,
167        p_template_owner,
168        p_template_repo,
169    )?;
170    let reqwest_url = ::reqwest::Url::parse(&url)?;
171    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
172    let headers = request.headers_mut();
173    headers.append(
174        "User-Agent",
175        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
176    );
177    if let Some(value) = &h_accept {
178        headers.append(
179            "Accept",
180            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
181        );
182    }
183    Ok(request)
184}
185
186#[cfg(feature = "reqwest-blocking")]
187pub fn reqwest_blocking_request(
188    mut builder: ::reqwest::blocking::Request,
189    content: Content<::reqwest::blocking::Body>,
190) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
191    if let Some(content_type) = content.content_type() {
192        builder.headers_mut().append(
193            ::reqwest::header::HeaderName::from_static("content-type"),
194            ::reqwest::header::HeaderValue::try_from(content_type)?,
195        );
196    }
197    *builder.body_mut() = Some(content.into_body());
198    Ok(builder)
199}
200
201#[cfg(feature = "reqwest-blocking")]
202impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
203    fn from(body: ::reqwest::blocking::Body) -> Self {
204        Self::new(body)
205    }
206}
207
208/// Types for body parameter in [`super::repos_create_using_template`]
209pub mod body {
210    #[allow(non_snake_case)]
211    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
212    pub struct Json<'a> {
213        /// The organization or person who will own the new repository. To create a new repository in an organization, the authenticated user must be a member of the specified organization.
214        #[serde(skip_serializing_if = "Option::is_none", default)]
215        pub owner: ::std::option::Option<::std::borrow::Cow<'a, str>>,
216
217        /// The name of the new repository.
218        pub name: ::std::borrow::Cow<'a, str>,
219
220        /// A short description of the new repository.
221        #[serde(skip_serializing_if = "Option::is_none", default)]
222        pub description: ::std::option::Option<::std::borrow::Cow<'a, str>>,
223
224        /// Set to `true` to include the directory structure and files from all branches in the template repository, and not just the default branch. Default: `false`.
225        #[serde(skip_serializing_if = "Option::is_none", default)]
226        pub include_all_branches: ::std::option::Option<bool>,
227
228        /// Either `true` to create a new private repository or `false` to create a new public one.
229        #[serde(skip_serializing_if = "Option::is_none", default)]
230        pub private: ::std::option::Option<bool>,
231
232        #[serde(flatten)]
233        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
234    }
235
236    #[cfg(feature = "hyper")]
237    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
238        type Error = crate::v1_1_4::ApiError;
239
240        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
241            Ok(
242                Self::new(::serde_json::to_vec(value)?.into())
243                .with_content_type(&b"application/json"[..])
244            )
245        }
246    }
247
248    #[cfg(feature = "reqwest")]
249    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
250        type Error = crate::v1_1_4::ApiError;
251
252        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
253            Ok(
254                Self::new(::serde_json::to_vec(value)?.into())
255                .with_content_type(&b"application/json"[..])
256            )
257        }
258    }
259
260    #[cfg(feature = "reqwest-blocking")]
261    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
262        type Error = crate::v1_1_4::ApiError;
263
264        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
265            Ok(
266                Self::new(::serde_json::to_vec(value)?.into())
267                .with_content_type(&b"application/json"[..])
268            )
269        }
270    }
271}