use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::pagination::HasId;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BatchStatus {
Validating,
Failed,
InProgress,
Finalizing,
Completed,
Expired,
Cancelling,
Cancelled,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct BatchError {
#[serde(default)]
pub code: Option<String>,
#[serde(default)]
pub line: Option<i64>,
#[serde(default)]
pub message: Option<String>,
#[serde(default)]
pub param: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct BatchErrors {
#[serde(default)]
pub data: Vec<BatchError>,
#[serde(default)]
pub object: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct BatchRequestCounts {
#[serde(default)]
pub completed: i64,
#[serde(default)]
pub failed: i64,
#[serde(default)]
pub total: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct Batch {
pub id: String,
#[serde(default)]
pub completion_window: String,
#[serde(default)]
pub created_at: i64,
#[serde(default)]
pub endpoint: String,
#[serde(default)]
pub input_file_id: String,
#[serde(default)]
pub object: String,
pub status: BatchStatus,
#[serde(default)]
pub cancelled_at: Option<i64>,
#[serde(default)]
pub cancelling_at: Option<i64>,
#[serde(default)]
pub completed_at: Option<i64>,
#[serde(default)]
pub expired_at: Option<i64>,
#[serde(default)]
pub expires_at: Option<i64>,
#[serde(default)]
pub failed_at: Option<i64>,
#[serde(default)]
pub finalizing_at: Option<i64>,
#[serde(default)]
pub in_progress_at: Option<i64>,
#[serde(default)]
pub error_file_id: Option<String>,
#[serde(default)]
pub output_file_id: Option<String>,
#[serde(default)]
pub errors: Option<BatchErrors>,
#[serde(default)]
pub metadata: Option<HashMap<String, String>>,
#[serde(default)]
pub model: Option<String>,
#[serde(default)]
pub request_counts: Option<BatchRequestCounts>,
#[serde(default)]
pub usage: Option<serde_json::Value>,
}
impl HasId for Batch {
fn id(&self) -> Option<&str> {
Some(&self.id)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct BatchCreateParams {
pub completion_window: String,
pub endpoint: String,
pub input_file_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub output_expires_after: Option<crate::types::common::ExpiresAfter>,
}
impl BatchCreateParams {
pub fn new(
input_file_id: impl Into<String>,
endpoint: impl Into<String>,
completion_window: impl Into<String>,
) -> Self {
Self {
completion_window: completion_window.into(),
endpoint: endpoint.into(),
input_file_id: input_file_id.into(),
metadata: None,
output_expires_after: None,
}
}
pub fn metadata(mut self, metadata: HashMap<String, String>) -> Self {
self.metadata = Some(metadata);
self
}
}
#[derive(Debug, Clone, Default)]
pub struct BatchListParams {
pub after: Option<String>,
pub limit: Option<u32>,
}
impl BatchListParams {
pub fn after(mut self, after: impl Into<String>) -> Self {
self.after = Some(after.into());
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub(crate) fn to_query(&self) -> Vec<(String, String)> {
crate::pagination::cursor_query(self.after.as_deref(), None, self.limit, None)
}
}