Skip to content

Commit 67f5b49

Browse files
authored
Optional auth header and with api keys #157 (#1)
* made key optional * with api version
1 parent 19458d8 commit 67f5b49

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openai-api-rs"
3-
version = "6.0.4"
3+
version = "6.0.3"
44
edition = "2021"
55
authors = ["Dongri Jin <dongrium@gmail.com>"]
66
license = "MIT"
@@ -14,8 +14,6 @@ default = ["default-tls"]
1414
rustls = ["reqwest/rustls-tls", "tokio-tungstenite/rustls-tls-webpki-roots"]
1515
default-tls = ["reqwest/default-tls", "tokio-tungstenite/native-tls"]
1616

17-
[dependencies]
18-
tracing = "0.1.41"
1917

2018
[dependencies.reqwest]
2119
version = "0.12"

src/v1/api.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const API_URL_V1: &str = "https://api.openai.com/v1";
5656
pub struct OpenAIClientBuilder {
5757
api_endpoint: Option<String>,
5858
api_key: Option<String>,
59+
api_version: Option<String>,
5960
organization: Option<String>,
6061
proxy: Option<String>,
6162
timeout: Option<u64>,
@@ -65,7 +66,8 @@ pub struct OpenAIClientBuilder {
6566
#[derive(Debug)]
6667
pub struct OpenAIClient {
6768
api_endpoint: String,
68-
api_key: String,
69+
api_key: Option<String>,
70+
api_version: Option<String>,
6971
organization: Option<String>,
7072
proxy: Option<String>,
7173
timeout: Option<u64>,
@@ -82,6 +84,11 @@ impl OpenAIClientBuilder {
8284
self
8385
}
8486

87+
pub fn with_api_version(mut self, api_version: impl Into<String>) -> Self {
88+
self.api_version = Some(api_version.into());
89+
self
90+
}
91+
8592
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
8693
self.api_endpoint = Some(endpoint.into());
8794
self
@@ -112,14 +119,14 @@ impl OpenAIClientBuilder {
112119
}
113120

114121
pub fn build(self) -> Result<OpenAIClient, Box<dyn Error>> {
115-
let api_key = self.api_key.ok_or("API key is required")?;
116122
let api_endpoint = self.api_endpoint.unwrap_or_else(|| {
117123
std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned())
118124
});
119125

120126
Ok(OpenAIClient {
121127
api_endpoint,
122-
api_key,
128+
api_version: self.api_version,
129+
api_key: self.api_key,
123130
organization: self.organization,
124131
proxy: self.proxy,
125132
timeout: self.timeout,
@@ -134,7 +141,13 @@ impl OpenAIClient {
134141
}
135142

136143
async fn build_request(&self, method: Method, path: &str) -> reqwest::RequestBuilder {
137-
let url = format!("{}/{}", self.api_endpoint, path);
144+
let url = format!(
145+
"{}/{}?api-version={}",
146+
self.api_endpoint,
147+
path,
148+
self.api_version.as_deref().unwrap_or("v1")
149+
);
150+
138151
let client = Client::builder();
139152

140153
#[cfg(feature = "rustls")]
@@ -154,9 +167,14 @@ impl OpenAIClient {
154167

155168
let client = client.build().unwrap();
156169

157-
let mut request = client
158-
.request(method, url)
159-
.header("Authorization", format!("Bearer {}", self.api_key));
170+
let mut request = client.request(method, url);
171+
172+
if self.api_key.is_some() {
173+
request = request.header(
174+
"Authorization",
175+
format!("Bearer {}", self.api_key.as_ref().unwrap()),
176+
);
177+
}
160178

161179
if let Some(organization) = &self.organization {
162180
request = request.header("openai-organization", organization);
@@ -180,20 +198,9 @@ impl OpenAIClient {
180198
path: &str,
181199
body: &impl serde::ser::Serialize,
182200
) -> Result<T, APIError> {
183-
let request_builder = self.build_request(Method::POST, path).await;
184-
let request_builder = request_builder.json(body);
185-
186-
// 💡 Convert to request to inspect it before sending
187-
let client = request_builder
188-
.try_clone()
189-
.expect("Cannot clone request builder")
190-
.build()
191-
.expect("Failed to build request");
192-
193-
// 🔍 Debug log: URL, headers, and optionally body
194-
tracing::debug!("🔵 URL: {}", client.url());
195-
tracing::debug!("🟢 Headers:\n{:#?}", client.headers());
196-
let response = request_builder.send().await?;
201+
let request = self.build_request(Method::POST, path).await;
202+
let request = request.json(body);
203+
let response = request.send().await?;
197204
self.handle_response(response).await
198205
}
199206

0 commit comments

Comments
 (0)