1use serde::Serialize;
2
3use crate::{
4 config::Config,
5 error::OpenAIError,
6 types::{Batch, BatchRequest, ListBatchesResponse},
7 Client,
8};
9
10pub struct Batches<'c, C: Config> {
14 client: &'c Client<C>,
15}
16
17impl<'c, C: Config> Batches<'c, C> {
18 pub fn new(client: &'c Client<C>) -> Self {
19 Self { client }
20 }
21
22 #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
24 pub async fn create(&self, request: BatchRequest) -> Result<Batch, OpenAIError> {
25 self.client.post("/batches", request).await
26 }
27
28 #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
30 pub async fn list<Q>(&self, query: &Q) -> Result<ListBatchesResponse, OpenAIError>
31 where
32 Q: Serialize + ?Sized,
33 {
34 self.client.get_with_query("/batches", &query).await
35 }
36
37 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
39 pub async fn retrieve(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
40 self.client.get(&format!("/batches/{batch_id}")).await
41 }
42
43 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
45 pub async fn cancel(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
46 self.client
47 .post(
48 &format!("/batches/{batch_id}/cancel"),
49 serde_json::json!({}),
50 )
51 .await
52 }
53}