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