@@ -43,6 +43,7 @@ use reqwest::multipart::{Form, Part};
43
43
use reqwest:: { Client , Method , Response } ;
44
44
use serde:: Serialize ;
45
45
use serde_json:: Value ;
46
+ use url:: Url ;
46
47
47
48
use std:: error:: Error ;
48
49
use std:: fs:: { create_dir_all, File } ;
@@ -56,7 +57,6 @@ const API_URL_V1: &str = "https://api.openai.com/v1";
56
57
pub struct OpenAIClientBuilder {
57
58
api_endpoint : Option < String > ,
58
59
api_key : Option < String > ,
59
- api_version : Option < String > ,
60
60
organization : Option < String > ,
61
61
proxy : Option < String > ,
62
62
timeout : Option < u64 > ,
@@ -67,7 +67,6 @@ pub struct OpenAIClientBuilder {
67
67
pub struct OpenAIClient {
68
68
api_endpoint : String ,
69
69
api_key : Option < String > ,
70
- api_version : Option < String > ,
71
70
organization : Option < String > ,
72
71
proxy : Option < String > ,
73
72
timeout : Option < u64 > ,
@@ -84,11 +83,6 @@ impl OpenAIClientBuilder {
84
83
self
85
84
}
86
85
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
-
92
86
pub fn with_endpoint ( mut self , endpoint : impl Into < String > ) -> Self {
93
87
self . api_endpoint = Some ( endpoint. into ( ) ) ;
94
88
self
@@ -125,7 +119,6 @@ impl OpenAIClientBuilder {
125
119
126
120
Ok ( OpenAIClient {
127
121
api_endpoint,
128
- api_version : self . api_version ,
129
122
api_key : self . api_key ,
130
123
organization : self . organization ,
131
124
proxy : self . proxy ,
@@ -141,11 +134,9 @@ impl OpenAIClient {
141
134
}
142
135
143
136
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) ) ;
149
140
150
141
let client = Client :: builder ( ) ;
151
142
@@ -790,7 +781,18 @@ impl OpenAIClient {
790
781
let url = Self :: query_params ( limit, None , after, None , "batches" . to_string ( ) ) ;
791
782
self . get ( & url) . await
792
783
}
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) ?;
793
787
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
+ }
794
796
fn query_params (
795
797
limit : Option < i64 > ,
796
798
order : Option < String > ,
0 commit comments