diff --git a/Cargo.toml b/Cargo.toml index 7844cb6..6899636 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openai-api-rs" -version = "0.1.2" +version = "0.1.3" edition = "2021" authors = ["Dongri Jin "] license = "MIT" diff --git a/README.md b/README.md index 3d3a2b6..cb899c1 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Cargo.toml ```toml [dependencies] -openai-api-rs = "0.1.2" +openai-api-rs = "0.1.3" ``` ## Example: diff --git a/src/v1/api.rs b/src/v1/api.rs index 32a260e..c6a4deb 100644 --- a/src/v1/api.rs +++ b/src/v1/api.rs @@ -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> { + ) -> Result { 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> { + pub async fn get(&self, path: &str) -> Result { 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> { + pub async fn delete(&self, path: &str) -> Result { 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> { - let res = self.post("/completions", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn completion(&self, req: CompletionRequest) -> Result { + let res = self.post("/completions", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } - pub async fn edit(&self, req: EditRequest) -> Result> { - let res = self.post("/edits", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn edit(&self, req: EditRequest) -> Result { + let res = self.post("/edits", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } pub async fn image_generation( &self, req: ImageGenerationRequest, - ) -> Result> { - let res = self.post("/images/generations", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + ) -> Result { + let res = self.post("/images/generations", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } - pub async fn image_edit( - &self, - req: ImageEditRequest, - ) -> Result> { - let res = self.post("/images/edits", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn image_edit(&self, req: ImageEditRequest) -> Result { + let res = self.post("/images/edits", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } pub async fn image_variation( &self, req: ImageVariationRequest, - ) -> Result> { - let res = self.post("/images/variations", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + ) -> Result { + let res = self.post("/images/variations", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } - pub async fn embedding( - &self, - req: EmbeddingRequest, - ) -> Result> { - let res = self.post("/embeddings", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn embedding(&self, req: EmbeddingRequest) -> Result { + let res = self.post("/embeddings", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } - pub async fn file_list(&self) -> Result> { - let res = self.get("/files").await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn file_list(&self) -> Result { + let res = self.get("/files").await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } - pub async fn file_upload( - &self, - req: FileUploadRequest, - ) -> Result> { - let res = self.post("/files", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn file_upload(&self, req: FileUploadRequest) -> Result { + let res = self.post("/files", &req).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } - pub async fn file_delete( - &self, - req: FileDeleteRequest, - ) -> Result> { - let res = self.delete(&format!("{}/{}", "/files", req.file_id)).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + pub async fn file_delete(&self, req: FileDeleteRequest) -> Result { + let res = self + .delete(&format!("{}/{}", "/files", req.file_id)) + .await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } pub async fn file_retrieve( &self, req: FileRetrieveRequest, - ) -> Result> { - let res = self.get(&format!("{}/{}", "/files", req.file_id)).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + ) -> Result { + let res = self.get(&format!("{}/{}", "/files", req.file_id)).await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } pub async fn file_retrieve_content( &self, req: FileRetrieveContentRequest, - ) -> Result> { + ) -> Result { let res = self .get(&format!("{}/{}/content", "/files", req.file_id)) - .await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + .await?; + let r = res.json::().await; + match r { + Ok(r) => Ok(r), + Err(e) => Err(self.new_error(e)), } } pub async fn chat_completion( &self, req: ChatCompletionRequest, - ) -> Result> { - let res = self.post("/chat/completions", &req).await; - match res { - Ok(res) => { - let r = res.json::().await?; - Ok(r) - } - Err(e) => Err(e), + ) -> Result { + let res = self.post("/chat/completions", &req).await?; + let r = res.json::().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) + } }