From 65c49a6fdfd2a73a4afc04ed5fcbb9a9ae60cfc6 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Thu, 10 Jul 2025 15:47:30 +0900 Subject: [PATCH 1/2] Fix batch object --- src/v1/batch.rs | 50 +++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/v1/batch.rs b/src/v1/batch.rs index 05e889d..f6badb8 100644 --- a/src/v1/batch.rs +++ b/src/v1/batch.rs @@ -23,27 +23,41 @@ pub struct RequestCounts { } #[derive(Debug, Serialize, Deserialize)] -pub struct BatchResponse { - pub id: String, +pub struct BatchError { + pub code: String, + pub line: Option, + pub message: String, + pub param: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct BatchErrors { pub object: String, - pub endpoint: String, - pub errors: Option>, - pub input_file_id: String, - pub completion_window: String, - pub status: String, - pub output_file_id: Option, - pub error_file_id: Option, - pub created_at: u64, - pub in_progress_at: Option, - pub expires_at: Option, - pub finalizing_at: Option, - pub completed_at: Option, - pub failed_at: Option, - pub expired_at: Option, - pub cancelling_at: Option, + pub data: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct BatchResponse { pub cancelled_at: Option, - pub request_counts: RequestCounts, + pub cancelling_at: Option, + pub completed_at: Option, + pub completion_window: String, + pub created_at: u64, + pub endpoint: String, + pub error_file_id: Option, + pub errors: Option, + pub expired_at: Option, + pub expires_at: Option, + pub failed_at: Option, + pub finalizing_at: Option, + pub id: String, + pub in_progress_at: Option, + pub input_file_id: String, pub metadata: Option, + pub object: String, + pub output_file_id: Option, + pub request_counts: RequestCounts, + pub status: String, } #[derive(Debug, Serialize, Deserialize)] From bb59b45effd8d5135bee8cd8d3d89abe7d25a2ef Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Thu, 10 Jul 2025 16:10:45 +0900 Subject: [PATCH 2/2] cargo clippy --- examples/batch.rs | 2 +- examples/function_call.rs | 2 +- examples/function_call_role.rs | 6 +-- src/v1/api.rs | 83 ++++++++++++++++------------------ src/v1/error.rs | 4 +- 5 files changed, 45 insertions(+), 52 deletions(-) diff --git a/examples/batch.rs b/examples/batch.rs index aca5784..4a5538b 100644 --- a/examples/batch.rs +++ b/examples/batch.rs @@ -44,7 +44,7 @@ async fn main() -> Result<(), Box> { let result = client.retrieve_file_content(file_id).await?; let s = match str::from_utf8(&result) { Ok(v) => v.to_string(), - Err(e) => panic!("Invalid UTF-8 sequence: {}", e), + Err(e) => panic!("Invalid UTF-8 sequence: {e}"), }; let json_value: Value = from_str(&s)?; let result_json = to_string_pretty(&json_value)?; diff --git a/examples/function_call.rs b/examples/function_call.rs index 06bd922..3935599 100644 --- a/examples/function_call.rs +++ b/examples/function_call.rs @@ -86,7 +86,7 @@ async fn main() -> Result<(), Box> { let coin = c.coin; if name == "get_coin_price" { let price = get_coin_price(&coin); - println!("{} price: {}", coin, price); + println!("{coin} price: {price}"); } } } diff --git a/examples/function_call_role.rs b/examples/function_call_role.rs index 46148f4..901d5d7 100644 --- a/examples/function_call_role.rs +++ b/examples/function_call_role.rs @@ -79,9 +79,9 @@ async fn main() -> Result<(), Box> { let arguments = function_call.arguments.clone().unwrap(); let c: Currency = serde_json::from_str(&arguments)?; let coin = c.coin; - println!("coin: {}", coin); + println!("coin: {coin}"); let price = get_coin_price(&coin); - println!("price: {}", price); + println!("price: {price}"); let req = ChatCompletionRequest::new( GPT4_O.to_string(), @@ -99,7 +99,7 @@ async fn main() -> Result<(), Box> { role: chat_completion::MessageRole::function, content: chat_completion::Content::Text({ let price = get_coin_price(&coin); - format!("{{\"price\": {}}}", price) + format!("{{\"price\": {price}}}") }), name: Some(String::from("get_coin_price")), tool_calls: None, diff --git a/src/v1/api.rs b/src/v1/api.rs index 1c50695..ce0b1c4 100644 --- a/src/v1/api.rs +++ b/src/v1/api.rs @@ -163,7 +163,7 @@ impl OpenAIClient { let mut request = client.request(method, url); if let Some(api_key) = &self.api_key { - request = request.header("Authorization", format!("Bearer {}", api_key)); + request = request.header("Authorization", format!("Bearer {api_key}")); } if let Some(organization) = &self.organization { @@ -244,7 +244,7 @@ impl OpenAIClient { Ok(parsed) } Err(e) => Err(APIError::CustomError { - message: format!("Failed to parse JSON: {} / response {}", e, text), + message: format!("Failed to parse JSON: {e} / response {text}"), }), } } else { @@ -253,7 +253,7 @@ impl OpenAIClient { .await .unwrap_or_else(|_| "Unknown error".to_string()); Err(APIError::CustomError { - message: format!("{}: {}", status, error_message), + message: format!("{status}: {error_message}"), }) } } @@ -320,11 +320,11 @@ impl OpenAIClient { &mut self, file_id: String, ) -> Result { - self.get(&format!("files/{}", file_id)).await + self.get(&format!("files/{file_id}")).await } pub async fn retrieve_file_content(&self, file_id: String) -> Result { - self.get_raw(&format!("files/{}/content", file_id)).await + self.get_raw(&format!("files/{file_id}/content")).await } pub async fn chat_completion( @@ -495,7 +495,7 @@ impl OpenAIClient { &mut self, assistant_id: String, ) -> Result { - self.get(&format!("assistants/{}", assistant_id)).await + self.get(&format!("assistants/{assistant_id}")).await } pub async fn modify_assistant( @@ -503,15 +503,14 @@ impl OpenAIClient { assistant_id: String, req: AssistantRequest, ) -> Result { - self.post(&format!("assistants/{}", assistant_id), &req) - .await + self.post(&format!("assistants/{assistant_id}"), &req).await } pub async fn delete_assistant( &mut self, assistant_id: String, ) -> Result { - self.delete(&format!("assistants/{}", assistant_id)).await + self.delete(&format!("assistants/{assistant_id}")).await } pub async fn list_assistant( @@ -530,7 +529,7 @@ impl OpenAIClient { assistant_id: String, req: AssistantFileRequest, ) -> Result { - self.post(&format!("assistants/{}/files", assistant_id), &req) + self.post(&format!("assistants/{assistant_id}/files"), &req) .await } @@ -539,7 +538,7 @@ impl OpenAIClient { assistant_id: String, file_id: String, ) -> Result { - self.get(&format!("assistants/{}/files/{}", assistant_id, file_id)) + self.get(&format!("assistants/{assistant_id}/files/{file_id}")) .await } @@ -548,7 +547,7 @@ impl OpenAIClient { assistant_id: String, file_id: String, ) -> Result { - self.delete(&format!("assistants/{}/files/{}", assistant_id, file_id)) + self.delete(&format!("assistants/{assistant_id}/files/{file_id}")) .await } @@ -565,7 +564,7 @@ impl OpenAIClient { order, after, before, - format!("assistants/{}/files", assistant_id), + format!("assistants/{assistant_id}/files"), ); self.get(&url).await } @@ -578,7 +577,7 @@ impl OpenAIClient { } pub async fn retrieve_thread(&mut self, thread_id: String) -> Result { - self.get(&format!("threads/{}", thread_id)).await + self.get(&format!("threads/{thread_id}")).await } pub async fn modify_thread( @@ -586,14 +585,14 @@ impl OpenAIClient { thread_id: String, req: ModifyThreadRequest, ) -> Result { - self.post(&format!("threads/{}", thread_id), &req).await + self.post(&format!("threads/{thread_id}"), &req).await } pub async fn delete_thread( &mut self, thread_id: String, ) -> Result { - self.delete(&format!("threads/{}", thread_id)).await + self.delete(&format!("threads/{thread_id}")).await } pub async fn create_message( @@ -601,7 +600,7 @@ impl OpenAIClient { thread_id: String, req: CreateMessageRequest, ) -> Result { - self.post(&format!("threads/{}/messages", thread_id), &req) + self.post(&format!("threads/{thread_id}/messages"), &req) .await } @@ -610,7 +609,7 @@ impl OpenAIClient { thread_id: String, message_id: String, ) -> Result { - self.get(&format!("threads/{}/messages/{}", thread_id, message_id)) + self.get(&format!("threads/{thread_id}/messages/{message_id}")) .await } @@ -620,15 +619,12 @@ impl OpenAIClient { message_id: String, req: ModifyMessageRequest, ) -> Result { - self.post( - &format!("threads/{}/messages/{}", thread_id, message_id), - &req, - ) - .await + self.post(&format!("threads/{thread_id}/messages/{message_id}"), &req) + .await } pub async fn list_messages(&mut self, thread_id: String) -> Result { - self.get(&format!("threads/{}/messages", thread_id)).await + self.get(&format!("threads/{thread_id}/messages")).await } pub async fn retrieve_message_file( @@ -638,8 +634,7 @@ impl OpenAIClient { file_id: String, ) -> Result { self.get(&format!( - "threads/{}/messages/{}/files/{}", - thread_id, message_id, file_id + "threads/{thread_id}/messages/{message_id}/files/{file_id}" )) .await } @@ -658,7 +653,7 @@ impl OpenAIClient { order, after, before, - format!("threads/{}/messages/{}/files", thread_id, message_id), + format!("threads/{thread_id}/messages/{message_id}/files"), ); self.get(&url).await } @@ -668,8 +663,7 @@ impl OpenAIClient { thread_id: String, req: CreateRunRequest, ) -> Result { - self.post(&format!("threads/{}/runs", thread_id), &req) - .await + self.post(&format!("threads/{thread_id}/runs"), &req).await } pub async fn retrieve_run( @@ -677,7 +671,7 @@ impl OpenAIClient { thread_id: String, run_id: String, ) -> Result { - self.get(&format!("threads/{}/runs/{}", thread_id, run_id)) + self.get(&format!("threads/{thread_id}/runs/{run_id}")) .await } @@ -687,7 +681,7 @@ impl OpenAIClient { run_id: String, req: ModifyRunRequest, ) -> Result { - self.post(&format!("threads/{}/runs/{}", thread_id, run_id), &req) + self.post(&format!("threads/{thread_id}/runs/{run_id}"), &req) .await } @@ -704,7 +698,7 @@ impl OpenAIClient { order, after, before, - format!("threads/{}/runs", thread_id), + format!("threads/{thread_id}/runs"), ); self.get(&url).await } @@ -715,7 +709,7 @@ impl OpenAIClient { run_id: String, ) -> Result { self.post( - &format!("threads/{}/runs/{}/cancel", thread_id, run_id), + &format!("threads/{thread_id}/runs/{run_id}/cancel"), &ModifyRunRequest::default(), ) .await @@ -735,8 +729,7 @@ impl OpenAIClient { step_id: String, ) -> Result { self.get(&format!( - "threads/{}/runs/{}/steps/{}", - thread_id, run_id, step_id + "threads/{thread_id}/runs/{run_id}/steps/{step_id}" )) .await } @@ -755,7 +748,7 @@ impl OpenAIClient { order, after, before, - format!("threads/{}/runs/{}/steps", thread_id, run_id), + format!("threads/{thread_id}/runs/{run_id}/steps"), ); self.get(&url).await } @@ -768,12 +761,12 @@ impl OpenAIClient { } pub async fn retrieve_batch(&mut self, batch_id: String) -> Result { - self.get(&format!("batches/{}", batch_id)).await + self.get(&format!("batches/{batch_id}")).await } pub async fn cancel_batch(&mut self, batch_id: String) -> Result { self.post( - &format!("batches/{}/cancel", batch_id), + &format!("batches/{batch_id}/cancel"), &common::EmptyRequestBody {}, ) .await @@ -793,14 +786,14 @@ impl OpenAIClient { } pub async fn retrieve_model(&mut self, model_id: String) -> Result { - self.get(&format!("models/{}", model_id)).await + self.get(&format!("models/{model_id}")).await } pub async fn delete_model( &mut self, model_id: String, ) -> Result { - self.delete(&format!("models/{}", model_id)).await + self.delete(&format!("models/{model_id}")).await } fn build_url_with_preserved_query(&self, path: &str) -> Result { @@ -829,16 +822,16 @@ impl OpenAIClient { ) -> String { let mut params = vec![]; if let Some(limit) = limit { - params.push(format!("limit={}", limit)); + params.push(format!("limit={limit}")); } if let Some(order) = order { - params.push(format!("order={}", order)); + params.push(format!("order={order}")); } if let Some(after) = after { - params.push(format!("after={}", after)); + params.push(format!("after={after}")); } if let Some(before) = before { - params.push(format!("before={}", before)); + params.push(format!("before={before}")); } if !params.is_empty() { url = format!("{}?{}", url, params.join("&")); @@ -866,7 +859,7 @@ impl OpenAIClient { map.get(file_field) .and_then(|v| v.as_str()) .ok_or(APIError::CustomError { - message: format!("Field '{}' not found or not a string", file_field), + message: format!("Field '{file_field}' not found or not a string"), })? } else { return Err(APIError::CustomError { diff --git a/src/v1/error.rs b/src/v1/error.rs index d1625a2..f13b23f 100644 --- a/src/v1/error.rs +++ b/src/v1/error.rs @@ -11,8 +11,8 @@ pub enum APIError { impl fmt::Display for APIError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - APIError::ReqwestError(err) => write!(f, "ReqwestError: {}", err), - APIError::CustomError { message } => write!(f, "APIError: {}", message), + APIError::ReqwestError(err) => write!(f, "ReqwestError: {err}"), + APIError::CustomError { message } => write!(f, "APIError: {message}"), } } }