use core::fmt;
use reqwest::RequestBuilder;
use std::fmt::Display;
pub trait ReqwestExt: Sized {
fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
where
U: fmt::Display,
P: fmt::Display;
#[inline]
fn basic_auth_ext<U, P>(self, username: U, password: Option<P>) -> Self
where
U: fmt::Display,
P: fmt::Display,
{
self.basic_auth(username, password)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl ReqwestExt for RequestBuilder {
fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
where
U: Display,
P: Display,
{
self.basic_auth(username, password)
}
}
#[cfg(target_arch = "wasm32")]
impl ReqwestExt for RequestBuilder {
fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
where
U: Display,
P: Display,
{
use base64::write::EncoderWriter as Base64Encoder;
use std::io::Write;
let mut header_value = b"Basic ".to_vec();
{
let mut encoder = Base64Encoder::new(&mut header_value, base64::STANDARD);
write!(encoder, "{}:", username).unwrap();
if let Some(password) = password {
write!(encoder, "{}", password).unwrap();
}
}
self.header(reqwest::header::AUTHORIZATION, header_value)
}
}
#[cfg(test)]
mod test {
use reqwest::Client;
#[test]
fn test() {
#[cfg(target_arch = "wasm32")]
use super::ReqwestExt;
let client = Client::new();
let _req = client
.get("http://localhost")
.basic_auth("foo", Some("bar"));
}
#[test]
fn test_ext() {
use super::ReqwestExt;
let client = Client::new();
let _req = client
.get("http://localhost")
.basic_auth_ext("foo", Some("bar"));
}
}