use std::sync::Arc;
use reqwest::Method;
use crate::constants::{batch_by_id, batch_cancel, ENDPOINT_BATCHES};
use crate::error::Result;
use crate::http::HttpTransport;
use crate::options::RequestOptions;
use crate::types::{Batch, BatchCreateParams, BatchList, BatchListParams};
use super::require;
pub struct Batches {
t: Arc<HttpTransport>,
}
impl Batches {
pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
Self { t }
}
pub async fn create(
&self,
params: BatchCreateParams,
req: impl Into<Option<RequestOptions>>,
) -> Result<Batch> {
let body =
serde_json::to_value(¶ms).map_err(|e| crate::Error::Decode(e.to_string()))?;
let (data, _) = self
.t
.request(
Method::POST,
ENDPOINT_BATCHES,
Some(&body),
&[],
req.into().as_ref(),
)
.await?;
require(data)
}
pub async fn list(
&self,
params: BatchListParams,
req: impl Into<Option<RequestOptions>>,
) -> Result<BatchList> {
let (data, _) = self
.t
.request(
Method::GET,
ENDPOINT_BATCHES,
None,
¶ms.query(),
req.into().as_ref(),
)
.await?;
require(data)
}
pub async fn retrieve(
&self,
batch_id: &str,
req: impl Into<Option<RequestOptions>>,
) -> Result<Batch> {
let (data, _) = self
.t
.request(
Method::GET,
&batch_by_id(batch_id),
None,
&[],
req.into().as_ref(),
)
.await?;
require(data)
}
pub async fn cancel(
&self,
batch_id: &str,
req: impl Into<Option<RequestOptions>>,
) -> Result<Batch> {
let (data, _) = self
.t
.request(
Method::POST,
&batch_cancel(batch_id),
None,
&[],
req.into().as_ref(),
)
.await?;
require(data)
}
}