jinxapi_github/v1_1_4/request/
repos_create_dispatch_event.rs

1//! Create a repository dispatch event
2//! 
3//! You can use this endpoint to trigger a webhook event called `repository_dispatch` when you want activity that happens outside of GitHub to trigger a GitHub Actions workflow or GitHub App webhook. You must configure your GitHub Actions workflow or GitHub App to run when the `repository_dispatch` event occurs. For an example `repository_dispatch` webhook payload, see "[RepositoryDispatchEvent](https://docs.github.com/webhooks/event-payloads/#repository_dispatch)."
4//! 
5//! The `client_payload` parameter is available for any extra information that your workflow might need. This parameter is a JSON payload that will be passed on when the webhook event is dispatched. For example, the `client_payload` can include a message that a user would like to send using a GitHub Actions workflow. Or the `client_payload` can be used as a test to debug your workflow.
6//! 
7//! This endpoint requires write access to the repository by providing either:
8//! 
9//!   - Personal access tokens with `repo` scope. For more information, see "[Creating a personal access token for the command line](https://docs.github.com/articles/creating-a-personal-access-token-for-the-command-line)" in the GitHub Help documentation.
10//!   - GitHub Apps with both `metadata:read` and `contents:read&write` permissions.
11//! 
12//! This input example shows how you can use the `client_payload` as a test to debug your workflow.
13//! 
14//! [API method documentation](https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event)
15
16pub struct Content<Body>
17{
18    body: Body,
19    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
20}
21
22impl<Body> Content<Body> {
23    pub fn new(body: Body) -> Self {
24        Self { body, content_type_value: None }
25    }
26
27    #[must_use]
28    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
29        self.content_type_value = Some(content_type.into());
30        self
31    }
32
33    fn content_type(&self) -> Option<&[u8]> {
34        self.content_type_value.as_deref()
35    }
36
37    fn into_body(self) -> Body {
38        self.body
39    }
40}
41
42fn url_string(
43    base_url: &str,
44    p_owner: &str,
45    p_repo: &str,
46) -> Result<String, crate::v1_1_4::ApiError> {
47    let trimmed = if base_url.is_empty() {
48        "https://api.github.com"
49    } else {
50        base_url.trim_end_matches('/')
51    };
52    let mut url = String::with_capacity(trimmed.len() + 37);
53    url.push_str(trimmed);
54    url.push_str("/repos/");
55    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
56    url.push('/');
57    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
58    url.push_str("/dispatches");
59    Ok(url)
60}
61
62#[cfg(feature = "hyper")]
63pub fn http_builder(
64    base_url: &str,
65    p_owner: &str,
66    p_repo: &str,
67    h_user_agent: &str,
68    h_accept: ::std::option::Option<&str>,
69) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
70    let url = url_string(
71        base_url,
72        p_owner,
73        p_repo,
74    )?;
75    let mut builder = ::http::request::Request::post(url);
76    builder = builder.header(
77        "User-Agent",
78        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
79    );
80    if let Some(value) = &h_accept {
81        builder = builder.header(
82            "Accept",
83            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
84        );
85    }
86    Ok(builder)
87}
88
89#[cfg(feature = "hyper")]
90pub fn hyper_request(
91    mut builder: ::http::request::Builder,
92    content: Content<::hyper::Body>,
93) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
94{
95    if let Some(content_type) = content.content_type() {
96        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
97    }
98    Ok(builder.body(content.into_body())?)
99}
100
101#[cfg(feature = "hyper")]
102impl From<::hyper::Body> for Content<::hyper::Body> {
103    fn from(body: ::hyper::Body) -> Self {
104        Self::new(body)
105    }
106}
107
108#[cfg(feature = "reqwest")]
109pub fn reqwest_builder(
110    base_url: &str,
111    p_owner: &str,
112    p_repo: &str,
113    h_user_agent: &str,
114    h_accept: ::std::option::Option<&str>,
115) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
116    let url = url_string(
117        base_url,
118        p_owner,
119        p_repo,
120    )?;
121    let reqwest_url = ::reqwest::Url::parse(&url)?;
122    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
123    let headers = request.headers_mut();
124    headers.append(
125        "User-Agent",
126        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
127    );
128    if let Some(value) = &h_accept {
129        headers.append(
130            "Accept",
131            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
132        );
133    }
134    Ok(request)
135}
136
137#[cfg(feature = "reqwest")]
138pub fn reqwest_request(
139    mut builder: ::reqwest::Request,
140    content: Content<::reqwest::Body>,
141) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
142    if let Some(content_type) = content.content_type() {
143        builder.headers_mut().append(
144            ::reqwest::header::HeaderName::from_static("content-type"),
145            ::reqwest::header::HeaderValue::try_from(content_type)?,
146        );
147    }
148    *builder.body_mut() = Some(content.into_body());
149    Ok(builder)
150}
151
152#[cfg(feature = "reqwest")]
153impl From<::reqwest::Body> for Content<::reqwest::Body> {
154    fn from(body: ::reqwest::Body) -> Self {
155        Self::new(body)
156    }
157}
158
159#[cfg(feature = "reqwest-blocking")]
160pub fn reqwest_blocking_builder(
161    base_url: &str,
162    p_owner: &str,
163    p_repo: &str,
164    h_user_agent: &str,
165    h_accept: ::std::option::Option<&str>,
166) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
167    let url = url_string(
168        base_url,
169        p_owner,
170        p_repo,
171    )?;
172    let reqwest_url = ::reqwest::Url::parse(&url)?;
173    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
174    let headers = request.headers_mut();
175    headers.append(
176        "User-Agent",
177        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
178    );
179    if let Some(value) = &h_accept {
180        headers.append(
181            "Accept",
182            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
183        );
184    }
185    Ok(request)
186}
187
188#[cfg(feature = "reqwest-blocking")]
189pub fn reqwest_blocking_request(
190    mut builder: ::reqwest::blocking::Request,
191    content: Content<::reqwest::blocking::Body>,
192) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
193    if let Some(content_type) = content.content_type() {
194        builder.headers_mut().append(
195            ::reqwest::header::HeaderName::from_static("content-type"),
196            ::reqwest::header::HeaderValue::try_from(content_type)?,
197        );
198    }
199    *builder.body_mut() = Some(content.into_body());
200    Ok(builder)
201}
202
203#[cfg(feature = "reqwest-blocking")]
204impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
205    fn from(body: ::reqwest::blocking::Body) -> Self {
206        Self::new(body)
207    }
208}
209
210/// Types for body parameter in [`super::repos_create_dispatch_event`]
211pub mod body {
212    #[allow(non_snake_case)]
213    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
214    pub struct Json<'a> {
215        /// A custom webhook event name. Must be 100 characters or fewer.
216        pub event_type: ::std::borrow::Cow<'a, str>,
217
218        #[serde(skip_serializing_if = "Option::is_none", default)]
219        pub client_payload: ::std::option::Option<::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>>,
220
221        #[serde(flatten)]
222        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
223    }
224
225    #[cfg(feature = "hyper")]
226    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
227        type Error = crate::v1_1_4::ApiError;
228
229        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
230            Ok(
231                Self::new(::serde_json::to_vec(value)?.into())
232                .with_content_type(&b"application/json"[..])
233            )
234        }
235    }
236
237    #[cfg(feature = "reqwest")]
238    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
239        type Error = crate::v1_1_4::ApiError;
240
241        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
242            Ok(
243                Self::new(::serde_json::to_vec(value)?.into())
244                .with_content_type(&b"application/json"[..])
245            )
246        }
247    }
248
249    #[cfg(feature = "reqwest-blocking")]
250    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
251        type Error = crate::v1_1_4::ApiError;
252
253        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
254            Ok(
255                Self::new(::serde_json::to_vec(value)?.into())
256                .with_content_type(&b"application/json"[..])
257            )
258        }
259    }
260}