jinxapi_github/v1_1_4/request/
repos_create_webhook.rs

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