Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
exposed HttpProxy
Browse files Browse the repository at this point in the history
this allows users of library to proxy authz http requests while reusing
client's settings
  • Loading branch information
khodzha committed Apr 13, 2021
1 parent 07662e1 commit 6e9c090
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ trait Authorize: Sync + Send {
seconds: usize,
) -> Result<(), Error>;

fn http_proxy(&self) -> Option<HttpProxy>;

fn box_clone(&self) -> Box<dyn Authorize>;
}

Expand Down Expand Up @@ -174,6 +176,14 @@ impl ClientMap {
Ok(())
}
}

pub fn http_proxy(&self, audience: &str) -> Option<HttpProxy> {
if let Some(client) = self.inner.get(audience) {
client.http_proxy()
} else {
None
}
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -223,6 +233,10 @@ impl Authorize for NoneClient {
fn box_clone(&self) -> Box<dyn Authorize> {
Box::new(self.clone())
}

fn http_proxy(&self) -> Option<HttpProxy> {
None
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -289,6 +303,10 @@ impl Authorize for LocalClient {
fn box_clone(&self) -> Box<dyn Authorize> {
Box::new(self.clone())
}

fn http_proxy(&self) -> Option<HttpProxy> {
None
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -679,6 +697,13 @@ impl Authorize for HttpClient {
Ok(())
}

fn http_proxy(&self) -> Option<HttpProxy> {
Some(HttpProxy {
client: self.client.clone(),
url: self.uri.clone(),
})
}

fn box_clone(&self) -> Box<dyn Authorize> {
Box::new(self.clone())
}
Expand Down Expand Up @@ -844,6 +869,29 @@ impl Authorize for LocalWhitelistClient {
fn box_clone(&self) -> Box<dyn Authorize> {
Box::new(self.clone())
}

fn http_proxy(&self) -> Option<HttpProxy> {
None
}
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone)]
pub struct HttpProxy {
client: Arc<isahc::HttpClient>,
url: String,
}

impl HttpProxy {
pub async fn send_async<T: Into<isahc::AsyncBody>>(
&self,
payload: T,
) -> Result<isahc::Response<isahc::AsyncBody>, isahc::Error> {
let request = isahc::Request::post(&self.url).body(payload)?;

self.client.send_async(request).await
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 6e9c090

Please sign in to comment.