jinxapi_github/v1_1_4/request/
git_update_ref.rs

1//! Update a reference
2//! 
3//! [API method documentation](https://docs.github.com/rest/reference/git#update-a-reference)
4
5pub struct Content<Body>
6{
7    body: Body,
8    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
9}
10
11impl<Body> Content<Body> {
12    pub fn new(body: Body) -> Self {
13        Self { body, content_type_value: None }
14    }
15
16    #[must_use]
17    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
18        self.content_type_value = Some(content_type.into());
19        self
20    }
21
22    fn content_type(&self) -> Option<&[u8]> {
23        self.content_type_value.as_deref()
24    }
25
26    fn into_body(self) -> Body {
27        self.body
28    }
29}
30
31fn url_string(
32    base_url: &str,
33    p_owner: &str,
34    p_repo: &str,
35    p_ref: &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() + 37);
43    url.push_str(trimmed);
44    url.push_str("/repos/");
45    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
46    url.push('/');
47    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
48    url.push_str("/git/refs/");
49    ::querylizer::Simple::extend(&mut url, &p_ref, false, &::querylizer::encode_path)?;
50    Ok(url)
51}
52
53#[cfg(feature = "hyper")]
54pub fn http_builder(
55    base_url: &str,
56    p_owner: &str,
57    p_repo: &str,
58    p_ref: &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        p_ref,
67    )?;
68    let mut builder = ::http::request::Request::patch(url);
69    builder = builder.header(
70        "User-Agent",
71        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
72    );
73    if let Some(value) = &h_accept {
74        builder = builder.header(
75            "Accept",
76            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
77        );
78    }
79    Ok(builder)
80}
81
82#[cfg(feature = "hyper")]
83pub fn hyper_request(
84    mut builder: ::http::request::Builder,
85    content: Content<::hyper::Body>,
86) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
87{
88    if let Some(content_type) = content.content_type() {
89        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
90    }
91    Ok(builder.body(content.into_body())?)
92}
93
94#[cfg(feature = "hyper")]
95impl From<::hyper::Body> for Content<::hyper::Body> {
96    fn from(body: ::hyper::Body) -> Self {
97        Self::new(body)
98    }
99}
100
101#[cfg(feature = "reqwest")]
102pub fn reqwest_builder(
103    base_url: &str,
104    p_owner: &str,
105    p_repo: &str,
106    p_ref: &str,
107    h_user_agent: &str,
108    h_accept: ::std::option::Option<&str>,
109) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
110    let url = url_string(
111        base_url,
112        p_owner,
113        p_repo,
114        p_ref,
115    )?;
116    let reqwest_url = ::reqwest::Url::parse(&url)?;
117    let mut request = ::reqwest::Request::new(::reqwest::Method::PATCH, reqwest_url);
118    let headers = request.headers_mut();
119    headers.append(
120        "User-Agent",
121        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
122    );
123    if let Some(value) = &h_accept {
124        headers.append(
125            "Accept",
126            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
127        );
128    }
129    Ok(request)
130}
131
132#[cfg(feature = "reqwest")]
133pub fn reqwest_request(
134    mut builder: ::reqwest::Request,
135    content: Content<::reqwest::Body>,
136) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
137    if let Some(content_type) = content.content_type() {
138        builder.headers_mut().append(
139            ::reqwest::header::HeaderName::from_static("content-type"),
140            ::reqwest::header::HeaderValue::try_from(content_type)?,
141        );
142    }
143    *builder.body_mut() = Some(content.into_body());
144    Ok(builder)
145}
146
147#[cfg(feature = "reqwest")]
148impl From<::reqwest::Body> for Content<::reqwest::Body> {
149    fn from(body: ::reqwest::Body) -> Self {
150        Self::new(body)
151    }
152}
153
154#[cfg(feature = "reqwest-blocking")]
155pub fn reqwest_blocking_builder(
156    base_url: &str,
157    p_owner: &str,
158    p_repo: &str,
159    p_ref: &str,
160    h_user_agent: &str,
161    h_accept: ::std::option::Option<&str>,
162) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
163    let url = url_string(
164        base_url,
165        p_owner,
166        p_repo,
167        p_ref,
168    )?;
169    let reqwest_url = ::reqwest::Url::parse(&url)?;
170    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PATCH, reqwest_url);
171    let headers = request.headers_mut();
172    headers.append(
173        "User-Agent",
174        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
175    );
176    if let Some(value) = &h_accept {
177        headers.append(
178            "Accept",
179            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
180        );
181    }
182    Ok(request)
183}
184
185#[cfg(feature = "reqwest-blocking")]
186pub fn reqwest_blocking_request(
187    mut builder: ::reqwest::blocking::Request,
188    content: Content<::reqwest::blocking::Body>,
189) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
190    if let Some(content_type) = content.content_type() {
191        builder.headers_mut().append(
192            ::reqwest::header::HeaderName::from_static("content-type"),
193            ::reqwest::header::HeaderValue::try_from(content_type)?,
194        );
195    }
196    *builder.body_mut() = Some(content.into_body());
197    Ok(builder)
198}
199
200#[cfg(feature = "reqwest-blocking")]
201impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
202    fn from(body: ::reqwest::blocking::Body) -> Self {
203        Self::new(body)
204    }
205}
206
207/// Types for body parameter in [`super::git_update_ref`]
208pub mod body {
209    #[allow(non_snake_case)]
210    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
211    pub struct Json<'a> {
212        /// The SHA1 value to set this reference to
213        pub sha: ::std::borrow::Cow<'a, str>,
214
215        /// Indicates whether to force the update or to make sure the update is a fast-forward update. Leaving this out or setting it to `false` will make sure you're not overwriting work.
216        #[serde(skip_serializing_if = "Option::is_none", default)]
217        pub force: ::std::option::Option<bool>,
218
219        #[serde(flatten)]
220        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
221    }
222
223    #[cfg(feature = "hyper")]
224    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
225        type Error = crate::v1_1_4::ApiError;
226
227        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
228            Ok(
229                Self::new(::serde_json::to_vec(value)?.into())
230                .with_content_type(&b"application/json"[..])
231            )
232        }
233    }
234
235    #[cfg(feature = "reqwest")]
236    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
237        type Error = crate::v1_1_4::ApiError;
238
239        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
240            Ok(
241                Self::new(::serde_json::to_vec(value)?.into())
242                .with_content_type(&b"application/json"[..])
243            )
244        }
245    }
246
247    #[cfg(feature = "reqwest-blocking")]
248    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
249        type Error = crate::v1_1_4::ApiError;
250
251        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
252            Ok(
253                Self::new(::serde_json::to_vec(value)?.into())
254                .with_content_type(&b"application/json"[..])
255            )
256        }
257    }
258}