portkey_sdk/service/batches.rs
1use std::future::Future;
2
3use crate::model::{Batch, CreateBatchRequest, ListBatchesResponse, PaginationParams};
4use crate::{PortkeyClient, Result};
5
6/// Service for managing batch processing jobs.
7///
8/// # Example
9///
10/// ```no_run
11/// use portkey_sdk::{PortkeyConfig, PortkeyClient, Result};
12/// use portkey_sdk::service::BatchesService;
13/// use portkey_sdk::model::CreateBatchRequest;
14///
15/// # async fn example() -> Result<()> {
16/// let config = PortkeyConfig::builder()
17/// .with_api_key("your-api-key")
18/// .build()?;
19/// let client = PortkeyClient::new(config)?;
20///
21/// let batch = client.create_batch(
22/// CreateBatchRequest {
23/// input_file_id: "file-abc123".to_string(),
24/// endpoint: "/v1/chat/completions".to_string(),
25/// completion_window: "24h".to_string(),
26/// metadata: None,
27/// }
28/// ).await?;
29///
30/// println!("Created batch: {}", batch.id);
31/// # Ok(())
32/// # }
33/// ```
34pub trait BatchesService {
35 /// Creates and executes a batch from an uploaded file of requests.
36 ///
37 /// # Example
38 ///
39 /// ```no_run
40 /// # use portkey_sdk::{PortkeyClient, Result};
41 /// # use portkey_sdk::service::BatchesService;
42 /// # use portkey_sdk::model::CreateBatchRequest;
43 /// # async fn example(client: PortkeyClient) -> Result<()> {
44 /// let batch = client.create_batch(
45 /// CreateBatchRequest {
46 /// input_file_id: "file-abc123".to_string(),
47 /// endpoint: "/v1/chat/completions".to_string(),
48 /// completion_window: "24h".to_string(),
49 /// metadata: None,
50 /// }
51 /// ).await?;
52 /// # Ok(())
53 /// # }
54 /// ```
55 fn create_batch(&self, request: CreateBatchRequest) -> impl Future<Output = Result<Batch>>;
56
57 /// Retrieves a batch.
58 ///
59 /// # Arguments
60 ///
61 /// * `batch_id` - The ID of the batch to retrieve.
62 ///
63 /// # Example
64 ///
65 /// ```no_run
66 /// # use portkey_sdk::{PortkeyClient, Result};
67 /// # use portkey_sdk::service::BatchesService;
68 /// # async fn example(client: PortkeyClient) -> Result<()> {
69 /// let batch = client.retrieve_batch("batch_abc123").await?;
70 /// println!("Status: {}", batch.status);
71 /// # Ok(())
72 /// # }
73 /// ```
74 fn retrieve_batch(&self, batch_id: &str) -> impl Future<Output = Result<Batch>>;
75
76 /// Cancels an in-progress batch.
77 ///
78 /// # Arguments
79 ///
80 /// * `batch_id` - The ID of the batch to cancel.
81 ///
82 /// # Example
83 ///
84 /// ```no_run
85 /// # use portkey_sdk::{PortkeyClient, Result};
86 /// # use portkey_sdk::service::BatchesService;
87 /// # async fn example(client: PortkeyClient) -> Result<()> {
88 /// let batch = client.cancel_batch("batch_abc123").await?;
89 /// println!("Cancelled batch: {}", batch.id);
90 /// # Ok(())
91 /// # }
92 /// ```
93 fn cancel_batch(&self, batch_id: &str) -> impl Future<Output = Result<Batch>>;
94
95 /// List your organization's batches.
96 ///
97 /// # Arguments
98 ///
99 /// * `after` - A cursor for use in pagination.
100 /// * `limit` - A limit on the number of objects to be returned (1-100, default: 20).
101 ///
102 /// # Example
103 ///
104 /// ```no_run
105 /// # use portkey_sdk::service::BatchesService;
106 /// # use portkey_sdk::model::PaginationParams;
107 /// # async fn example(client: &impl BatchesService) -> portkey_sdk::Result<()> {
108 /// let params = PaginationParams { limit: Some(10), order: None, after: None, before: None };
109 /// let batches = client.list_batches(params).await?;
110 /// for batch in batches.data {
111 /// println!("Batch {}: {}", batch.id, batch.status);
112 /// }
113 /// # Ok(())
114 /// # }
115 /// ```
116 fn list_batches(
117 &self,
118 params: PaginationParams,
119 ) -> impl Future<Output = Result<ListBatchesResponse>>;
120}
121
122impl BatchesService for PortkeyClient {
123 async fn create_batch(&self, request: CreateBatchRequest) -> Result<Batch> {
124 #[cfg(feature = "tracing")]
125 tracing::debug!(
126 target: crate::TRACING_TARGET_SERVICE,
127 "Creating batch"
128 );
129
130 let response = self
131 .send_json(reqwest::Method::POST, "/batches", &request)
132 .await?;
133 let response = response.error_for_status()?;
134 let batch: Batch = response.json().await?;
135
136 #[cfg(feature = "tracing")]
137 tracing::debug!(
138 target: crate::TRACING_TARGET_SERVICE,
139 "Batch created successfully"
140 );
141
142 Ok(batch)
143 }
144
145 async fn retrieve_batch(&self, batch_id: &str) -> Result<Batch> {
146 #[cfg(feature = "tracing")]
147 tracing::debug!(
148 target: crate::TRACING_TARGET_SERVICE,
149 batch_id = %batch_id,
150 "Retrieving batch"
151 );
152
153 let response = self
154 .send(reqwest::Method::GET, &format!("/batches/{}", batch_id))
155 .await?;
156 let response = response.error_for_status()?;
157 let batch: Batch = response.json().await?;
158
159 #[cfg(feature = "tracing")]
160 tracing::debug!(
161 target: crate::TRACING_TARGET_SERVICE,
162 "Batch retrieved successfully"
163 );
164
165 Ok(batch)
166 }
167
168 async fn cancel_batch(&self, batch_id: &str) -> Result<Batch> {
169 #[cfg(feature = "tracing")]
170 tracing::debug!(
171 target: crate::TRACING_TARGET_SERVICE,
172 batch_id = %batch_id,
173 "Cancelling batch"
174 );
175
176 let response = self
177 .send_json(
178 reqwest::Method::POST,
179 &format!("/batches/{}/cancel", batch_id),
180 &serde_json::json!({}),
181 )
182 .await?;
183 let response = response.error_for_status()?;
184 let batch: Batch = response.json().await?;
185
186 #[cfg(feature = "tracing")]
187 tracing::debug!(
188 target: crate::TRACING_TARGET_SERVICE,
189 "Batch cancelled successfully"
190 );
191
192 Ok(batch)
193 }
194
195 async fn list_batches(&self, params: PaginationParams<'_>) -> Result<ListBatchesResponse> {
196 #[cfg(feature = "tracing")]
197 tracing::debug!(
198 target: crate::TRACING_TARGET_SERVICE,
199 "Listing batches"
200 );
201
202 let query_params = params.to_query_params();
203 let query_params_refs: Vec<(&str, &str)> =
204 query_params.iter().map(|(k, v)| (*k, v.as_str())).collect();
205
206 let response = self
207 .send_with_params(reqwest::Method::GET, "/batches", &query_params_refs)
208 .await?;
209 let response = response.error_for_status()?;
210 let batches: ListBatchesResponse = response.json().await?;
211
212 #[cfg(feature = "tracing")]
213 tracing::debug!(
214 target: crate::TRACING_TARGET_SERVICE,
215 "Batches retrieved successfully"
216 );
217
218 Ok(batches)
219 }
220}