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

Commit

Permalink
Use client with longer timeout for bulk kv commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymichal committed Jun 2, 2020
1 parent 99c639a commit 554cfb9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
23 changes: 23 additions & 0 deletions src/commands/kv/bulk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,26 @@ pub mod put;

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

use std::time::Duration;

use cloudflare::framework::auth::Credentials;
use cloudflare::framework::{Environment, HttpApiClient, HttpApiClientConfig};

use crate::http::feature::headers;
use crate::settings::global_user::GlobalUser;

// 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
9 changes: 5 additions & 4 deletions src/commands/kv/key/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cloudflare::endpoints::workerskv::delete_key::DeleteKey;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
use crate::commands::kv::{format_error, validate_target};
use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;
use crate::terminal::interactive;
Expand All @@ -13,8 +14,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 All @@ -36,7 +37,7 @@ pub fn delete(

match response {
Ok(_) => message::success("Success"),
Err(e) => print!("{}", kv::format_error(e)),
Err(e) => print!("{}", format_error(e)),
}

Ok(())
Expand Down
22 changes: 1 addition & 21 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
use std::collections::HashSet;
use std::time::Duration;

use cloudflare::framework::auth::Credentials;
use cloudflare::framework::response::ApiFailure;
use cloudflare::framework::{Environment, HttpApiClient, HttpApiClientConfig};

use percent_encoding::{percent_encode, PATH_SEGMENT_ENCODE_SET};

use crate::settings::global_user::GlobalUser;
use crate::http;
use crate::settings::toml::Target;

use crate::http::{self, feature::headers};

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
2 changes: 1 addition & 1 deletion src/settings/toml/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::PathBuf;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Target {
pub account_id: String,
#[serde(rename = "kv-namespaces")]
Expand Down

0 comments on commit 554cfb9

Please sign in to comment.