Skip to content

Commit ad59163

Browse files
committed
configurable per request path
1 parent cd5c76b commit ad59163

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

async-openai/src/client.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,14 @@ impl<C: Config> Client<C> {
204204
path: &str,
205205
request_options: &RequestOptions,
206206
) -> reqwest::RequestBuilder {
207-
let mut request_builder = self
208-
.http_client
209-
.request(method, self.config.url(path))
207+
let mut request_builder = if let Some(path) = request_options.path() {
208+
self.http_client
209+
.request(method, self.config.url(path.as_str()))
210+
} else {
211+
self.http_client.request(method, self.config.url(path))
212+
};
213+
214+
request_builder = request_builder
210215
.query(&self.config.query())
211216
.headers(self.config.headers());
212217

async-openai/src/request_options.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@ use crate::{config::OPENAI_API_BASE, error::OpenAIError};
88
pub struct RequestOptions {
99
query: Option<Vec<(String, String)>>,
1010
headers: Option<HeaderMap>,
11+
path: Option<String>,
1112
}
1213

1314
impl RequestOptions {
1415
pub(crate) fn new() -> Self {
1516
Self {
1617
query: None,
1718
headers: None,
19+
path: None,
1820
}
1921
}
2022

23+
pub(crate) fn with_path(&mut self, path: &str) -> Result<(), OpenAIError> {
24+
if path.is_empty() {
25+
return Err(OpenAIError::InvalidArgument(
26+
"Path cannot be empty".to_string(),
27+
));
28+
}
29+
self.path = Some(path.to_string());
30+
Ok(())
31+
}
32+
2133
pub(crate) fn with_headers(&mut self, headers: HeaderMap) {
2234
// merge with existing headers or update with new headers
2335
if let Some(existing_headers) = &mut self.headers {
@@ -81,4 +93,8 @@ impl RequestOptions {
8193
pub(crate) fn headers(&self) -> Option<&HeaderMap> {
8294
self.headers.as_ref()
8395
}
96+
97+
pub(crate) fn path(&self) -> Option<&String> {
98+
self.path.as_ref()
99+
}
84100
}

async-openai/src/traits.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ pub trait RequestOptionsBuilder: Sized {
5353
self.options_mut().with_query(query)?;
5454
Ok(self)
5555
}
56+
57+
/// Add a path to RequestOptions
58+
fn path<P: Into<String>>(mut self, path: P) -> Result<Self, OpenAIError> {
59+
self.options_mut().with_path(path.into().as_str())?;
60+
Ok(self)
61+
}
5662
}

0 commit comments

Comments
 (0)