jinxapi_github/v1_1_4/request/
projects_create_for_repo.rs

1//! Create a repository project
2//! 
3//! Creates a repository project board. Returns a `404 Not Found` status if projects are disabled in the repository. If you do not have sufficient privileges to perform this action, a `401 Unauthorized` or `410 Gone` status is returned.
4//! 
5//! [API method documentation](https://docs.github.com/rest/reference/projects#create-a-repository-project)
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_owner: &str,
36    p_repo: &str,
37) -> Result<String, crate::v1_1_4::ApiError> {
38    let trimmed = if base_url.is_empty() {
39        "https://api.github.com"
40    } else {
41        base_url.trim_end_matches('/')
42    };
43    let mut url = String::with_capacity(trimmed.len() + 35);
44    url.push_str(trimmed);
45    url.push_str("/repos/");
46    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
47    url.push('/');
48    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
49    url.push_str("/projects");
50    Ok(url)
51}
52
53#[cfg(feature = "hyper")]
54pub fn http_builder(
55    base_url: &str,
56    p_owner: &str,
57    p_repo: &str,
58    h_user_agent: &str,
59    h_accept: ::std::option::Option<&str>,
60) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
61    let url = url_string(
62        base_url,
63        p_owner,
64        p_repo,
65    )?;
66    let mut builder = ::http::request::Request::post(url);
67    builder = builder.header(
68        "User-Agent",
69        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
70    );
71    if let Some(value) = &h_accept {
72        builder = builder.header(
73            "Accept",
74            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
75        );
76    }
77    Ok(builder)
78}
79
80#[cfg(feature = "hyper")]
81pub fn hyper_request(
82    mut builder: ::http::request::Builder,
83    content: Content<::hyper::Body>,
84) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
85{
86    if let Some(content_type) = content.content_type() {
87        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
88    }
89    Ok(builder.body(content.into_body())?)
90}
91
92#[cfg(feature = "hyper")]
93impl From<::hyper::Body> for Content<::hyper::Body> {
94    fn from(body: ::hyper::Body) -> Self {
95        Self::new(body)
96    }
97}
98
99#[cfg(feature = "reqwest")]
100pub fn reqwest_builder(
101    base_url: &str,
102    p_owner: &str,
103    p_repo: &str,
104    h_user_agent: &str,
105    h_accept: ::std::option::Option<&str>,
106) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
107    let url = url_string(
108        base_url,
109        p_owner,
110        p_repo,
111    )?;
112    let reqwest_url = ::reqwest::Url::parse(&url)?;
113    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
114    let headers = request.headers_mut();
115    headers.append(
116        "User-Agent",
117        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
118    );
119    if let Some(value) = &h_accept {
120        headers.append(
121            "Accept",
122            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
123        );
124    }
125    Ok(request)
126}
127
128#[cfg(feature = "reqwest")]
129pub fn reqwest_request(
130    mut builder: ::reqwest::Request,
131    content: Content<::reqwest::Body>,
132) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
133    if let Some(content_type) = content.content_type() {
134        builder.headers_mut().append(
135            ::reqwest::header::HeaderName::from_static("content-type"),
136            ::reqwest::header::HeaderValue::try_from(content_type)?,
137        );
138    }
139    *builder.body_mut() = Some(content.into_body());
140    Ok(builder)
141}
142
143#[cfg(feature = "reqwest")]
144impl From<::reqwest::Body> for Content<::reqwest::Body> {
145    fn from(body: ::reqwest::Body) -> Self {
146        Self::new(body)
147    }
148}
149
150#[cfg(feature = "reqwest-blocking")]
151pub fn reqwest_blocking_builder(
152    base_url: &str,
153    p_owner: &str,
154    p_repo: &str,
155    h_user_agent: &str,
156    h_accept: ::std::option::Option<&str>,
157) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
158    let url = url_string(
159        base_url,
160        p_owner,
161        p_repo,
162    )?;
163    let reqwest_url = ::reqwest::Url::parse(&url)?;
164    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
165    let headers = request.headers_mut();
166    headers.append(
167        "User-Agent",
168        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
169    );
170    if let Some(value) = &h_accept {
171        headers.append(
172            "Accept",
173            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
174        );
175    }
176    Ok(request)
177}
178
179#[cfg(feature = "reqwest-blocking")]
180pub fn reqwest_blocking_request(
181    mut builder: ::reqwest::blocking::Request,
182    content: Content<::reqwest::blocking::Body>,
183) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
184    if let Some(content_type) = content.content_type() {
185        builder.headers_mut().append(
186            ::reqwest::header::HeaderName::from_static("content-type"),
187            ::reqwest::header::HeaderValue::try_from(content_type)?,
188        );
189    }
190    *builder.body_mut() = Some(content.into_body());
191    Ok(builder)
192}
193
194#[cfg(feature = "reqwest-blocking")]
195impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
196    fn from(body: ::reqwest::blocking::Body) -> Self {
197        Self::new(body)
198    }
199}
200
201/// Types for body parameter in [`super::projects_create_for_repo`]
202pub mod body {
203    #[allow(non_snake_case)]
204    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
205    pub struct Json<'a> {
206        /// The name of the project.
207        pub name: ::std::borrow::Cow<'a, str>,
208
209        /// The description of the project.
210        #[serde(skip_serializing_if = "Option::is_none", default)]
211        pub body: ::std::option::Option<::std::borrow::Cow<'a, str>>,
212
213        #[serde(flatten)]
214        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
215    }
216
217    #[cfg(feature = "hyper")]
218    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
219        type Error = crate::v1_1_4::ApiError;
220
221        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
222            Ok(
223                Self::new(::serde_json::to_vec(value)?.into())
224                .with_content_type(&b"application/json"[..])
225            )
226        }
227    }
228
229    #[cfg(feature = "reqwest")]
230    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
231        type Error = crate::v1_1_4::ApiError;
232
233        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
234            Ok(
235                Self::new(::serde_json::to_vec(value)?.into())
236                .with_content_type(&b"application/json"[..])
237            )
238        }
239    }
240
241    #[cfg(feature = "reqwest-blocking")]
242    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
243        type Error = crate::v1_1_4::ApiError;
244
245        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
246            Ok(
247                Self::new(::serde_json::to_vec(value)?.into())
248                .with_content_type(&b"application/json"[..])
249            )
250        }
251    }
252}