use reqwest::blocking::{
RequestBuilder
};
#[cfg(feature = "async")]
use reqwest::{
RequestBuilder as ARequestBuilder
};
use reqwest::{
header::{
HeaderMap,
USER_AGENT, AUTHORIZATION, CONTENT_TYPE, CONTENT_LENGTH,
HeaderValue,
},
Url,
Method,
};
use crate::error::{Result, Error};
use crate::http::Client;
use crate::constants::USER_AGENT as B_API_USER_AGENT;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request<'a> {
pub body: Option<&'a [u8]>,
pub headers: Option<HeaderMap>,
pub endpoint: String,
pub method: Method,
}
impl<'a> Default for Request<'a> {
fn default() -> Request<'a> {
Request {
body: None,
headers: None,
endpoint: String::from(""),
method: Method::GET,
}
}
}
impl<'a> Request<'a> {
pub fn build(&'a self, client: &Client) -> Result<RequestBuilder> {
let Request {
body,
headers: ref r_headers,
endpoint: ref r_endpoint,
ref method,
} = *self;
let mut builder = client.inner.request(
method.clone(),
Url::parse(r_endpoint).map_err(Error::Url)?,
);
if let Some(ref bytes) = body { let b_vec = Vec::from(*bytes);
builder = builder.body(b_vec);
}
let key = &client.auth_key;
let key = if key.starts_with("Bearer ") {
key.clone()
} else {
format!("Bearer {}", key)
};
let mut headers = HeaderMap::with_capacity(3);
headers.insert(USER_AGENT, HeaderValue::from_static(&B_API_USER_AGENT));
headers.insert(AUTHORIZATION,
HeaderValue::from_str(&key).map_err(Error::Authorization)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_static(&"application/json"));
headers.insert(CONTENT_LENGTH, HeaderValue::from_static(&"0"));
if let Some(ref r_headers) = r_headers {
headers.extend(r_headers.clone());
}
builder = builder.headers(headers);
Ok(builder)
}
#[cfg(feature = "async")]
pub fn a_build(&'a self, client: &Client) -> Result<ARequestBuilder> {
let Request {
body,
headers: ref r_headers,
endpoint: ref r_endpoint,
ref method,
} = *self;
let mut builder = client.a_inner.request(
method.clone(),
Url::parse(r_endpoint).map_err(Error::Url)?,
);
if let Some(ref bytes) = body { let b_vec = Vec::from(*bytes);
builder = builder.body(b_vec);
}
let key = &client.auth_key;
let key = if key.starts_with("Bearer ") {
key.clone()
} else {
format!("Bearer {}", key) };
let mut headers = HeaderMap::with_capacity(3);
headers.insert(USER_AGENT, HeaderValue::from_static(&B_API_USER_AGENT));
headers.insert(AUTHORIZATION,
HeaderValue::from_str(&key).map_err(Error::Authorization)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_static(&"application/json"));
headers.insert(CONTENT_LENGTH, HeaderValue::from_static(&"0"));
if let Some(ref r_headers) = r_headers {
headers.extend(r_headers.clone());
}
builder = builder.headers(headers);
Ok(builder)
}
}