Skip to content

Commit

Permalink
Add support for Workers KV bulk upload API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gabbifish committed Aug 20, 2019
1 parent 54b21d4 commit 3d0164a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/endpoints/workerskv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod list_namespace_keys;
pub mod list_namespaces;
pub mod remove_namespace;
pub mod rename_namespace;
pub mod write_bulk;

/// Workers KV Namespace
/// A Namespace is a collection of key-value pairs stored in Workers KV.
Expand Down
39 changes: 39 additions & 0 deletions src/endpoints/workerskv/write_bulk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::framework::endpoint::{Endpoint, Method};

/// Write Key-Value Pairs in Bulk
/// Writes multiple key-value pairs to Workers KV at once.
/// A 404 is returned if a write action is for a namespace ID the account doesn't have.
/// https://api.cloudflare.com/#workers-kv-namespace-write-multiple-key-value-pairs
pub struct WriteBulk<'a> {
pub account_identifier: &'a str,
pub namespace_identifier: &'a str,
pub bulk_key_value_pairs: Vec<KeyValuePair>,
}

impl<'a> Endpoint<(), (), Vec<KeyValuePair>> for WriteBulk<'a> {
fn method(&self) -> Method {
Method::Put
}
fn path(&self) -> String {
format!(
"accounts/{}/storage/kv/namespaces/{}/bulk",
self.account_identifier, self.namespace_identifier
)
}
fn body(&self) -> Option<Vec<KeyValuePair>> {
Some(self.bulk_key_value_pairs.clone())
}
// default content-type is already application/json
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct KeyValuePair {
pub key: String,
pub value: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub expiration: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expiration_ttl: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub base64: Option<bool>,
}

0 comments on commit 3d0164a

Please sign in to comment.