ClientBuilder implementation.
parent
4674fb3a39
commit
76fe0331a2
|
@ -53,25 +53,47 @@ pub struct Client {
|
||||||
pub(crate) ws: tokio::sync::Mutex<Option<crate::client_ws::WsStream>>,
|
pub(crate) ws: tokio::sync::Mutex<Option<crate::client_ws::WsStream>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
pub struct ClientBuilder {
|
||||||
pub async fn connect(url: &str, credentials: impl Into<Credentials>) -> crate::Result<Self> {
|
credentials: Option<Credentials>,
|
||||||
Self::connect_(url, credentials, None::<Vec<String>>).await
|
trusted_hosts: AHashSet<String>,
|
||||||
|
timeout: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_with_trusted(
|
impl Default for ClientBuilder {
|
||||||
url: &str,
|
fn default() -> Self {
|
||||||
credentials: impl Into<Credentials>,
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClientBuilder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
credentials: None,
|
||||||
|
trusted_hosts: AHashSet::new(),
|
||||||
|
timeout: DEFAULT_TIMEOUT_MS,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn credentials(mut self, credentials: impl Into<Credentials>) -> Self {
|
||||||
|
self.credentials = Some(credentials.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn timeout(mut self, timeout: u64) -> Self {
|
||||||
|
self.timeout = timeout;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn follow_redirects(
|
||||||
|
mut self,
|
||||||
trusted_hosts: impl IntoIterator<Item = impl Into<String>>,
|
trusted_hosts: impl IntoIterator<Item = impl Into<String>>,
|
||||||
) -> crate::Result<Self> {
|
) -> Self {
|
||||||
Self::connect_(url, credentials, trusted_hosts.into()).await
|
self.trusted_hosts = trusted_hosts.into_iter().map(|h| h.into()).collect();
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect_(
|
pub async fn connect(self, url: &str) -> crate::Result<Client> {
|
||||||
url: &str,
|
let authorization = match self.credentials.expect("Missing credentials") {
|
||||||
credentials: impl Into<Credentials>,
|
|
||||||
trusted_hosts: Option<impl IntoIterator<Item = impl Into<String>>>,
|
|
||||||
) -> crate::Result<Self> {
|
|
||||||
let authorization = match credentials.into() {
|
|
||||||
Credentials::Basic(s) => format!("Basic {}", s),
|
Credentials::Basic(s) => format!("Basic {}", s),
|
||||||
Credentials::Bearer(s) => format!("Bearer {}", s),
|
Credentials::Bearer(s) => format!("Bearer {}", s),
|
||||||
};
|
};
|
||||||
|
@ -85,17 +107,13 @@ impl Client {
|
||||||
header::HeaderValue::from_str(&authorization).unwrap(),
|
header::HeaderValue::from_str(&authorization).unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let trusted_hosts = Arc::new(
|
let trusted_hosts = Arc::new(self.trusted_hosts);
|
||||||
trusted_hosts
|
|
||||||
.map(|hosts| hosts.into_iter().map(|h| h.into()).collect::<AHashSet<_>>())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let trusted_hosts_ = trusted_hosts.clone();
|
let trusted_hosts_ = trusted_hosts.clone();
|
||||||
let session: Session = serde_json::from_slice(
|
let session: Session = serde_json::from_slice(
|
||||||
&Client::handle_error(
|
&Client::handle_error(
|
||||||
reqwest::Client::builder()
|
reqwest::Client::builder()
|
||||||
.timeout(Duration::from_millis(DEFAULT_TIMEOUT_MS))
|
.timeout(Duration::from_millis(self.timeout))
|
||||||
.redirect(redirect::Policy::custom(move |attempt| {
|
.redirect(redirect::Policy::custom(move |attempt| {
|
||||||
if attempt.previous().len() > 5 {
|
if attempt.previous().len() > 5 {
|
||||||
attempt.error("Too many redirects.")
|
attempt.error("Too many redirects.")
|
||||||
|
@ -143,20 +161,27 @@ impl Client {
|
||||||
trusted_hosts,
|
trusted_hosts,
|
||||||
#[cfg(feature = "websockets")]
|
#[cfg(feature = "websockets")]
|
||||||
authorization,
|
authorization,
|
||||||
timeout: DEFAULT_TIMEOUT_MS,
|
timeout: self.timeout,
|
||||||
headers,
|
headers,
|
||||||
default_account_id,
|
default_account_id,
|
||||||
#[cfg(feature = "websockets")]
|
#[cfg(feature = "websockets")]
|
||||||
ws: None.into(),
|
ws: None.into(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client {
|
||||||
|
#[allow(clippy::new_ret_no_self)]
|
||||||
|
pub fn new() -> ClientBuilder {
|
||||||
|
ClientBuilder::new()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_timeout(&mut self, timeout: u64) -> &mut Self {
|
pub fn set_timeout(&mut self, timeout: u64) -> &mut Self {
|
||||||
self.timeout = timeout;
|
self.timeout = timeout;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_trusted_hosts(
|
pub fn set_follow_redirects(
|
||||||
&mut self,
|
&mut self,
|
||||||
trusted_hosts: impl IntoIterator<Item = impl Into<String>>,
|
trusted_hosts: impl IntoIterator<Item = impl Into<String>>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
|
|
Loading…
Reference in New Issue