Skip to content

Commit

Permalink
Merge pull request #19 from dongri/add-new-with-endpoint
Browse files Browse the repository at this point in the history
Add new_with_endpoint
  • Loading branch information
Dongri Jin authored Jul 28, 2023
2 parents 39c8e2b + c3a08c3 commit e34bde5
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ use crate::v1::moderation::{CreateModerationRequest, CreateModerationResponse};

use reqwest::Response;

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

pub struct Client {
pub api_endpoint: String,
pub api_key: String,
}

impl Client {
pub fn new(api_key: String) -> Self {
Self { api_key }
Self::new_with_endpoint(API_URL_V1.to_owned(), api_key)
}

pub fn new_with_endpoint(api_endpoint: String, api_key: String) -> Self {
Self {
api_endpoint,
api_key,
}
}

pub async fn post<T: serde::ser::Serialize>(
Expand All @@ -43,7 +51,11 @@ impl Client {
params: &T,
) -> Result<Response, APIError> {
let client = reqwest::Client::new();
let url = format!("{APU_URL_V1}{path}");
let url = format!(
"{api_endpoint}{path}",
api_endpoint = self.api_endpoint,
path = path
);
let res = client
.post(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
Expand All @@ -67,7 +79,11 @@ impl Client {

pub async fn get(&self, path: &str) -> Result<Response, APIError> {
let client = reqwest::Client::new();
let url = format!("{APU_URL_V1}{path}");
let url = format!(
"{api_endpoint}{path}",
api_endpoint = self.api_endpoint,
path = path
);
let res = client
.get(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
Expand All @@ -90,7 +106,11 @@ impl Client {

pub async fn delete(&self, path: &str) -> Result<Response, APIError> {
let client = reqwest::Client::new();
let url = format!("{APU_URL_V1}{path}");
let url = format!(
"{api_endpoint}{path}",
api_endpoint = self.api_endpoint,
path = path
);
let res = client
.delete(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
Expand Down

0 comments on commit e34bde5

Please sign in to comment.