Skip to content

Commit 6ea6beb

Browse files
committed
preserve query parameters from the url
1 parent 6b2577d commit 6ea6beb

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ features = ["connect"]
4040
[dependencies.futures-util]
4141
version = "0.3.31"
4242
features = ["sink", "std"]
43+
44+
[dependencies.url]
45+
version = "2.5.4"

src/v1/api.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use reqwest::multipart::{Form, Part};
4343
use reqwest::{Client, Method, Response};
4444
use serde::Serialize;
4545
use serde_json::Value;
46+
use url::Url;
4647

4748
use std::error::Error;
4849
use std::fs::{create_dir_all, File};
@@ -56,7 +57,6 @@ const API_URL_V1: &str = "https://api.openai.com/v1";
5657
pub struct OpenAIClientBuilder {
5758
api_endpoint: Option<String>,
5859
api_key: Option<String>,
59-
api_version: Option<String>,
6060
organization: Option<String>,
6161
proxy: Option<String>,
6262
timeout: Option<u64>,
@@ -67,7 +67,6 @@ pub struct OpenAIClientBuilder {
6767
pub struct OpenAIClient {
6868
api_endpoint: String,
6969
api_key: Option<String>,
70-
api_version: Option<String>,
7170
organization: Option<String>,
7271
proxy: Option<String>,
7372
timeout: Option<u64>,
@@ -84,11 +83,6 @@ impl OpenAIClientBuilder {
8483
self
8584
}
8685

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-
9286
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
9387
self.api_endpoint = Some(endpoint.into());
9488
self
@@ -125,7 +119,6 @@ impl OpenAIClientBuilder {
125119

126120
Ok(OpenAIClient {
127121
api_endpoint,
128-
api_version: self.api_version,
129122
api_key: self.api_key,
130123
organization: self.organization,
131124
proxy: self.proxy,
@@ -141,11 +134,9 @@ impl OpenAIClient {
141134
}
142135

143136
async fn build_request(&self, method: Method, path: &str) -> reqwest::RequestBuilder {
144-
let mut url = format!("{}/{}", self.api_endpoint, path);
145-
146-
if let Some(api_version) = &self.api_version {
147-
url = format!("{}?api-version={}", url, api_version);
148-
}
137+
let url = self
138+
.build_url_with_preserved_query(path)
139+
.unwrap_or_else(|_| format!("{}/{}", self.api_endpoint, path));
149140

150141
let client = Client::builder();
151142

@@ -790,7 +781,18 @@ impl OpenAIClient {
790781
let url = Self::query_params(limit, None, after, None, "batches".to_string());
791782
self.get(&url).await
792783
}
784+
fn build_url_with_preserved_query(&self, path: &str) -> Result<String, url::ParseError> {
785+
let base = Url::parse(&self.api_endpoint)?;
786+
let mut url = base.join(path)?;
793787

788+
if let Some(q) = base.query() {
789+
for (k, v) in url::form_urlencoded::parse(q.as_bytes()) {
790+
url.query_pairs_mut().append_pair(&k, &v);
791+
}
792+
}
793+
794+
Ok(url.to_string())
795+
}
794796
fn query_params(
795797
limit: Option<i64>,
796798
order: Option<String>,

0 commit comments

Comments
 (0)