Change error type

This commit is contained in:
Dongri Jin
2023-03-04 19:21:34 +09:00
parent 6a68775555
commit 95bef4a7b8
3 changed files with 93 additions and 125 deletions

View File

@ -12,6 +12,7 @@ use crate::v1::image::{
ImageVariationRequest, ImageVariationResponse,
};
use reqwest::Response;
use std::io::Error;
const APU_URL_V1: &str = "https://api.openai.com/v1";
@ -28,7 +29,7 @@ impl Client {
&self,
path: &str,
params: &T,
) -> Result<Response, Box<dyn std::error::Error>> {
) -> Result<Response, Error> {
let client = reqwest::Client::new();
let url = format!("{APU_URL_V1}{path}");
let res = client
@ -44,16 +45,16 @@ impl Client {
match res {
Ok(res) => match res.status().is_success() {
true => Ok(res),
false => Err(Box::new(std::io::Error::new(
false => Err(Error::new(
std::io::ErrorKind::Other,
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 url = format!("{APU_URL_V1}{path}");
let res = client
@ -68,16 +69,16 @@ impl Client {
match res {
Ok(res) => match res.status().is_success() {
true => Ok(res),
false => Err(Box::new(std::io::Error::new(
false => Err(Error::new(
std::io::ErrorKind::Other,
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 url = format!("{APU_URL_V1}{path}");
let res = client
@ -92,176 +93,143 @@ impl Client {
match res {
Ok(res) => match res.status().is_success() {
true => Ok(res),
false => Err(Box::new(std::io::Error::new(
false => Err(Error::new(
std::io::ErrorKind::Other,
format!("{}: {}", res.status(), res.text().await.unwrap()),
))),
)),
},
Err(e) => Err(Box::new(e)),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn completion(
&self,
req: CompletionRequest,
) -> Result<CompletionResponse, Box<dyn std::error::Error>> {
let res = self.post("/completions", &req).await;
match res {
Ok(res) => {
let r = res.json::<CompletionResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn completion(&self, req: CompletionRequest) -> Result<CompletionResponse, Error> {
let res = self.post("/completions", &req).await?;
let r = res.json::<CompletionResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn edit(&self, req: EditRequest) -> Result<EditResponse, Box<dyn std::error::Error>> {
let res = self.post("/edits", &req).await;
match res {
Ok(res) => {
let r = res.json::<EditResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn edit(&self, req: EditRequest) -> Result<EditResponse, Error> {
let res = self.post("/edits", &req).await?;
let r = res.json::<EditResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn image_generation(
&self,
req: ImageGenerationRequest,
) -> Result<ImageGenerationResponse, Box<dyn std::error::Error>> {
let res = self.post("/images/generations", &req).await;
match res {
Ok(res) => {
let r = res.json::<ImageGenerationResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
) -> Result<ImageGenerationResponse, Error> {
let res = self.post("/images/generations", &req).await?;
let r = res.json::<ImageGenerationResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn image_edit(
&self,
req: ImageEditRequest,
) -> Result<ImageEditResponse, Box<dyn std::error::Error>> {
let res = self.post("/images/edits", &req).await;
match res {
Ok(res) => {
let r = res.json::<ImageEditResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn image_edit(&self, req: ImageEditRequest) -> Result<ImageEditResponse, Error> {
let res = self.post("/images/edits", &req).await?;
let r = res.json::<ImageEditResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn image_variation(
&self,
req: ImageVariationRequest,
) -> Result<ImageVariationResponse, Box<dyn std::error::Error>> {
let res = self.post("/images/variations", &req).await;
match res {
Ok(res) => {
let r = res.json::<ImageVariationResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
) -> Result<ImageVariationResponse, Error> {
let res = self.post("/images/variations", &req).await?;
let r = res.json::<ImageVariationResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn embedding(
&self,
req: EmbeddingRequest,
) -> Result<EmbeddingResponse, Box<dyn std::error::Error>> {
let res = self.post("/embeddings", &req).await;
match res {
Ok(res) => {
let r = res.json::<EmbeddingResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn embedding(&self, req: EmbeddingRequest) -> Result<EmbeddingResponse, Error> {
let res = self.post("/embeddings", &req).await?;
let r = res.json::<EmbeddingResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn file_list(&self) -> Result<FileListResponse, Box<dyn std::error::Error>> {
let res = self.get("/files").await;
match res {
Ok(res) => {
let r = res.json::<FileListResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn file_list(&self) -> Result<FileListResponse, Error> {
let res = self.get("/files").await?;
let r = res.json::<FileListResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn file_upload(
&self,
req: FileUploadRequest,
) -> Result<FileUploadResponse, Box<dyn std::error::Error>> {
let res = self.post("/files", &req).await;
match res {
Ok(res) => {
let r = res.json::<FileUploadResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn file_upload(&self, req: FileUploadRequest) -> Result<FileUploadResponse, Error> {
let res = self.post("/files", &req).await?;
let r = res.json::<FileUploadResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn file_delete(
&self,
req: FileDeleteRequest,
) -> Result<FileDeleteResponse, Box<dyn std::error::Error>> {
let res = self.delete(&format!("{}/{}", "/files", req.file_id)).await;
match res {
Ok(res) => {
let r = res.json::<FileDeleteResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
pub async fn file_delete(&self, req: FileDeleteRequest) -> Result<FileDeleteResponse, Error> {
let res = self
.delete(&format!("{}/{}", "/files", req.file_id))
.await?;
let r = res.json::<FileDeleteResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn file_retrieve(
&self,
req: FileRetrieveRequest,
) -> Result<FileRetrieveResponse, Box<dyn std::error::Error>> {
let res = self.get(&format!("{}/{}", "/files", req.file_id)).await;
match res {
Ok(res) => {
let r = res.json::<FileRetrieveResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
) -> Result<FileRetrieveResponse, Error> {
let res = self.get(&format!("{}/{}", "/files", req.file_id)).await?;
let r = res.json::<FileRetrieveResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn file_retrieve_content(
&self,
req: FileRetrieveContentRequest,
) -> Result<FileRetrieveContentResponse, Box<dyn std::error::Error>> {
) -> Result<FileRetrieveContentResponse, Error> {
let res = self
.get(&format!("{}/{}/content", "/files", req.file_id))
.await;
match res {
Ok(res) => {
let r = res.json::<FileRetrieveContentResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
.await?;
let r = res.json::<FileRetrieveContentResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
pub async fn chat_completion(
&self,
req: ChatCompletionRequest,
) -> Result<ChatCompletionResponse, Box<dyn std::error::Error>> {
let res = self.post("/chat/completions", &req).await;
match res {
Ok(res) => {
let r = res.json::<ChatCompletionResponse>().await?;
Ok(r)
}
Err(e) => Err(e),
) -> Result<ChatCompletionResponse, Error> {
let res = self.post("/chat/completions", &req).await?;
let r = res.json::<ChatCompletionResponse>().await;
match r {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
}
}
fn new_error(&self, err: reqwest::Error) -> Error {
Error::new(std::io::ErrorKind::Other, err)
}
}