jinxapi_github/v1_1_4/request/
activity_set_thread_subscription.rs

1//! Set a thread subscription
2//! 
3//! If you are watching a repository, you receive notifications for all threads by default. Use this endpoint to ignore future notifications for threads until you comment on the thread or get an **@mention**.
4//! 
5//! You can also use this endpoint to subscribe to threads that you are currently not receiving notifications for or to subscribed to threads that you have previously ignored.
6//! 
7//! Unsubscribing from a conversation in a repository that you are not watching is functionally equivalent to the [Delete a thread subscription](https://docs.github.com/rest/reference/activity#delete-a-thread-subscription) endpoint.
8//! 
9//! [API method documentation](https://docs.github.com/rest/reference/activity#set-a-thread-subscription)
10
11pub struct Content<Body>
12{
13    body: Body,
14    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
15}
16
17impl<Body> Content<Body> {
18    pub fn new(body: Body) -> Self {
19        Self { body, content_type_value: None }
20    }
21
22    #[must_use]
23    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
24        self.content_type_value = Some(content_type.into());
25        self
26    }
27
28    fn content_type(&self) -> Option<&[u8]> {
29        self.content_type_value.as_deref()
30    }
31
32    fn into_body(self) -> Body {
33        self.body
34    }
35}
36
37fn url_string(
38    base_url: &str,
39    p_thread_id: i64,
40) -> Result<String, crate::v1_1_4::ApiError> {
41    let trimmed = if base_url.is_empty() {
42        "https://api.github.com"
43    } else {
44        base_url.trim_end_matches('/')
45    };
46    let mut url = String::with_capacity(trimmed.len() + 53);
47    url.push_str(trimmed);
48    url.push_str("/notifications/threads/");
49    ::querylizer::Simple::extend(&mut url, &p_thread_id, false, &::querylizer::encode_path)?;
50    url.push_str("/subscription");
51    Ok(url)
52}
53
54#[cfg(feature = "hyper")]
55pub fn http_builder(
56    base_url: &str,
57    p_thread_id: i64,
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_thread_id,
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_thread_id: i64,
102    h_user_agent: &str,
103    h_accept: ::std::option::Option<&str>,
104) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
105    let url = url_string(
106        base_url,
107        p_thread_id,
108    )?;
109    let reqwest_url = ::reqwest::Url::parse(&url)?;
110    let mut request = ::reqwest::Request::new(::reqwest::Method::PUT, reqwest_url);
111    let headers = request.headers_mut();
112    headers.append(
113        "User-Agent",
114        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
115    );
116    if let Some(value) = &h_accept {
117        headers.append(
118            "Accept",
119            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
120        );
121    }
122    Ok(request)
123}
124
125#[cfg(feature = "reqwest")]
126pub fn reqwest_request(
127    mut builder: ::reqwest::Request,
128    content: Content<::reqwest::Body>,
129) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
130    if let Some(content_type) = content.content_type() {
131        builder.headers_mut().append(
132            ::reqwest::header::HeaderName::from_static("content-type"),
133            ::reqwest::header::HeaderValue::try_from(content_type)?,
134        );
135    }
136    *builder.body_mut() = Some(content.into_body());
137    Ok(builder)
138}
139
140#[cfg(feature = "reqwest")]
141impl From<::reqwest::Body> for Content<::reqwest::Body> {
142    fn from(body: ::reqwest::Body) -> Self {
143        Self::new(body)
144    }
145}
146
147#[cfg(feature = "reqwest-blocking")]
148pub fn reqwest_blocking_builder(
149    base_url: &str,
150    p_thread_id: i64,
151    h_user_agent: &str,
152    h_accept: ::std::option::Option<&str>,
153) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
154    let url = url_string(
155        base_url,
156        p_thread_id,
157    )?;
158    let reqwest_url = ::reqwest::Url::parse(&url)?;
159    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PUT, reqwest_url);
160    let headers = request.headers_mut();
161    headers.append(
162        "User-Agent",
163        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
164    );
165    if let Some(value) = &h_accept {
166        headers.append(
167            "Accept",
168            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
169        );
170    }
171    Ok(request)
172}
173
174#[cfg(feature = "reqwest-blocking")]
175pub fn reqwest_blocking_request(
176    mut builder: ::reqwest::blocking::Request,
177    content: Content<::reqwest::blocking::Body>,
178) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
179    if let Some(content_type) = content.content_type() {
180        builder.headers_mut().append(
181            ::reqwest::header::HeaderName::from_static("content-type"),
182            ::reqwest::header::HeaderValue::try_from(content_type)?,
183        );
184    }
185    *builder.body_mut() = Some(content.into_body());
186    Ok(builder)
187}
188
189#[cfg(feature = "reqwest-blocking")]
190impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
191    fn from(body: ::reqwest::blocking::Body) -> Self {
192        Self::new(body)
193    }
194}
195
196/// Types for body parameter in [`super::activity_set_thread_subscription`]
197pub mod body {
198    #[allow(non_snake_case)]
199    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
200    pub struct Json<'a> {
201        /// Whether to block all notifications from a thread.
202        #[serde(skip_serializing_if = "Option::is_none", default)]
203        pub ignored: ::std::option::Option<bool>,
204
205        #[serde(flatten)]
206        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
207    }
208
209    #[cfg(feature = "hyper")]
210    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
211        type Error = crate::v1_1_4::ApiError;
212
213        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
214            Ok(
215                Self::new(::serde_json::to_vec(value)?.into())
216                .with_content_type(&b"application/json"[..])
217            )
218        }
219    }
220
221    #[cfg(feature = "reqwest")]
222    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
223        type Error = crate::v1_1_4::ApiError;
224
225        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
226            Ok(
227                Self::new(::serde_json::to_vec(value)?.into())
228                .with_content_type(&b"application/json"[..])
229            )
230        }
231    }
232
233    #[cfg(feature = "reqwest-blocking")]
234    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
235        type Error = crate::v1_1_4::ApiError;
236
237        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
238            Ok(
239                Self::new(::serde_json::to_vec(value)?.into())
240                .with_content_type(&b"application/json"[..])
241            )
242        }
243    }
244}