Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymichal committed May 28, 2020
1 parent 99c639a commit 1453536
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
16 changes: 16 additions & 0 deletions src/commands/kv/bulk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@ pub mod put;

pub use delete::run as delete;
pub use put::run as put;

// Create a special API client that has a longer timeout than usual, given that KV operations
// can be lengthy if payloads are large.
fn bulk_api_client(user: &GlobalUser) -> Result<HttpApiClient, failure::Error> {
let config = HttpApiClientConfig {
http_timeout: Duration::from_secs(5 * 60),
default_headers: headers(None),
};

HttpApiClient::new(
Credentials::from(user.to_owned()),
config,
Environment::Production,
)
}

8 changes: 5 additions & 3 deletions src/commands/kv/bulk/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ use std::path::Path;

use cloudflare::endpoints::workerskv::write_bulk::KeyValuePair;

use crate::commands::kv;
use crate::commands::kv::validate_target;
use crate::kv::bulk::put;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;
use crate::terminal::message;

use super::bulk_api_client;

pub fn run(
target: &Target,
user: &GlobalUser,
namespace_id: &str,
filename: &Path,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;
validate_target(target)?;

let pairs: Vec<KeyValuePair> = match &metadata(filename) {
Ok(file_type) if file_type.is_file() => {
Expand All @@ -36,7 +38,7 @@ pub fn run(
Err(e) => Err(failure::format_err!("{}", e)),
}?;

let client = kv::api_client(user)?;
let client = bulk_api_client(user)?;
put(&client, target, namespace_id, &pairs)?;
message::success("Success");

Expand Down
7 changes: 3 additions & 4 deletions src/commands/kv/key/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cloudflare::endpoints::workerskv::delete_key::DeleteKey;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
use crate::commands::kv::validate_target;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;
use crate::terminal::interactive;
Expand All @@ -13,8 +12,8 @@ pub fn delete(
id: &str,
key: &str,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let client = kv::api_client(user)?;
validate_target(target)?;
let client = http::cf_v4_client(user)?;

match interactive::delete(&format!("Are you sure you want to delete key \"{}\"?", key)) {
Ok(true) => (),
Expand Down
16 changes: 1 addition & 15 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;
use cloudflare::framework::auth::Credentials;
use cloudflare::framework::response::ApiFailure;
use cloudflare::framework::{Environment, HttpApiClient, HttpApiClientConfig};
use cloudflare::framework::apiclient::ApiClient;

use percent_encoding::{percent_encode, PATH_SEGMENT_ENCODE_SET};

Expand All @@ -16,21 +17,6 @@ pub mod bulk;
pub mod key;
pub mod namespace;

// Create a special API client that has a longer timeout than usual, given that KV operations
// can be lengthy if payloads are large.
fn api_client(user: &GlobalUser) -> Result<HttpApiClient, failure::Error> {
let config = HttpApiClientConfig {
http_timeout: Duration::from_secs(5 * 60),
default_headers: headers(None),
};

HttpApiClient::new(
Credentials::from(user.to_owned()),
config,
Environment::Production,
)
}

// TODO: callers outside this module should write their own error handling (lookin at you sites)
pub fn format_error(e: ApiFailure) -> String {
http::format_error(e, Some(&kv_help))
Expand Down

0 comments on commit 1453536

Please sign in to comment.