floopy/resources/
batches.rs1use 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
13pub 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 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(¶ms).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 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 ¶ms.query(),
68 req.into().as_ref(),
69 )
70 .await?;
71 require(data)
72 }
73
74 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 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}