Skip to content

Commit e34bde5

Browse files
author
Dongri Jin
authored
Merge pull request #19 from dongri/add-new-with-endpoint
Add new_with_endpoint
2 parents 39c8e2b + c3a08c3 commit e34bde5

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/v1/api.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ use crate::v1::moderation::{CreateModerationRequest, CreateModerationResponse};
2626

2727
use reqwest::Response;
2828

29-
const APU_URL_V1: &str = "https://api.openai.com/v1";
29+
const API_URL_V1: &str = "https://api.openai.com/v1";
3030

3131
pub struct Client {
32+
pub api_endpoint: String,
3233
pub api_key: String,
3334
}
3435

3536
impl Client {
3637
pub fn new(api_key: String) -> Self {
37-
Self { api_key }
38+
Self::new_with_endpoint(API_URL_V1.to_owned(), api_key)
39+
}
40+
41+
pub fn new_with_endpoint(api_endpoint: String, api_key: String) -> Self {
42+
Self {
43+
api_endpoint,
44+
api_key,
45+
}
3846
}
3947

4048
pub async fn post<T: serde::ser::Serialize>(
@@ -43,7 +51,11 @@ impl Client {
4351
params: &T,
4452
) -> Result<Response, APIError> {
4553
let client = reqwest::Client::new();
46-
let url = format!("{APU_URL_V1}{path}");
54+
let url = format!(
55+
"{api_endpoint}{path}",
56+
api_endpoint = self.api_endpoint,
57+
path = path
58+
);
4759
let res = client
4860
.post(&url)
4961
.header(reqwest::header::CONTENT_TYPE, "application/json")
@@ -67,7 +79,11 @@ impl Client {
6779

6880
pub async fn get(&self, path: &str) -> Result<Response, APIError> {
6981
let client = reqwest::Client::new();
70-
let url = format!("{APU_URL_V1}{path}");
82+
let url = format!(
83+
"{api_endpoint}{path}",
84+
api_endpoint = self.api_endpoint,
85+
path = path
86+
);
7187
let res = client
7288
.get(&url)
7389
.header(reqwest::header::CONTENT_TYPE, "application/json")
@@ -90,7 +106,11 @@ impl Client {
90106

91107
pub async fn delete(&self, path: &str) -> Result<Response, APIError> {
92108
let client = reqwest::Client::new();
93-
let url = format!("{APU_URL_V1}{path}");
109+
let url = format!(
110+
"{api_endpoint}{path}",
111+
api_endpoint = self.api_endpoint,
112+
path = path
113+
);
94114
let res = client
95115
.delete(&url)
96116
.header(reqwest::header::CONTENT_TYPE, "application/json")

0 commit comments

Comments
 (0)