diff --git a/Cargo.toml b/Cargo.toml index fc61d18..e7d1775 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ default-features = false [dependencies.minreq] version = "2" default-features = false -features = ["https-rustls", "json-using-serde"] \ No newline at end of file +features = ["https-rustls", "json-using-serde", "proxy"] diff --git a/src/v1/api.rs b/src/v1/api.rs index 1686f23..f188fb5 100644 --- a/src/v1/api.rs +++ b/src/v1/api.rs @@ -47,6 +47,7 @@ pub struct Client { pub api_endpoint: String, pub api_key: String, pub organization: Option, + pub proxy: Option, } impl Client { @@ -60,6 +61,7 @@ impl Client { api_endpoint, api_key, organization: None, + proxy: None, } } @@ -69,6 +71,17 @@ impl Client { api_endpoint: endpoint, api_key, organization: organization.into(), + proxy: None, + } + } + + pub fn new_with_proxy(api_key: String, proxy: String) -> Self { + let endpoint = std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned()); + Self { + api_endpoint: endpoint, + api_key, + organization: None, + proxy: Some(proxy), } } @@ -82,6 +95,9 @@ impl Client { if is_beta { request = request.with_header("OpenAI-Beta", "assistants=v1"); } + if let Some(proxy) = &self.proxy { + request = request.with_proxy(minreq::Proxy::new(proxy).unwrap()); + } request }