This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bulk write functionality. Currently only supports reading json fi…
…les (directory support to come)
- Loading branch information
Showing
3 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use cloudflare::apiclient::ApiClient; | ||
|
||
use std::fs; | ||
|
||
use cloudflare::workerskv::write_bulk::BulkWrite; | ||
use cloudflare::workerskv::write_bulk::BulkWriteParams; | ||
use cloudflare::workerskv::write_bulk::KeyValuePair; | ||
|
||
use crate::terminal::message; | ||
|
||
pub fn write_bulk(namespace_id: &str, filename: &str, expiration: Option<&str>, ttl: Option<&str>, base64: bool) -> Result<(), failure::Error> { | ||
let client = super::api_client()?; | ||
let account_id = super::account_id()?; | ||
|
||
// Read key-value pairs from json file into Vec<KeyValuePair> | ||
let data = fs::read_to_string(filename)?; | ||
let pairs: Vec<KeyValuePair> = serde_json::from_str(&data)?; | ||
|
||
let msg = format!("Uploading key-value pairs from \"{}\" to namespace {}", filename, namespace_id); | ||
message::working(&msg); | ||
|
||
let response = client.request(&BulkWrite { | ||
account_identifier: &account_id, | ||
namespace_identifier: namespace_id, | ||
bulk_key_value_pairs: pairs, | ||
|
||
params: BulkWriteParams { | ||
expiration: expiration, | ||
expiration_ttl: ttl, | ||
base64: base64, | ||
}, | ||
}); | ||
|
||
match response { | ||
Ok(_success) => message::success(&format!("Success")), | ||
Err(e) => super::print_error(e), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters