pub struct Content<Body>
{
body: Body,
content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
}
impl<Body> Content<Body> {
pub fn new(body: Body) -> Self {
Self { body, content_type_value: None }
}
#[must_use]
pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
self.content_type_value = Some(content_type.into());
self
}
fn content_type(&self) -> Option<&[u8]> {
self.content_type_value.as_deref()
}
fn into_body(self) -> Body {
self.body
}
}
#[cfg(feature = "hyper")]
pub fn http_builder(
base_url: &str,
h_user_agent: &str,
h_accept: ::std::option::Option<&str>,
) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
let default_url = concat!("https://api.github.com", "/markdown/raw");
let url = if base_url.is_empty() {
::http::uri::Uri::from_static(default_url)
} else {
let trimmed = base_url.trim_end_matches('/');
let mut url = String::with_capacity(trimmed.len() + 13);
url.push_str(trimmed);
url.push_str(&default_url[22..]);
url.try_into().map_err(::http::Error::from)?
};
let mut builder = ::http::request::Request::post(url);
builder = builder.header(
"User-Agent",
&::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
);
if let Some(value) = &h_accept {
builder = builder.header(
"Accept",
&::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
);
}
Ok(builder)
}
#[cfg(feature = "hyper")]
pub fn hyper_request(
mut builder: ::http::request::Builder,
content: Content<::hyper::Body>,
) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
{
if let Some(content_type) = content.content_type() {
builder = builder.header(::http::header::CONTENT_TYPE, content_type);
}
Ok(builder.body(content.into_body())?)
}
#[cfg(feature = "hyper")]
impl From<::hyper::Body> for Content<::hyper::Body> {
fn from(body: ::hyper::Body) -> Self {
Self::new(body)
}
}
#[cfg(feature = "reqwest")]
pub fn reqwest_builder(
base_url: &str,
h_user_agent: &str,
h_accept: ::std::option::Option<&str>,
) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
let default_url = concat!("https://api.github.com", "/markdown/raw");
let reqwest_url = if base_url.is_empty() {
::reqwest::Url::parse(default_url)?
} else {
let trimmed = base_url.trim_end_matches('/');
let mut url = String::with_capacity(trimmed.len() + 13);
url.push_str(trimmed);
url.push_str(&default_url[22..]);
::reqwest::Url::parse(&url)?
};
let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
let headers = request.headers_mut();
headers.append(
"User-Agent",
::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
);
if let Some(value) = &h_accept {
headers.append(
"Accept",
::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
);
}
Ok(request)
}
#[cfg(feature = "reqwest")]
pub fn reqwest_request(
mut builder: ::reqwest::Request,
content: Content<::reqwest::Body>,
) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
if let Some(content_type) = content.content_type() {
builder.headers_mut().append(
::reqwest::header::HeaderName::from_static("content-type"),
::reqwest::header::HeaderValue::try_from(content_type)?,
);
}
*builder.body_mut() = Some(content.into_body());
Ok(builder)
}
#[cfg(feature = "reqwest")]
impl From<::reqwest::Body> for Content<::reqwest::Body> {
fn from(body: ::reqwest::Body) -> Self {
Self::new(body)
}
}
#[cfg(feature = "reqwest-blocking")]
pub fn reqwest_blocking_builder(
base_url: &str,
h_user_agent: &str,
h_accept: ::std::option::Option<&str>,
) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
let default_url = concat!("https://api.github.com", "/markdown/raw");
let reqwest_url = if base_url.is_empty() {
::reqwest::Url::parse(default_url)?
} else {
let trimmed = base_url.trim_end_matches('/');
let mut url = String::with_capacity(trimmed.len() + 13);
url.push_str(trimmed);
url.push_str(&default_url[22..]);
::reqwest::Url::parse(&url)?
};
let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
let headers = request.headers_mut();
headers.append(
"User-Agent",
::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
);
if let Some(value) = &h_accept {
headers.append(
"Accept",
::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
);
}
Ok(request)
}
#[cfg(feature = "reqwest-blocking")]
pub fn reqwest_blocking_request(
mut builder: ::reqwest::blocking::Request,
content: Content<::reqwest::blocking::Body>,
) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
if let Some(content_type) = content.content_type() {
builder.headers_mut().append(
::reqwest::header::HeaderName::from_static("content-type"),
::reqwest::header::HeaderValue::try_from(content_type)?,
);
}
*builder.body_mut() = Some(content.into_body());
Ok(builder)
}
#[cfg(feature = "reqwest-blocking")]
impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
fn from(body: ::reqwest::blocking::Body) -> Self {
Self::new(body)
}
}
pub mod body {
#[cfg(feature = "hyper")]
impl<'a> TryFrom<&::std::borrow::Cow<'static, str>> for super::Content<::hyper::Body> {
type Error = crate::v1_1_4::ApiError;
fn try_from(value: &::std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
Ok(
Self::new(value.as_bytes().to_owned().into())
.with_content_type(&b"text/plain"[..])
)
}
}
#[cfg(feature = "reqwest")]
impl<'a> TryFrom<&::std::borrow::Cow<'static, str>> for super::Content<::reqwest::Body> {
type Error = crate::v1_1_4::ApiError;
fn try_from(value: &::std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
Ok(
Self::new(value.as_bytes().to_owned().into())
.with_content_type(&b"text/plain"[..])
)
}
}
#[cfg(feature = "reqwest-blocking")]
impl<'a> TryFrom<&::std::borrow::Cow<'static, str>> for super::Content<::reqwest::blocking::Body> {
type Error = crate::v1_1_4::ApiError;
fn try_from(value: &::std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
Ok(
Self::new(value.as_bytes().to_owned().into())
.with_content_type(&b"text/plain"[..])
)
}
}
}