use reqwest::Method;
use crate::client::Client;
use crate::error::OpenAIError;
use crate::pagination::List;
use crate::request::RequestOptions;
use crate::types::batches::{Batch, BatchCreateParams, BatchListParams};
#[derive(Debug, Clone)]
pub struct Batches {
client: Client,
}
impl Batches {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn create(&self, params: BatchCreateParams) -> Result<Batch, OpenAIError> {
let body = serde_json::to_value(¶ms)?;
self.client
.execute(Method::POST, "/batches", RequestOptions::json(body))
.await
}
pub async fn retrieve(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
self.client
.execute(
Method::GET,
&format!("/batches/{batch_id}"),
RequestOptions::default(),
)
.await
}
pub async fn list(&self, params: Option<BatchListParams>) -> Result<List<Batch>, OpenAIError> {
let query = params.map(|p| p.to_query()).unwrap_or_default();
self.client
.execute(Method::GET, "/batches", RequestOptions::query(query))
.await
}
pub async fn list_all(&self, params: Option<BatchListParams>) -> Result<Vec<Batch>, OpenAIError> {
let query = params.map(|p| p.to_query()).unwrap_or_default();
self.client.paginate_all("/batches", query).await
}
pub async fn cancel(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
self.client
.execute(
Method::POST,
&format!("/batches/{batch_id}/cancel"),
RequestOptions::default(),
)
.await
}
}