use crate::auth::AuthType;
use crate::request::{HttpMethod, NadeoRequest};
use crate::{Error, Result};
use reqwest::header::{HeaderMap, IntoHeaderName};
use serde::{Deserialize, Serialize};
pub struct NadeoRequestBuilder {
auth_type: Option<AuthType>,
url: Option<String>,
method: Option<HttpMethod>,
headers: HeaderMap,
}
impl Default for NadeoRequestBuilder {
fn default() -> Self {
NadeoRequestBuilder {
auth_type: None,
method: None,
headers: HeaderMap::new(),
url: None,
}
}
}
#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
pub enum RequestBuilderError {
#[error("no URL was provided")]
MissingUrl,
#[error("no HTTP method was provided")]
MissingHttpMethod,
#[error("no AuthType was provided")]
MissingAuthType,
}
impl NadeoRequestBuilder {
pub fn url(mut self, url: &str) -> Self {
self.url = Some(url.to_string());
self
}
pub fn method(mut self, method: HttpMethod) -> Self {
self.method = Some(method);
self
}
pub fn auth_type(mut self, auth_type: AuthType) -> Self {
self.auth_type = Some(auth_type);
self
}
pub fn add_header<K>(mut self, key: K, val: &str) -> Self
where
K: IntoHeaderName,
{
self.headers.insert(key, val.parse().unwrap());
self
}
pub fn build(self) -> Result<NadeoRequest> {
if self.url.is_none() {
return Err(Error::from(RequestBuilderError::MissingUrl));
}
if self.method.is_none() {
return Err(Error::from(RequestBuilderError::MissingHttpMethod));
}
if self.auth_type.is_none() {
return Err(Error::from(RequestBuilderError::MissingAuthType));
}
Ok(NadeoRequest {
auth_type: self.auth_type.unwrap(),
method: self.method.unwrap(),
url: self.url.unwrap(),
headers: self.headers,
})
}
}