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

Commit

Permalink
remove builder and use functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Apr 7, 2020
1 parent e65c94b commit f21dfcf
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn global_config(user: &GlobalUser, verify: bool) -> Result<(), failure::Err
// validate_credentials() checks the /user/tokens/verify endpoint (for API token)
// or /user endpoint (for global API key) to ensure provided credentials actually work.
pub fn validate_credentials(user: &GlobalUser) -> Result<(), failure::Error> {
let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

match user {
GlobalUser::TokenAuth { .. } => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/kv/key/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn get(target: &Target, user: &GlobalUser, id: &str, key: &str) -> Result<()
kv::url_encode_key(key)
);

let client = http::auth_client(None, &user);
let client = http::legacy_auth_client(&user);

let res = client.get(&api_endpoint).send()?;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/kv/key/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn put(target: &Target, user: &GlobalUser, data: KVMetaData) -> Result<(), f
};
let url = Url::parse_with_params(&api_endpoint, query_params);

let client = http::auth_client(None, &user);
let client = http::legacy_auth_client(&user);

let url_into_str = url?.into_string();

Expand Down
6 changes: 1 addition & 5 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashSet;
use std::time::Duration;

use cloudflare::framework::response::ApiFailure;
use cloudflare::framework::HttpApiClient;
Expand All @@ -19,10 +18,7 @@ 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 = http::CfApiClientConfig::builder()
.timeout(Duration::from_secs(5 * 60))
.build();
http::cf_api_client(user, config)
http::kv_auth_client(user)
}

fn format_error(e: ApiFailure) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/preview/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn upload(
let missing_fields = validate(&target);

if missing_fields.is_empty() {
let client = http::auth_client(None, &user);
let client = http::legacy_auth_client(&user);

if let Some(site_config) = target.site.clone() {
let site_namespace = publish::add_site_namespace(user, target, true)?;
Expand Down
9 changes: 4 additions & 5 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn publish(
}
upload_files(target, user, &site_namespace.id, to_upload)?;

let upload_client = http::auth_client(Some(Feature::Sites), user);
let upload_client = http::legacy_featured_auth_client(user, Feature::Sites);

// Next, upload and deploy the worker with the updated asset_manifest
upload::script(&upload_client, &target, Some(asset_manifest))?;
Expand All @@ -58,20 +58,19 @@ pub fn publish(
} else {
let uses_kv_bucket = sync_non_site_buckets(target, user, verbose)?;

let feature = if uses_kv_bucket {
let upload_client = if uses_kv_bucket {
let wrangler_toml = style("`wrangler.toml`").yellow().bold();
let issue_link = style("https://github.com/cloudflare/wrangler/issues/1136")
.blue()
.bold();
let msg = format!("As of 1.9.0, you will no longer be able to specify a bucket for a kv namespace in your {}.\nIf your application depends on this feature, please file an issue with your use case here:\n{}", wrangler_toml, issue_link);
message::deprecation_warning(&msg);

Some(Feature::Bucket)
http::legacy_featured_auth_client(user, Feature::Bucket)
} else {
None
http::legacy_auth_client(user)
};

let upload_client = http::auth_client(feature, user);
upload::script(&upload_client, &target, None)?;

deploy::worker(&user, &deploy_config)?;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::settings::global_user::GlobalUser;
use crate::terminal::message;

pub fn list(zone_identifier: String, user: &GlobalUser) -> Result<(), failure::Error> {
let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

let result = client.request(&ListRoutes {
zone_identifier: &zone_identifier,
Expand All @@ -30,7 +30,7 @@ pub fn delete(
user: &GlobalUser,
route_id: &str,
) -> Result<(), failure::Error> {
let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

let result = client.request(&DeleteRoute {
zone_identifier: &zone_identifier,
Expand Down
8 changes: 4 additions & 4 deletions src/commands/secret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn upload_draft_worker(
let error = &api_errors.errors[0];
if error.code == 10007 {
message::working(&format!("Worker {} doesn't exist in the API yet. Creating a draft Worker so we can create new secret.", target.name));
let upload_client = http::auth_client(None, user);
let upload_client = http::legacy_auth_client(user);
Some(upload::script(&upload_client, target, None))
} else {
None
Expand All @@ -84,7 +84,7 @@ pub fn create_secret(name: &str, user: &GlobalUser, target: &Target) -> Result<(
target.name
));

let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

let params = CreateSecretParams {
name: name.to_string(),
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn delete_secret(name: &str, user: &GlobalUser, target: &Target) -> Result<(
name, target.name
));

let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

let response = client.request(&DeleteSecret {
account_identifier: &target.account_id,
Expand All @@ -161,7 +161,7 @@ pub fn delete_secret(name: &str, user: &GlobalUser, target: &Target) -> Result<(

pub fn list_secrets(user: &GlobalUser, target: &Target) -> Result<(), failure::Error> {
validate_target(target)?;
let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

let response = client.request(&ListSecrets {
account_identifier: &target.account_id,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/subdomain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Subdomain {
pub fn get(account_id: &str, user: &GlobalUser) -> Result<Option<String>, failure::Error> {
let addr = subdomain_addr(account_id);

let client = http::auth_client(None, user);
let client = http::legacy_auth_client(user);

let response = client.get(&addr).send()?;

Expand All @@ -37,7 +37,7 @@ impl Subdomain {
};
let subdomain_request = serde_json::to_string(&subdomain)?;

let client = http::auth_client(None, user);
let client = http::legacy_auth_client(user);

let response = client.put(&addr).body(subdomain_request).send()?;

Expand Down
6 changes: 2 additions & 4 deletions src/commands/whoami/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::terminal::emoji;

use cloudflare::endpoints::account;
use cloudflare::endpoints::account::Account;
use cloudflare::endpoints::account::{self, Account};
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::HttpApiClientConfig;

use prettytable::{Cell, Row, Table};

Expand All @@ -26,7 +24,7 @@ pub fn whoami(user: &GlobalUser) -> Result<(), failure::Error> {
}

fn fetch_accounts(user: &GlobalUser) -> Result<Vec<Account>, failure::Error> {
let client = http::cf_api_client(user, HttpApiClientConfig::default())?;
let client = http::auth_client(user)?;
let response = client.request(&account::ListAccounts { params: None });
match response {
Ok(res) => Ok(res.result),
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn publish_zoneless(
zoneless_config.account_id, zoneless_config.script_name,
);

let client = http::auth_client(None, user);
let client = http::legacy_auth_client(user);

log::info!("Making public on subdomain...");
let res = client
Expand Down
4 changes: 2 additions & 2 deletions src/deploy/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn publish_routes(
}

fn fetch_all(user: &GlobalUser, zone_identifier: &str) -> Result<Vec<Route>, failure::Error> {
let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

let routes: Vec<Route> = match client.request(&ListRoutes { zone_identifier }) {
Ok(success) => success.result.iter().map(Route::from).collect(),
Expand All @@ -43,7 +43,7 @@ fn create(
zone_identifier: &str,
route: &Route,
) -> Result<Route, failure::Error> {
let client = http::cf_api_client(user, http::CfApiClientConfig::default())?;
let client = http::auth_client(user)?;

log::info!("Creating your route {:#?}", &route.pattern,);
match client.request(&CreateRoute {
Expand Down
61 changes: 28 additions & 33 deletions src/http/v4.rs → src/http/cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,41 @@ use crate::http::{feature::headers, Feature};
use crate::settings::global_user::GlobalUser;
use crate::terminal::{emoji, message};

pub struct CfApiClientConfig {
feature: Option<Feature>,
http_timeout: Duration,
}

impl CfApiClientConfig {
pub fn default() -> HttpApiClientConfig {
let mut config = Self::builder();
config.build()
}

pub fn builder() -> Self {
CfApiClientConfig {
http_timeout: Duration::from_secs(30),
feature: None,
}
}
pub fn auth_client(user: &GlobalUser) -> Result<HttpApiClient, failure::Error> {
let config = HttpApiClientConfig {
http_timeout: Duration::from_secs(30),
default_headers: headers(None),
};

pub fn timeout<'a>(&'a mut self, timeout: Duration) -> &'a mut CfApiClientConfig {
self.http_timeout = timeout;
self
}
HttpApiClient::new(
Credentials::from(user.to_owned()),
config,
Environment::Production,
)
}

pub fn feature<'a>(&'a mut self, feature: Feature) -> &'a mut CfApiClientConfig {
self.feature = Some(feature);
self
}
pub fn kv_auth_client(user: &GlobalUser) -> Result<HttpApiClient, failure::Error> {
let config = HttpApiClientConfig {
http_timeout: Duration::from_secs(5 * 60),
default_headers: headers(None),
};

pub fn build<'a>(&'a mut self) -> HttpApiClientConfig {
HttpApiClientConfig {
http_timeout: self.http_timeout,
default_headers: headers(self.feature),
}
}
HttpApiClient::new(
Credentials::from(user.to_owned()),
config,
Environment::Production,
)
}

pub fn cf_api_client(
pub fn featured_auth_client(
user: &GlobalUser,
config: HttpApiClientConfig,
feature: Feature,
) -> Result<HttpApiClient, failure::Error> {
let config = HttpApiClientConfig {
http_timeout: Duration::from_secs(30),
default_headers: headers(Some(feature)),
};

HttpApiClient::new(
Credentials::from(user.to_owned()),
config,
Expand Down
10 changes: 9 additions & 1 deletion src/http/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ pub fn client(feature: Option<Feature>) -> Client {
.expect("could not create http client")
}

pub fn auth_client(feature: Option<Feature>, user: &GlobalUser) -> Client {
pub fn legacy_auth_client(user: &GlobalUser) -> Client {
get_client(user, None)
}

pub fn legacy_featured_auth_client(user: &GlobalUser, feature: Feature) -> Client {
get_client(user, Some(feature))
}

fn get_client(user: &GlobalUser, feature: Option<Feature>) -> Client {
let mut headers = headers(feature);
add_auth_headers(&mut headers, user);

Expand Down
6 changes: 3 additions & 3 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(self) mod cf;
pub(self) mod feature;
pub(self) mod legacy;
pub(self) mod v4;

pub use cf::{auth_client, featured_auth_client, format_error, kv_auth_client};
pub use feature::Feature;
pub use legacy::{auth_client, client};
pub use v4::{cf_api_client, format_error, CfApiClientConfig};
pub use legacy::{client, legacy_auth_client, legacy_featured_auth_client};

0 comments on commit f21dfcf

Please sign in to comment.