Skip to content

Commit 036b850

Browse files
author
Dongri Jin
authored
Merge pull request #32 from dongri/refactoring-minreq-header
Refactoring http request headers
2 parents e0e6a92 + 545d6ad commit 036b850

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openai-api-rs"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
edition = "2021"
55
authors = ["Dongri Jin <[email protected]>"]
66
license = "MIT"

src/v1/api.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ impl Client {
5757
}
5858
}
5959

60+
pub fn build_request(&self, request: minreq::Request) -> minreq::Request {
61+
let mut request = request
62+
.with_header("Content-Type", "application/json")
63+
.with_header("Authorization", format!("Bearer {}", self.api_key));
64+
if let Some(organization) = &self.organization {
65+
request = request.with_header("openai-organization", organization);
66+
}
67+
request
68+
}
69+
6070
pub fn post<T: serde::ser::Serialize>(
6171
&self,
6272
path: &str,
@@ -68,16 +78,8 @@ impl Client {
6878
path = path
6979
);
7080

71-
let mut request = minreq::post(url)
72-
.with_header("Content-Type", "application/json")
73-
.with_header("Authorization", format!("Bearer {}", self.api_key));
74-
75-
if let Some(organization) = &self.organization {
76-
request = request.with_header("openai-organization", organization);
77-
}
78-
81+
let request = self.build_request(minreq::post(url));
7982
let res = request.with_json(params).unwrap().send();
80-
8183
match res {
8284
Ok(res) => {
8385
if (200..=299).contains(&res.status_code) {
@@ -99,16 +101,8 @@ impl Client {
99101
path = path
100102
);
101103

102-
let mut request = minreq::get(url)
103-
.with_header("Content-Type", "application/json")
104-
.with_header("Authorization", format!("Bearer {}", self.api_key));
105-
106-
if let Some(organization) = &self.organization {
107-
request = request.with_header("openai-organization", organization);
108-
}
109-
104+
let request = self.build_request(minreq::get(url));
110105
let res = request.send();
111-
112106
match res {
113107
Ok(res) => {
114108
if (200..=299).contains(&res.status_code) {
@@ -130,16 +124,8 @@ impl Client {
130124
path = path
131125
);
132126

133-
let mut request = minreq::delete(url)
134-
.with_header("Content-Type", "application/json")
135-
.with_header("Authorization", format!("Bearer {}", self.api_key));
136-
137-
if let Some(organization) = &self.organization {
138-
request = request.with_header("openai-organization", organization);
139-
}
140-
127+
let request = self.build_request(minreq::delete(url));
141128
let res = request.send();
142-
143129
match res {
144130
Ok(res) => {
145131
if (200..=299).contains(&res.status_code) {

0 commit comments

Comments
 (0)