Skip to content

Commit

Permalink
feat(kmkc): support proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
noaione committed Jan 11, 2024
1 parent 1c191b3 commit 85cb491
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions tosho_kmkc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ impl KMClient {
/// # Arguments
/// * `config` - The config to use for the client
pub fn new(config: KMConfig) -> Self {
Self::make_client(config, None)
}

/// Attach a proxy to the client.
///
/// This will clone the client and return a new client with the proxy attached.
///
/// # Arguments
/// * `proxy` - The proxy to attach to the client
pub fn with_proxy(&self, proxy: reqwest::Proxy) -> Self {
Self::make_client(self.config.clone(), Some(proxy))
}

/// Internal function to create new client.
fn make_client(config: KMConfig, proxy: Option<reqwest::Proxy>) -> Self {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::ACCEPT,
Expand All @@ -81,9 +96,12 @@ impl KMClient {
// make cookie store
let client = reqwest::Client::builder()
.default_headers(headers)
.cookie_provider(std::sync::Arc::clone(&cookie_store))
.build()
.unwrap();
.cookie_provider(std::sync::Arc::clone(&cookie_store));

let client = match proxy {
Some(proxy) => client.proxy(proxy).build().unwrap(),
None => client.build().unwrap(),
};

Self {
inner: client,
Expand All @@ -104,9 +122,12 @@ impl KMClient {

let client = reqwest::Client::builder()
.default_headers(headers)
.cookie_provider(std::sync::Arc::clone(&cookie_store))
.build()
.unwrap();
.cookie_provider(std::sync::Arc::clone(&cookie_store));

let client = match proxy {
Some(proxy) => client.proxy(proxy).build().unwrap(),
None => client.build().unwrap(),
};

Self {
inner: client,
Expand Down

0 comments on commit 85cb491

Please sign in to comment.