use reqwest::ClientBuilder;
use reqwest::header::{HeaderMap, HeaderValue};
use url::Url;
use crate::api::RestApi;
use crate::api::error::BuildError;
use crate::api::types::{API_KEY_HEADER, DEFAULT_PRO_URL, DEFAULT_URL, DEFAULT_USER_AGENT};
pub struct RestApiBuilder {
url: Option<String>,
user_agent: String,
api_key: Option<String>,
}
impl RestApiBuilder {
pub fn new<T, U, V>(url: Option<T>, user_agent: U, api_key: Option<V>) -> Self
where
T: Into<String>,
U: Into<String>,
V: Into<String>,
{
RestApiBuilder {
url: url.map(|v| v.into()),
user_agent: user_agent.into(),
api_key: api_key.map(Into::into),
}
}
pub fn with_url<T: Into<String>>(mut self, url: T) -> Self {
self.url = Some(url.into());
self
}
pub fn with_api_key<T: Into<String>>(mut self, api_key: T) -> Self {
self.api_key = Some(api_key.into());
self
}
pub fn with_user_agent<T: Into<String>>(mut self, user_agent: T) -> Self {
self.user_agent = user_agent.into();
self
}
pub fn build(self) -> Result<RestApi, BuildError> {
let mut headers = HeaderMap::new();
let agent = HeaderValue::from_str(&self.user_agent)?;
headers.insert("User-Agent", agent);
let url = match (&self.url, &self.api_key) {
(Some(url), _) => url,
(None, Some(_)) => DEFAULT_PRO_URL,
(None, None) => DEFAULT_URL,
};
let parsed_url = Url::parse(url)?;
if let Some(key) = &self.api_key {
let mut api_key = HeaderValue::from_str(key)?;
api_key.set_sensitive(true);
headers.insert(API_KEY_HEADER, api_key);
}
let client = ClientBuilder::new().default_headers(headers).build()?;
Ok(RestApi::new(parsed_url, client))
}
}
impl Default for RestApiBuilder {
fn default() -> Self {
RestApiBuilder {
url: None,
api_key: None,
user_agent: DEFAULT_USER_AGENT.into(),
}
}
}