Skip to main content

floopy/resources/
batches.rs

1use std::sync::Arc;
2
3use reqwest::Method;
4
5use crate::constants::{batch_by_id, batch_cancel, ENDPOINT_BATCHES};
6use crate::error::Result;
7use crate::http::HttpTransport;
8use crate::options::RequestOptions;
9use crate::types::{Batch, BatchCreateParams, BatchList, BatchListParams};
10
11use super::require;
12
13/// Batches API — create/list/retrieve/cancel asynchronous batch jobs.
14///
15/// A batch carries no model up front, so select the upstream with
16/// [`RequestOptions::provider`] (the `floopy-provider` header).
17pub struct Batches {
18    t: Arc<HttpTransport>,
19}
20
21impl Batches {
22    pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
23        Self { t }
24    }
25
26    /// Create a batch from a previously uploaded input file.
27    ///
28    /// # Errors
29    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
30    /// failure.
31    pub async fn create(
32        &self,
33        params: BatchCreateParams,
34        req: impl Into<Option<RequestOptions>>,
35    ) -> Result<Batch> {
36        let body =
37            serde_json::to_value(&params).map_err(|e| crate::Error::Decode(e.to_string()))?;
38        let (data, _) = self
39            .t
40            .request(
41                Method::POST,
42                ENDPOINT_BATCHES,
43                Some(&body),
44                &[],
45                req.into().as_ref(),
46            )
47            .await?;
48        require(data)
49    }
50
51    /// List batches for the organization.
52    ///
53    /// # Errors
54    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
55    /// failure.
56    pub async fn list(
57        &self,
58        params: BatchListParams,
59        req: impl Into<Option<RequestOptions>>,
60    ) -> Result<BatchList> {
61        let (data, _) = self
62            .t
63            .request(
64                Method::GET,
65                ENDPOINT_BATCHES,
66                None,
67                &params.query(),
68                req.into().as_ref(),
69            )
70            .await?;
71        require(data)
72    }
73
74    /// Retrieve a single batch (poll its status).
75    ///
76    /// # Errors
77    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
78    /// failure.
79    pub async fn retrieve(
80        &self,
81        batch_id: &str,
82        req: impl Into<Option<RequestOptions>>,
83    ) -> Result<Batch> {
84        let (data, _) = self
85            .t
86            .request(
87                Method::GET,
88                &batch_by_id(batch_id),
89                None,
90                &[],
91                req.into().as_ref(),
92            )
93            .await?;
94        require(data)
95    }
96
97    /// Request cancellation of an in-progress batch.
98    ///
99    /// # Errors
100    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
101    /// failure.
102    pub async fn cancel(
103        &self,
104        batch_id: &str,
105        req: impl Into<Option<RequestOptions>>,
106    ) -> Result<Batch> {
107        let (data, _) = self
108            .t
109            .request(
110                Method::POST,
111                &batch_cancel(batch_id),
112                None,
113                &[],
114                req.into().as_ref(),
115            )
116            .await?;
117        require(data)
118    }
119}