use serde::{Deserialize, Serialize};
use url::Url;
use crate::{
batches::Batch,
errors::OapiError,
rest::post::{Post, PostNoStream},
};
crate::request_body! {
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct CreateBatchRequest {
pub input_file_id: String,
pub endpoint: String,
pub completion_window: CompletionWindow,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<std::collections::HashMap<String, String>>,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum CompletionWindow {
#[default]
#[serde(rename = "24h")]
Hours24,
}
impl CompletionWindow {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
CompletionWindow::Hours24 => "24h",
}
}
}
impl Post for CreateBatchRequest {
fn is_streaming(&self) -> bool {
false
}
fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
url.path_segments_mut()
.map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
.push("batches");
Ok(url.to_string())
}
}
impl PostNoStream for CreateBatchRequest {
type Response = Batch;
}