jinxapi_github/v1_1_4/request/
markdown_render.rs

1//! Render a Markdown document
2//! 
3//! [API method documentation](https://docs.github.com/rest/reference/markdown#render-a-markdown-document)
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
31#[cfg(feature = "hyper")]
32pub fn http_builder(
33    base_url: &str,
34    h_user_agent: &str,
35    h_accept: ::std::option::Option<&str>,
36) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
37    let default_url = concat!("https://api.github.com", "/markdown");
38    let url = if base_url.is_empty() {
39        ::http::uri::Uri::from_static(default_url)
40    } else {
41        let trimmed = base_url.trim_end_matches('/');
42        let mut url = String::with_capacity(trimmed.len() + 9);
43        url.push_str(trimmed);
44        url.push_str(&default_url[22..]);
45        url.try_into().map_err(::http::Error::from)?
46    };
47    let mut builder = ::http::request::Request::post(url);
48    builder = builder.header(
49        "User-Agent",
50        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
51    );
52    if let Some(value) = &h_accept {
53        builder = builder.header(
54            "Accept",
55            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
56        );
57    }
58    Ok(builder)
59}
60
61#[cfg(feature = "hyper")]
62pub fn hyper_request(
63    mut builder: ::http::request::Builder,
64    content: Content<::hyper::Body>,
65) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
66{
67    if let Some(content_type) = content.content_type() {
68        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
69    }
70    Ok(builder.body(content.into_body())?)
71}
72
73#[cfg(feature = "hyper")]
74impl From<::hyper::Body> for Content<::hyper::Body> {
75    fn from(body: ::hyper::Body) -> Self {
76        Self::new(body)
77    }
78}
79
80#[cfg(feature = "reqwest")]
81pub fn reqwest_builder(
82    base_url: &str,
83    h_user_agent: &str,
84    h_accept: ::std::option::Option<&str>,
85) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
86    let default_url = concat!("https://api.github.com", "/markdown");
87    let reqwest_url = if base_url.is_empty() {
88        ::reqwest::Url::parse(default_url)?
89    } else {
90        let trimmed = base_url.trim_end_matches('/');
91        let mut url = String::with_capacity(trimmed.len() + 9);
92        url.push_str(trimmed);
93        url.push_str(&default_url[22..]);
94        ::reqwest::Url::parse(&url)?
95    };
96    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
97    let headers = request.headers_mut();
98    headers.append(
99        "User-Agent",
100        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
101    );
102    if let Some(value) = &h_accept {
103        headers.append(
104            "Accept",
105            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
106        );
107    }
108    Ok(request)
109}
110
111#[cfg(feature = "reqwest")]
112pub fn reqwest_request(
113    mut builder: ::reqwest::Request,
114    content: Content<::reqwest::Body>,
115) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
116    if let Some(content_type) = content.content_type() {
117        builder.headers_mut().append(
118            ::reqwest::header::HeaderName::from_static("content-type"),
119            ::reqwest::header::HeaderValue::try_from(content_type)?,
120        );
121    }
122    *builder.body_mut() = Some(content.into_body());
123    Ok(builder)
124}
125
126#[cfg(feature = "reqwest")]
127impl From<::reqwest::Body> for Content<::reqwest::Body> {
128    fn from(body: ::reqwest::Body) -> Self {
129        Self::new(body)
130    }
131}
132
133#[cfg(feature = "reqwest-blocking")]
134pub fn reqwest_blocking_builder(
135    base_url: &str,
136    h_user_agent: &str,
137    h_accept: ::std::option::Option<&str>,
138) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
139    let default_url = concat!("https://api.github.com", "/markdown");
140    let reqwest_url = if base_url.is_empty() {
141        ::reqwest::Url::parse(default_url)?
142    } else {
143        let trimmed = base_url.trim_end_matches('/');
144        let mut url = String::with_capacity(trimmed.len() + 9);
145        url.push_str(trimmed);
146        url.push_str(&default_url[22..]);
147        ::reqwest::Url::parse(&url)?
148    };
149    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
150    let headers = request.headers_mut();
151    headers.append(
152        "User-Agent",
153        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
154    );
155    if let Some(value) = &h_accept {
156        headers.append(
157            "Accept",
158            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
159        );
160    }
161    Ok(request)
162}
163
164#[cfg(feature = "reqwest-blocking")]
165pub fn reqwest_blocking_request(
166    mut builder: ::reqwest::blocking::Request,
167    content: Content<::reqwest::blocking::Body>,
168) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
169    if let Some(content_type) = content.content_type() {
170        builder.headers_mut().append(
171            ::reqwest::header::HeaderName::from_static("content-type"),
172            ::reqwest::header::HeaderValue::try_from(content_type)?,
173        );
174    }
175    *builder.body_mut() = Some(content.into_body());
176    Ok(builder)
177}
178
179#[cfg(feature = "reqwest-blocking")]
180impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
181    fn from(body: ::reqwest::blocking::Body) -> Self {
182        Self::new(body)
183    }
184}
185
186/// Types for body parameter in [`super::markdown_render`]
187pub mod body {
188    #[allow(non_snake_case)]
189    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
190    pub struct Json<'a> {
191        /// The Markdown text to render in HTML.
192        pub text: ::std::borrow::Cow<'a, str>,
193
194        /// The rendering mode. Can be either `markdown` or `gfm`.
195        /// 
196        /// # Example
197        /// 
198        /// ```json
199        /// "markdown"
200        /// ```
201        #[serde(skip_serializing_if = "Option::is_none", default)]
202        pub mode: ::std::option::Option<::std::borrow::Cow<'a, str>>,
203
204        /// The repository context to use when creating references in `gfm` mode.  For example, setting `context` to `octo-org/octo-repo` will change the text `#42` into an HTML link to issue 42 in the `octo-org/octo-repo` repository.
205        #[serde(skip_serializing_if = "Option::is_none", default)]
206        pub context: ::std::option::Option<::std::borrow::Cow<'a, str>>,
207
208        #[serde(flatten)]
209        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
210    }
211
212    #[cfg(feature = "hyper")]
213    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
214        type Error = crate::v1_1_4::ApiError;
215
216        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
217            Ok(
218                Self::new(::serde_json::to_vec(value)?.into())
219                .with_content_type(&b"application/json"[..])
220            )
221        }
222    }
223
224    #[cfg(feature = "reqwest")]
225    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
226        type Error = crate::v1_1_4::ApiError;
227
228        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
229            Ok(
230                Self::new(::serde_json::to_vec(value)?.into())
231                .with_content_type(&b"application/json"[..])
232            )
233        }
234    }
235
236    #[cfg(feature = "reqwest-blocking")]
237    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
238        type Error = crate::v1_1_4::ApiError;
239
240        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
241            Ok(
242                Self::new(::serde_json::to_vec(value)?.into())
243                .with_content_type(&b"application/json"[..])
244            )
245        }
246    }
247}