use crate::framework::endpoint::{EndpointSpec, Method, RequestBody};
use crate::endpoints::workerskv::WorkersKvBulkResult;
use crate::framework::response::ApiSuccess;
use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub struct WriteBulk<'a> {
pub account_identifier: &'a str,
pub namespace_identifier: &'a str,
pub bulk_key_value_pairs: Vec<KeyValuePair>,
}
impl EndpointSpec for WriteBulk<'_> {
type JsonResponse = WorkersKvBulkResult;
type ResponseType = ApiSuccess<Self::JsonResponse>;
fn method(&self) -> Method {
Method::PUT
}
fn path(&self) -> String {
format!(
"accounts/{}/storage/kv/namespaces/{}/bulk",
self.account_identifier, self.namespace_identifier
)
}
#[inline]
fn body(&self) -> Option<RequestBody> {
if self.bulk_key_value_pairs.len() > 10_000 {
panic!("Bulk write request must have 10,000 key-value pairs or less.");
}
let body = serde_json::to_string(&self.bulk_key_value_pairs).unwrap();
Some(RequestBody::Json(body))
}
}
#[serde_with::skip_serializing_none]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct KeyValuePair {
pub key: String,
pub value: String,
pub expiration: Option<i64>,
pub expiration_ttl: Option<i64>,
pub base64: Option<bool>,
}