mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-23 15:48:07 +00:00
Merge pull request #32 from dongri/refactoring-minreq-header
Refactoring http request headers
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "openai-api-rs"
|
name = "openai-api-rs"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Dongri Jin <dongrify@gmail.com>"]
|
authors = ["Dongri Jin <dongrify@gmail.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
@ -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>(
|
pub fn post<T: serde::ser::Serialize>(
|
||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
@ -68,16 +78,8 @@ impl Client {
|
|||||||
path = path
|
path = path
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut request = minreq::post(url)
|
let request = self.build_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 res = request.with_json(params).unwrap().send();
|
let res = request.with_json(params).unwrap().send();
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
if (200..=299).contains(&res.status_code) {
|
if (200..=299).contains(&res.status_code) {
|
||||||
@ -99,16 +101,8 @@ impl Client {
|
|||||||
path = path
|
path = path
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut request = minreq::get(url)
|
let request = self.build_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 res = request.send();
|
let res = request.send();
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
if (200..=299).contains(&res.status_code) {
|
if (200..=299).contains(&res.status_code) {
|
||||||
@ -130,16 +124,8 @@ impl Client {
|
|||||||
path = path
|
path = path
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut request = minreq::delete(url)
|
let request = self.build_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 res = request.send();
|
let res = request.send();
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
if (200..=299).contains(&res.status_code) {
|
if (200..=299).contains(&res.status_code) {
|
||||||
|
Reference in New Issue
Block a user