Skip to content

Commit 20deee9

Browse files
author
Dongri Jin
authored
Merge pull request #28 from dongri/add-header-for-organization
Add new_with_organization
2 parents 1965624 + f318d3d commit 20deee9

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

src/v1/api.rs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const API_URL_V1: &str = "https://api.openai.com/v1";
3131
pub struct Client {
3232
pub api_endpoint: String,
3333
pub api_key: String,
34+
pub organization: Option<String>,
3435
}
3536

3637
impl Client {
@@ -43,30 +44,58 @@ impl Client {
4344
Self {
4445
api_endpoint,
4546
api_key,
47+
organization: None,
4648
}
4749
}
4850

51+
pub fn new_with_organization(api_key: String, organization: String) -> Self {
52+
let endpoint = std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned());
53+
Self {
54+
api_endpoint: endpoint,
55+
api_key,
56+
organization: organization.into(),
57+
}
58+
}
59+
60+
pub async fn client_builder(&self) -> Result<reqwest::Client, reqwest::Error> {
61+
let mut headers = reqwest::header::HeaderMap::new();
62+
headers.insert(
63+
reqwest::header::CONTENT_TYPE,
64+
reqwest::header::HeaderValue::from_static("application/json"),
65+
);
66+
headers.insert(
67+
reqwest::header::AUTHORIZATION,
68+
reqwest::header::HeaderValue::from_str(&("Bearer ".to_owned() + &self.api_key))
69+
.unwrap(),
70+
);
71+
match &self.organization {
72+
Some(organization) => headers.insert(
73+
reqwest::header::HeaderName::from_static("openai-organization"),
74+
reqwest::header::HeaderValue::from_str(organization).unwrap(),
75+
),
76+
None => None,
77+
};
78+
let client = reqwest::Client::builder()
79+
.default_headers(headers)
80+
.build()?;
81+
Ok(client)
82+
}
83+
4984
pub async fn post<T: serde::ser::Serialize>(
5085
&self,
5186
path: &str,
5287
params: &T,
5388
) -> Result<Response, APIError> {
54-
let client = reqwest::Client::new();
5589
let url = format!(
5690
"{api_endpoint}{path}",
5791
api_endpoint = self.api_endpoint,
5892
path = path
5993
);
60-
let res = client
61-
.post(&url)
62-
.header(reqwest::header::CONTENT_TYPE, "application/json")
63-
.header(
64-
reqwest::header::AUTHORIZATION,
65-
"Bearer ".to_owned() + &self.api_key,
66-
)
67-
.json(&params)
68-
.send()
69-
.await;
94+
let client = match self.client_builder().await {
95+
Ok(c) => c,
96+
Err(e) => return Err(self.new_error(e)),
97+
};
98+
let res = client.post(&url).json(&params).send().await;
7099
match res {
71100
Ok(res) => match res.status().is_success() {
72101
true => Ok(res),
@@ -79,21 +108,16 @@ impl Client {
79108
}
80109

81110
pub async fn get(&self, path: &str) -> Result<Response, APIError> {
82-
let client = reqwest::Client::new();
83111
let url = format!(
84112
"{api_endpoint}{path}",
85113
api_endpoint = self.api_endpoint,
86114
path = path
87115
);
88-
let res = client
89-
.get(&url)
90-
.header(reqwest::header::CONTENT_TYPE, "application/json")
91-
.header(
92-
reqwest::header::AUTHORIZATION,
93-
"Bearer ".to_owned() + &self.api_key,
94-
)
95-
.send()
96-
.await;
116+
let client = match self.client_builder().await {
117+
Ok(c) => c,
118+
Err(e) => return Err(self.new_error(e)),
119+
};
120+
let res = client.get(&url).send().await;
97121
match res {
98122
Ok(res) => match res.status().is_success() {
99123
true => Ok(res),
@@ -106,21 +130,16 @@ impl Client {
106130
}
107131

108132
pub async fn delete(&self, path: &str) -> Result<Response, APIError> {
109-
let client = reqwest::Client::new();
110133
let url = format!(
111134
"{api_endpoint}{path}",
112135
api_endpoint = self.api_endpoint,
113136
path = path
114137
);
115-
let res = client
116-
.delete(&url)
117-
.header(reqwest::header::CONTENT_TYPE, "application/json")
118-
.header(
119-
reqwest::header::AUTHORIZATION,
120-
"Bearer ".to_owned() + &self.api_key,
121-
)
122-
.send()
123-
.await;
138+
let client = match self.client_builder().await {
139+
Ok(c) => c,
140+
Err(e) => return Err(self.new_error(e)),
141+
};
142+
let res = client.delete(&url).send().await;
124143
match res {
125144
Ok(res) => match res.status().is_success() {
126145
true => Ok(res),

0 commit comments

Comments
 (0)