Skip to content

Commit

Permalink
Merge pull request #32 from dongri/refactoring-minreq-header
Browse files Browse the repository at this point in the history
Refactoring http request headers
  • Loading branch information
Dongri Jin authored Aug 27, 2023
2 parents e0e6a92 + 545d6ad commit 036b850
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openai-api-rs"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
authors = ["Dongri Jin <[email protected]>"]
license = "MIT"
Expand Down
40 changes: 13 additions & 27 deletions src/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: serde::ser::Serialize>(
&self,
path: &str,
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 036b850

Please sign in to comment.