Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>)>
}

impl Client {
Expand All @@ -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<S: Into<String>>(mut self, username: S, password: Option<S>) -> Self {
self.credentials = Some((username.into(), password.map(S::into)));
self
}

fn create_client(&self) -> Result<reqwest::Client, Error> {
let client = reqwest::Client::builder()
.gzip(self._gzip)
Expand Down Expand Up @@ -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)
}

Expand Down