mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-27 17:39:23 +00:00
Change error type
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "openai-api-rs"
|
name = "openai-api-rs"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Dongri Jin <dongrify@gmail.com>"]
|
authors = ["Dongri Jin <dongrify@gmail.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
Cargo.toml
|
Cargo.toml
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
openai-api-rs = "0.1.2"
|
openai-api-rs = "0.1.3"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
|
214
src/v1/api.rs
214
src/v1/api.rs
@ -12,6 +12,7 @@ use crate::v1::image::{
|
|||||||
ImageVariationRequest, ImageVariationResponse,
|
ImageVariationRequest, ImageVariationResponse,
|
||||||
};
|
};
|
||||||
use reqwest::Response;
|
use reqwest::Response;
|
||||||
|
use std::io::Error;
|
||||||
|
|
||||||
const APU_URL_V1: &str = "https://api.openai.com/v1";
|
const APU_URL_V1: &str = "https://api.openai.com/v1";
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ impl Client {
|
|||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
params: &T,
|
params: &T,
|
||||||
) -> Result<Response, Box<dyn std::error::Error>> {
|
) -> Result<Response, Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let url = format!("{APU_URL_V1}{path}");
|
let url = format!("{APU_URL_V1}{path}");
|
||||||
let res = client
|
let res = client
|
||||||
@ -44,16 +45,16 @@ impl Client {
|
|||||||
match res {
|
match res {
|
||||||
Ok(res) => match res.status().is_success() {
|
Ok(res) => match res.status().is_success() {
|
||||||
true => Ok(res),
|
true => Ok(res),
|
||||||
false => Err(Box::new(std::io::Error::new(
|
false => Err(Error::new(
|
||||||
std::io::ErrorKind::Other,
|
std::io::ErrorKind::Other,
|
||||||
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||||
))),
|
)),
|
||||||
},
|
},
|
||||||
Err(e) => Err(Box::new(e)),
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(&self, path: &str) -> Result<Response, Box<dyn std::error::Error>> {
|
pub async fn get(&self, path: &str) -> Result<Response, Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let url = format!("{APU_URL_V1}{path}");
|
let url = format!("{APU_URL_V1}{path}");
|
||||||
let res = client
|
let res = client
|
||||||
@ -68,16 +69,16 @@ impl Client {
|
|||||||
match res {
|
match res {
|
||||||
Ok(res) => match res.status().is_success() {
|
Ok(res) => match res.status().is_success() {
|
||||||
true => Ok(res),
|
true => Ok(res),
|
||||||
false => Err(Box::new(std::io::Error::new(
|
false => Err(Error::new(
|
||||||
std::io::ErrorKind::Other,
|
std::io::ErrorKind::Other,
|
||||||
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||||
))),
|
)),
|
||||||
},
|
},
|
||||||
Err(e) => Err(Box::new(e)),
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, path: &str) -> Result<Response, Box<dyn std::error::Error>> {
|
pub async fn delete(&self, path: &str) -> Result<Response, Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let url = format!("{APU_URL_V1}{path}");
|
let url = format!("{APU_URL_V1}{path}");
|
||||||
let res = client
|
let res = client
|
||||||
@ -92,176 +93,143 @@ impl Client {
|
|||||||
match res {
|
match res {
|
||||||
Ok(res) => match res.status().is_success() {
|
Ok(res) => match res.status().is_success() {
|
||||||
true => Ok(res),
|
true => Ok(res),
|
||||||
false => Err(Box::new(std::io::Error::new(
|
false => Err(Error::new(
|
||||||
std::io::ErrorKind::Other,
|
std::io::ErrorKind::Other,
|
||||||
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||||
))),
|
)),
|
||||||
},
|
},
|
||||||
Err(e) => Err(Box::new(e)),
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn completion(
|
pub async fn completion(&self, req: CompletionRequest) -> Result<CompletionResponse, Error> {
|
||||||
&self,
|
let res = self.post("/completions", &req).await?;
|
||||||
req: CompletionRequest,
|
let r = res.json::<CompletionResponse>().await;
|
||||||
) -> Result<CompletionResponse, Box<dyn std::error::Error>> {
|
match r {
|
||||||
let res = self.post("/completions", &req).await;
|
Ok(r) => Ok(r),
|
||||||
match res {
|
Err(e) => Err(self.new_error(e)),
|
||||||
Ok(res) => {
|
|
||||||
let r = res.json::<CompletionResponse>().await?;
|
|
||||||
Ok(r)
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn edit(&self, req: EditRequest) -> Result<EditResponse, Box<dyn std::error::Error>> {
|
pub async fn edit(&self, req: EditRequest) -> Result<EditResponse, Error> {
|
||||||
let res = self.post("/edits", &req).await;
|
let res = self.post("/edits", &req).await?;
|
||||||
match res {
|
let r = res.json::<EditResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<EditResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn image_generation(
|
pub async fn image_generation(
|
||||||
&self,
|
&self,
|
||||||
req: ImageGenerationRequest,
|
req: ImageGenerationRequest,
|
||||||
) -> Result<ImageGenerationResponse, Box<dyn std::error::Error>> {
|
) -> Result<ImageGenerationResponse, Error> {
|
||||||
let res = self.post("/images/generations", &req).await;
|
let res = self.post("/images/generations", &req).await?;
|
||||||
match res {
|
let r = res.json::<ImageGenerationResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<ImageGenerationResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn image_edit(
|
pub async fn image_edit(&self, req: ImageEditRequest) -> Result<ImageEditResponse, Error> {
|
||||||
&self,
|
let res = self.post("/images/edits", &req).await?;
|
||||||
req: ImageEditRequest,
|
let r = res.json::<ImageEditResponse>().await;
|
||||||
) -> Result<ImageEditResponse, Box<dyn std::error::Error>> {
|
match r {
|
||||||
let res = self.post("/images/edits", &req).await;
|
Ok(r) => Ok(r),
|
||||||
match res {
|
Err(e) => Err(self.new_error(e)),
|
||||||
Ok(res) => {
|
|
||||||
let r = res.json::<ImageEditResponse>().await?;
|
|
||||||
Ok(r)
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn image_variation(
|
pub async fn image_variation(
|
||||||
&self,
|
&self,
|
||||||
req: ImageVariationRequest,
|
req: ImageVariationRequest,
|
||||||
) -> Result<ImageVariationResponse, Box<dyn std::error::Error>> {
|
) -> Result<ImageVariationResponse, Error> {
|
||||||
let res = self.post("/images/variations", &req).await;
|
let res = self.post("/images/variations", &req).await?;
|
||||||
match res {
|
let r = res.json::<ImageVariationResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<ImageVariationResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn embedding(
|
pub async fn embedding(&self, req: EmbeddingRequest) -> Result<EmbeddingResponse, Error> {
|
||||||
&self,
|
let res = self.post("/embeddings", &req).await?;
|
||||||
req: EmbeddingRequest,
|
let r = res.json::<EmbeddingResponse>().await;
|
||||||
) -> Result<EmbeddingResponse, Box<dyn std::error::Error>> {
|
match r {
|
||||||
let res = self.post("/embeddings", &req).await;
|
Ok(r) => Ok(r),
|
||||||
match res {
|
Err(e) => Err(self.new_error(e)),
|
||||||
Ok(res) => {
|
|
||||||
let r = res.json::<EmbeddingResponse>().await?;
|
|
||||||
Ok(r)
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn file_list(&self) -> Result<FileListResponse, Box<dyn std::error::Error>> {
|
pub async fn file_list(&self) -> Result<FileListResponse, Error> {
|
||||||
let res = self.get("/files").await;
|
let res = self.get("/files").await?;
|
||||||
match res {
|
let r = res.json::<FileListResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<FileListResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn file_upload(
|
pub async fn file_upload(&self, req: FileUploadRequest) -> Result<FileUploadResponse, Error> {
|
||||||
&self,
|
let res = self.post("/files", &req).await?;
|
||||||
req: FileUploadRequest,
|
let r = res.json::<FileUploadResponse>().await;
|
||||||
) -> Result<FileUploadResponse, Box<dyn std::error::Error>> {
|
match r {
|
||||||
let res = self.post("/files", &req).await;
|
Ok(r) => Ok(r),
|
||||||
match res {
|
Err(e) => Err(self.new_error(e)),
|
||||||
Ok(res) => {
|
|
||||||
let r = res.json::<FileUploadResponse>().await?;
|
|
||||||
Ok(r)
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn file_delete(
|
pub async fn file_delete(&self, req: FileDeleteRequest) -> Result<FileDeleteResponse, Error> {
|
||||||
&self,
|
let res = self
|
||||||
req: FileDeleteRequest,
|
.delete(&format!("{}/{}", "/files", req.file_id))
|
||||||
) -> Result<FileDeleteResponse, Box<dyn std::error::Error>> {
|
.await?;
|
||||||
let res = self.delete(&format!("{}/{}", "/files", req.file_id)).await;
|
let r = res.json::<FileDeleteResponse>().await;
|
||||||
match res {
|
match r {
|
||||||
Ok(res) => {
|
Ok(r) => Ok(r),
|
||||||
let r = res.json::<FileDeleteResponse>().await?;
|
Err(e) => Err(self.new_error(e)),
|
||||||
Ok(r)
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn file_retrieve(
|
pub async fn file_retrieve(
|
||||||
&self,
|
&self,
|
||||||
req: FileRetrieveRequest,
|
req: FileRetrieveRequest,
|
||||||
) -> Result<FileRetrieveResponse, Box<dyn std::error::Error>> {
|
) -> Result<FileRetrieveResponse, Error> {
|
||||||
let res = self.get(&format!("{}/{}", "/files", req.file_id)).await;
|
let res = self.get(&format!("{}/{}", "/files", req.file_id)).await?;
|
||||||
match res {
|
let r = res.json::<FileRetrieveResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<FileRetrieveResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn file_retrieve_content(
|
pub async fn file_retrieve_content(
|
||||||
&self,
|
&self,
|
||||||
req: FileRetrieveContentRequest,
|
req: FileRetrieveContentRequest,
|
||||||
) -> Result<FileRetrieveContentResponse, Box<dyn std::error::Error>> {
|
) -> Result<FileRetrieveContentResponse, Error> {
|
||||||
let res = self
|
let res = self
|
||||||
.get(&format!("{}/{}/content", "/files", req.file_id))
|
.get(&format!("{}/{}/content", "/files", req.file_id))
|
||||||
.await;
|
.await?;
|
||||||
match res {
|
let r = res.json::<FileRetrieveContentResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<FileRetrieveContentResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn chat_completion(
|
pub async fn chat_completion(
|
||||||
&self,
|
&self,
|
||||||
req: ChatCompletionRequest,
|
req: ChatCompletionRequest,
|
||||||
) -> Result<ChatCompletionResponse, Box<dyn std::error::Error>> {
|
) -> Result<ChatCompletionResponse, Error> {
|
||||||
let res = self.post("/chat/completions", &req).await;
|
let res = self.post("/chat/completions", &req).await?;
|
||||||
match res {
|
let r = res.json::<ChatCompletionResponse>().await;
|
||||||
Ok(res) => {
|
match r {
|
||||||
let r = res.json::<ChatCompletionResponse>().await?;
|
Ok(r) => Ok(r),
|
||||||
Ok(r)
|
Err(e) => Err(self.new_error(e)),
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_error(&self, err: reqwest::Error) -> Error {
|
||||||
|
Error::new(std::io::ErrorKind::Other, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user