jinxapi_github/v1_1_4/request/
projects_add_collaborator.rs

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