use super::{idem, seg};
use crate::error::Result;
use crate::http::{Body, HttpClient};
use crate::models::{
Batch, BatchReport, BatchStopResult, CreateBatch, ListBatchItems, Message, Page, RequestOptions,
};
use crate::util::{query, uuid_v4};
#[derive(Debug, Clone)]
pub struct Batches {
http: HttpClient,
}
impl Batches {
pub(crate) fn new(http: HttpClient) -> Self {
Self { http }
}
pub async fn create(&self, params: &CreateBatch) -> Result<Batch> {
self.create_with(params, &RequestOptions::default()).await
}
pub async fn create_with(&self, params: &CreateBatch, opts: &RequestOptions) -> Result<Batch> {
self.http
.send("POST", "/v1/messages/batch", params, idem(opts))
.await
}
pub async fn create_from_csv(
&self,
csv: impl Into<Vec<u8>>,
dedupe: Option<bool>,
) -> Result<Batch> {
self.create_from_csv_with(csv, dedupe, &RequestOptions::default())
.await
}
pub async fn create_from_csv_with(
&self,
csv: impl Into<Vec<u8>>,
dedupe: Option<bool>,
opts: &RequestOptions,
) -> Result<Batch> {
let csv = csv.into();
let body = match dedupe {
None => Body::Raw {
content_type: "text/csv".to_string(),
bytes: csv,
},
Some(flag) => {
let boundary = format!("opensms-{}", uuid_v4());
let mut b = Vec::with_capacity(csv.len() + 400);
b.extend_from_slice(
format!(
"--{boundary}\r\nContent-Disposition: form-data; name=\"dedupe\"\r\n\r\n{flag}\r\n\
--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"batch.csv\"\r\n\
Content-Type: text/csv\r\n\r\n"
)
.as_bytes(),
);
b.extend_from_slice(&csv);
b.extend_from_slice(format!("\r\n--{boundary}--\r\n").as_bytes());
Body::Raw {
content_type: format!("multipart/form-data; boundary={boundary}"),
bytes: b,
}
}
};
self.http
.json("POST", "/v1/messages/batch", body, idem(opts))
.await
}
pub async fn get(&self, id: &str) -> Result<Batch> {
self.http
.get(&format!("/v1/batches/{}", seg(id, "id")?))
.await
}
pub async fn validation(&self, id: &str) -> Result<BatchReport> {
self.http
.get(&format!("/v1/batches/{}/validation", seg(id, "id")?))
.await
}
pub async fn start(&self, id: &str) -> Result<Batch> {
self.start_with(id, &RequestOptions::default()).await
}
pub async fn start_with(&self, id: &str, opts: &RequestOptions) -> Result<Batch> {
let path = format!("/v1/batches/{}/start", seg(id, "id")?);
self.http.json("POST", &path, Body::Empty, idem(opts)).await
}
pub async fn stop(&self, id: &str) -> Result<BatchStopResult> {
self.stop_with(id, &RequestOptions::default()).await
}
pub async fn stop_with(&self, id: &str, opts: &RequestOptions) -> Result<BatchStopResult> {
let path = format!("/v1/batches/{}/stop", seg(id, "id")?);
self.http.json("POST", &path, Body::Empty, idem(opts)).await
}
pub async fn list_items(&self, id: &str, params: ListBatchItems) -> Result<Page<Message>> {
let q = query(&[
("status", params.status),
("limit", params.limit.map(|v| v.to_string())),
("cursor", params.cursor),
]);
self.http
.get(&format!("/v1/batches/{}/items{q}", seg(id, "id")?))
.await
}
}