From 31add378266a3759d38d97e182efbb937c787ae2 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Thu, 3 Jan 2019 17:22:52 +0000 Subject: [PATCH] add basic authentication --- src/client.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index 7c21680..eaf108f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -18,7 +18,10 @@ pub struct Client { _gzip: bool, _timeout: u8, pub uri: String, - pub db_prefix: String + pub db_prefix: String, + + // (username, password) + credentials: Option<(String, Option)> } impl Client { @@ -34,10 +37,16 @@ impl Client { _gzip: true, _timeout: 4, dbs: Vec::new(), - db_prefix: String::new() + db_prefix: String::new(), + credentials: None, }) } + pub fn with_basic_auth>(mut self, username: S, password: Option) -> Self { + self.credentials = Some((username.into(), password.map(S::into))); + self + } + fn create_client(&self) -> Result { let client = reqwest::Client::builder() .gzip(self._gzip) @@ -172,6 +181,10 @@ impl Client { req.header(reqwest::header::Referer::new(uri.clone())); req.header(reqwest::header::ContentType::json()); + if let Some((ref username, ref password)) = self.credentials { + req.basic_auth(username.clone(), password.clone()); + } + Ok(req) }