diff --git a/Cargo.toml b/Cargo.toml index 98cdff7..db9e502 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openai-api-rs" -version = "1.0.0" +version = "1.0.1" edition = "2021" authors = ["Dongri Jin "] license = "MIT" diff --git a/src/v1/api.rs b/src/v1/api.rs index 44fc2f0..a4f5c95 100644 --- a/src/v1/api.rs +++ b/src/v1/api.rs @@ -57,6 +57,16 @@ impl Client { } } + pub fn build_request(&self, request: minreq::Request) -> minreq::Request { + let mut request = request + .with_header("Content-Type", "application/json") + .with_header("Authorization", format!("Bearer {}", self.api_key)); + if let Some(organization) = &self.organization { + request = request.with_header("openai-organization", organization); + } + request + } + pub fn post( &self, path: &str, @@ -68,16 +78,8 @@ impl Client { path = path ); - let mut request = minreq::post(url) - .with_header("Content-Type", "application/json") - .with_header("Authorization", format!("Bearer {}", self.api_key)); - - if let Some(organization) = &self.organization { - request = request.with_header("openai-organization", organization); - } - + let request = self.build_request(minreq::post(url)); let res = request.with_json(params).unwrap().send(); - match res { Ok(res) => { if (200..=299).contains(&res.status_code) { @@ -99,16 +101,8 @@ impl Client { path = path ); - let mut request = minreq::get(url) - .with_header("Content-Type", "application/json") - .with_header("Authorization", format!("Bearer {}", self.api_key)); - - if let Some(organization) = &self.organization { - request = request.with_header("openai-organization", organization); - } - + let request = self.build_request(minreq::get(url)); let res = request.send(); - match res { Ok(res) => { if (200..=299).contains(&res.status_code) { @@ -130,16 +124,8 @@ impl Client { path = path ); - let mut request = minreq::delete(url) - .with_header("Content-Type", "application/json") - .with_header("Authorization", format!("Bearer {}", self.api_key)); - - if let Some(organization) = &self.organization { - request = request.with_header("openai-organization", organization); - } - + let request = self.build_request(minreq::delete(url)); let res = request.send(); - match res { Ok(res) => { if (200..=299).contains(&res.status_code) {