jinxapi_github/v1_1_4/request/
markdown_render_raw.rs1pub struct Content<Body>
8{
9 body: Body,
10 content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
11}
12
13impl<Body> Content<Body> {
14 pub fn new(body: Body) -> Self {
15 Self { body, content_type_value: None }
16 }
17
18 #[must_use]
19 pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
20 self.content_type_value = Some(content_type.into());
21 self
22 }
23
24 fn content_type(&self) -> Option<&[u8]> {
25 self.content_type_value.as_deref()
26 }
27
28 fn into_body(self) -> Body {
29 self.body
30 }
31}
32
33#[cfg(feature = "hyper")]
34pub fn http_builder(
35 base_url: &str,
36 h_user_agent: &str,
37 h_accept: ::std::option::Option<&str>,
38) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
39 let default_url = concat!("https://api.github.com", "/markdown/raw");
40 let url = if base_url.is_empty() {
41 ::http::uri::Uri::from_static(default_url)
42 } else {
43 let trimmed = base_url.trim_end_matches('/');
44 let mut url = String::with_capacity(trimmed.len() + 13);
45 url.push_str(trimmed);
46 url.push_str(&default_url[22..]);
47 url.try_into().map_err(::http::Error::from)?
48 };
49 let mut builder = ::http::request::Request::post(url);
50 builder = builder.header(
51 "User-Agent",
52 &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
53 );
54 if let Some(value) = &h_accept {
55 builder = builder.header(
56 "Accept",
57 &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
58 );
59 }
60 Ok(builder)
61}
62
63#[cfg(feature = "hyper")]
64pub fn hyper_request(
65 mut builder: ::http::request::Builder,
66 content: Content<::hyper::Body>,
67) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
68{
69 if let Some(content_type) = content.content_type() {
70 builder = builder.header(::http::header::CONTENT_TYPE, content_type);
71 }
72 Ok(builder.body(content.into_body())?)
73}
74
75#[cfg(feature = "hyper")]
76impl From<::hyper::Body> for Content<::hyper::Body> {
77 fn from(body: ::hyper::Body) -> Self {
78 Self::new(body)
79 }
80}
81
82#[cfg(feature = "reqwest")]
83pub fn reqwest_builder(
84 base_url: &str,
85 h_user_agent: &str,
86 h_accept: ::std::option::Option<&str>,
87) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
88 let default_url = concat!("https://api.github.com", "/markdown/raw");
89 let reqwest_url = if base_url.is_empty() {
90 ::reqwest::Url::parse(default_url)?
91 } else {
92 let trimmed = base_url.trim_end_matches('/');
93 let mut url = String::with_capacity(trimmed.len() + 13);
94 url.push_str(trimmed);
95 url.push_str(&default_url[22..]);
96 ::reqwest::Url::parse(&url)?
97 };
98 let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
99 let headers = request.headers_mut();
100 headers.append(
101 "User-Agent",
102 ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
103 );
104 if let Some(value) = &h_accept {
105 headers.append(
106 "Accept",
107 ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
108 );
109 }
110 Ok(request)
111}
112
113#[cfg(feature = "reqwest")]
114pub fn reqwest_request(
115 mut builder: ::reqwest::Request,
116 content: Content<::reqwest::Body>,
117) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
118 if let Some(content_type) = content.content_type() {
119 builder.headers_mut().append(
120 ::reqwest::header::HeaderName::from_static("content-type"),
121 ::reqwest::header::HeaderValue::try_from(content_type)?,
122 );
123 }
124 *builder.body_mut() = Some(content.into_body());
125 Ok(builder)
126}
127
128#[cfg(feature = "reqwest")]
129impl From<::reqwest::Body> for Content<::reqwest::Body> {
130 fn from(body: ::reqwest::Body) -> Self {
131 Self::new(body)
132 }
133}
134
135#[cfg(feature = "reqwest-blocking")]
136pub fn reqwest_blocking_builder(
137 base_url: &str,
138 h_user_agent: &str,
139 h_accept: ::std::option::Option<&str>,
140) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
141 let default_url = concat!("https://api.github.com", "/markdown/raw");
142 let reqwest_url = if base_url.is_empty() {
143 ::reqwest::Url::parse(default_url)?
144 } else {
145 let trimmed = base_url.trim_end_matches('/');
146 let mut url = String::with_capacity(trimmed.len() + 13);
147 url.push_str(trimmed);
148 url.push_str(&default_url[22..]);
149 ::reqwest::Url::parse(&url)?
150 };
151 let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
152 let headers = request.headers_mut();
153 headers.append(
154 "User-Agent",
155 ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
156 );
157 if let Some(value) = &h_accept {
158 headers.append(
159 "Accept",
160 ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
161 );
162 }
163 Ok(request)
164}
165
166#[cfg(feature = "reqwest-blocking")]
167pub fn reqwest_blocking_request(
168 mut builder: ::reqwest::blocking::Request,
169 content: Content<::reqwest::blocking::Body>,
170) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
171 if let Some(content_type) = content.content_type() {
172 builder.headers_mut().append(
173 ::reqwest::header::HeaderName::from_static("content-type"),
174 ::reqwest::header::HeaderValue::try_from(content_type)?,
175 );
176 }
177 *builder.body_mut() = Some(content.into_body());
178 Ok(builder)
179}
180
181#[cfg(feature = "reqwest-blocking")]
182impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
183 fn from(body: ::reqwest::blocking::Body) -> Self {
184 Self::new(body)
185 }
186}
187
188pub mod body {
190 #[cfg(feature = "hyper")]
191 impl<'a> TryFrom<&::std::borrow::Cow<'static, str>> for super::Content<::hyper::Body> {
192 type Error = crate::v1_1_4::ApiError;
193
194 fn try_from(value: &::std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
195 Ok(
196 Self::new(value.as_bytes().to_owned().into())
197 .with_content_type(&b"text/plain"[..])
198 )
199 }
200 }
201
202 #[cfg(feature = "reqwest")]
203 impl<'a> TryFrom<&::std::borrow::Cow<'static, str>> for super::Content<::reqwest::Body> {
204 type Error = crate::v1_1_4::ApiError;
205
206 fn try_from(value: &::std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
207 Ok(
208 Self::new(value.as_bytes().to_owned().into())
209 .with_content_type(&b"text/plain"[..])
210 )
211 }
212 }
213
214 #[cfg(feature = "reqwest-blocking")]
215 impl<'a> TryFrom<&::std::borrow::Cow<'static, str>> for super::Content<::reqwest::blocking::Body> {
216 type Error = crate::v1_1_4::ApiError;
217
218 fn try_from(value: &::std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
219 Ok(
220 Self::new(value.as_bytes().to_owned().into())
221 .with_content_type(&b"text/plain"[..])
222 )
223 }
224 }
225}