jinxapi_github/v1_1_4/request/
orgs_create_webhook.rs

1//! Create an organization webhook
2//! 
3//! Here's how you can create a hook that posts payloads in JSON format:
4//! 
5//! [API method documentation](https://docs.github.com/rest/reference/orgs#create-an-organization-webhook)
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_org: &str,
36) -> Result<String, crate::v1_1_4::ApiError> {
37    let trimmed = if base_url.is_empty() {
38        "https://api.github.com"
39    } else {
40        base_url.trim_end_matches('/')
41    };
42    let mut url = String::with_capacity(trimmed.len() + 29);
43    url.push_str(trimmed);
44    url.push_str("/orgs/");
45    ::querylizer::Simple::extend(&mut url, &p_org, false, &::querylizer::encode_path)?;
46    url.push_str("/hooks");
47    Ok(url)
48}
49
50#[cfg(feature = "hyper")]
51pub fn http_builder(
52    base_url: &str,
53    p_org: &str,
54    h_user_agent: &str,
55    h_accept: ::std::option::Option<&str>,
56) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
57    let url = url_string(
58        base_url,
59        p_org,
60    )?;
61    let mut builder = ::http::request::Request::post(url);
62    builder = builder.header(
63        "User-Agent",
64        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
65    );
66    if let Some(value) = &h_accept {
67        builder = builder.header(
68            "Accept",
69            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
70        );
71    }
72    Ok(builder)
73}
74
75#[cfg(feature = "hyper")]
76pub fn hyper_request(
77    mut builder: ::http::request::Builder,
78    content: Content<::hyper::Body>,
79) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
80{
81    if let Some(content_type) = content.content_type() {
82        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
83    }
84    Ok(builder.body(content.into_body())?)
85}
86
87#[cfg(feature = "hyper")]
88impl From<::hyper::Body> for Content<::hyper::Body> {
89    fn from(body: ::hyper::Body) -> Self {
90        Self::new(body)
91    }
92}
93
94#[cfg(feature = "reqwest")]
95pub fn reqwest_builder(
96    base_url: &str,
97    p_org: &str,
98    h_user_agent: &str,
99    h_accept: ::std::option::Option<&str>,
100) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
101    let url = url_string(
102        base_url,
103        p_org,
104    )?;
105    let reqwest_url = ::reqwest::Url::parse(&url)?;
106    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
107    let headers = request.headers_mut();
108    headers.append(
109        "User-Agent",
110        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
111    );
112    if let Some(value) = &h_accept {
113        headers.append(
114            "Accept",
115            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
116        );
117    }
118    Ok(request)
119}
120
121#[cfg(feature = "reqwest")]
122pub fn reqwest_request(
123    mut builder: ::reqwest::Request,
124    content: Content<::reqwest::Body>,
125) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
126    if let Some(content_type) = content.content_type() {
127        builder.headers_mut().append(
128            ::reqwest::header::HeaderName::from_static("content-type"),
129            ::reqwest::header::HeaderValue::try_from(content_type)?,
130        );
131    }
132    *builder.body_mut() = Some(content.into_body());
133    Ok(builder)
134}
135
136#[cfg(feature = "reqwest")]
137impl From<::reqwest::Body> for Content<::reqwest::Body> {
138    fn from(body: ::reqwest::Body) -> Self {
139        Self::new(body)
140    }
141}
142
143#[cfg(feature = "reqwest-blocking")]
144pub fn reqwest_blocking_builder(
145    base_url: &str,
146    p_org: &str,
147    h_user_agent: &str,
148    h_accept: ::std::option::Option<&str>,
149) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
150    let url = url_string(
151        base_url,
152        p_org,
153    )?;
154    let reqwest_url = ::reqwest::Url::parse(&url)?;
155    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
156    let headers = request.headers_mut();
157    headers.append(
158        "User-Agent",
159        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
160    );
161    if let Some(value) = &h_accept {
162        headers.append(
163            "Accept",
164            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
165        );
166    }
167    Ok(request)
168}
169
170#[cfg(feature = "reqwest-blocking")]
171pub fn reqwest_blocking_request(
172    mut builder: ::reqwest::blocking::Request,
173    content: Content<::reqwest::blocking::Body>,
174) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
175    if let Some(content_type) = content.content_type() {
176        builder.headers_mut().append(
177            ::reqwest::header::HeaderName::from_static("content-type"),
178            ::reqwest::header::HeaderValue::try_from(content_type)?,
179        );
180    }
181    *builder.body_mut() = Some(content.into_body());
182    Ok(builder)
183}
184
185#[cfg(feature = "reqwest-blocking")]
186impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
187    fn from(body: ::reqwest::blocking::Body) -> Self {
188        Self::new(body)
189    }
190}
191
192/// Types for body parameter in [`super::orgs_create_webhook`]
193pub mod body {
194    #[allow(non_snake_case)]
195    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
196    pub struct Json<'a> {
197        /// Must be passed as "web".
198        pub name: ::std::borrow::Cow<'a, str>,
199
200        pub config: crate::v1_1_4::request::orgs_create_webhook::body::json::Config<'a>,
201
202        /// Determines what [events](https://docs.github.com/webhooks/event-payloads) the hook is triggered for.
203        #[serde(skip_serializing_if = "Option::is_none", default)]
204        pub events: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
205
206        /// Determines if notifications are sent when the webhook is triggered. Set to `true` to send notifications.
207        #[serde(skip_serializing_if = "Option::is_none", default)]
208        pub active: ::std::option::Option<bool>,
209
210        #[serde(flatten)]
211        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
212    }
213
214    /// Types for fields in [`Json`]
215    pub mod json {
216        /// Key/value pairs to provide settings for this webhook. [These are defined below](https://docs.github.com/rest/reference/orgs#create-hook-config-params).
217        #[allow(non_snake_case)]
218        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
219        pub struct Config<'a> {
220            /// The URL to which the payloads will be delivered.
221            /// 
222            /// # Example
223            /// 
224            /// ```json
225            /// "https://example.com/webhook"
226            /// ```
227            pub url: ::std::borrow::Cow<'a, str>,
228
229            /// The media type used to serialize the payloads. Supported values include `json` and `form`. The default is `form`.
230            /// 
231            /// # Example
232            /// 
233            /// ```json
234            /// "\"json\""
235            /// ```
236            #[serde(skip_serializing_if = "Option::is_none", default)]
237            pub content_type: ::std::option::Option<::std::borrow::Cow<'a, str>>,
238
239            /// If provided, the `secret` will be used as the `key` to generate the HMAC hex digest value for [delivery signature headers](https://docs.github.com/webhooks/event-payloads/#delivery-headers).
240            /// 
241            /// # Example
242            /// 
243            /// ```json
244            /// "\"********\""
245            /// ```
246            #[serde(skip_serializing_if = "Option::is_none", default)]
247            pub secret: ::std::option::Option<::std::borrow::Cow<'a, str>>,
248
249            #[serde(skip_serializing_if = "Option::is_none", default)]
250            pub insecure_ssl: ::std::option::Option<::serde_json::value::Value>,
251
252            /// # Example
253            /// 
254            /// ```json
255            /// "\"kdaigle\""
256            /// ```
257            #[serde(skip_serializing_if = "Option::is_none", default)]
258            pub username: ::std::option::Option<::std::borrow::Cow<'a, str>>,
259
260            /// # Example
261            /// 
262            /// ```json
263            /// "\"password\""
264            /// ```
265            #[serde(skip_serializing_if = "Option::is_none", default)]
266            pub password: ::std::option::Option<::std::borrow::Cow<'a, str>>,
267
268            #[serde(flatten)]
269            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
270        }
271    }
272
273    #[cfg(feature = "hyper")]
274    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
275        type Error = crate::v1_1_4::ApiError;
276
277        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
278            Ok(
279                Self::new(::serde_json::to_vec(value)?.into())
280                .with_content_type(&b"application/json"[..])
281            )
282        }
283    }
284
285    #[cfg(feature = "reqwest")]
286    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
287        type Error = crate::v1_1_4::ApiError;
288
289        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
290            Ok(
291                Self::new(::serde_json::to_vec(value)?.into())
292                .with_content_type(&b"application/json"[..])
293            )
294        }
295    }
296
297    #[cfg(feature = "reqwest-blocking")]
298    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
299        type Error = crate::v1_1_4::ApiError;
300
301        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
302            Ok(
303                Self::new(::serde_json::to_vec(value)?.into())
304                .with_content_type(&b"application/json"[..])
305            )
306        }
307    }
308}