openai_interface/batches/
create.rs1use serde::Serialize;
7use url::Url;
8
9use crate::{
10 batches::Batch,
11 errors::OapiError,
12 rest::post::{Post, PostNoStream},
13};
14
15#[derive(Debug, Default, Clone, Serialize)]
17pub struct CreateBatchRequest {
18 pub input_file_id: String,
20 pub endpoint: String,
23 pub completion_window: CompletionWindow,
26 #[serde(skip_serializing_if = "Option::is_none")]
29 pub metadata: Option<std::collections::HashMap<String, String>>,
30 #[serde(flatten, skip_serializing_if = "Option::is_none")]
33 pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
38pub enum CompletionWindow {
39 #[default]
41 Hours24,
42}
43
44impl CompletionWindow {
45 #[must_use]
47 pub fn as_str(self) -> &'static str {
48 match self {
49 CompletionWindow::Hours24 => "24h",
50 }
51 }
52}
53
54impl Serialize for CompletionWindow {
55 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
56 serializer.serialize_str(self.as_str())
57 }
58}
59
60impl Post for CreateBatchRequest {
61 fn is_streaming(&self) -> bool {
62 false
63 }
64
65 fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
69 let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
70 url.path_segments_mut()
71 .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
72 .push("batches");
73 Ok(url.to_string())
74 }
75}
76
77impl PostNoStream for CreateBatchRequest {
78 type Response = Batch;
79}