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

Commit

Permalink
Merge pull request #1070 from cloudflare/avery/uniform-user-agent
Browse files Browse the repository at this point in the history
Sends custom user agent with cloudflare-rs
  • Loading branch information
EverlastingBugstopper authored Apr 7, 2020
2 parents c9c5bbe + c0e3f73 commit 9b2c231
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 180 deletions.
3 changes: 1 addition & 2 deletions src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::path::PathBuf;

use cloudflare::endpoints::user::{GetUserDetails, GetUserTokenStatus};
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::HttpApiClientConfig;

use crate::http;
use crate::settings::global_user::{get_global_config_path, GlobalUser};
Expand Down Expand Up @@ -46,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_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_client(user)?;

match user {
GlobalUser::TokenAuth { .. } => {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/kv/key/get.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO:(gabbi) This file should use cloudflare-rs instead of our http::auth_client
// TODO:(gabbi) This file should use cloudflare-rs instead of our http::legacy_auth_client
// when https://github.com/cloudflare/cloudflare-rs/issues/26 is handled (this is
// because the GET key operation doesn't return json on success--just the raw
// value).
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/commands/kv/key/put.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO: (gabbi) This file should use cloudflare-rs instead of our http::auth_client
// TODO: (gabbi) This file should use cloudflare-rs instead of our http::legacy_auth_client
// when https://github.com/cloudflare/cloudflare-rs/issues/26 is handled (this is
// because the SET key request body is not json--it is the raw value).

Expand Down 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
22 changes: 12 additions & 10 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::collections::HashSet;
use std::time::Duration;

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

use percent_encoding::{percent_encode, PATH_SEGMENT_ENCODE_SET};

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

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

pub mod bucket;
pub mod bulk;
Expand All @@ -19,14 +20,15 @@ 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> {
http::cf_v4_api_client(
user,
HttpApiClientConfig {
default_headers: http::headers(None),
// Use 5 minute timeout instead of default 30-second one.
// This is useful for bulk upload operations.
http_timeout: Duration::from_secs(5 * 60),
},
let config = HttpApiClientConfig {
http_timeout: Duration::from_secs(5 * 60),
default_headers: headers(None),
};

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

Expand Down
2 changes: 1 addition & 1 deletion src/commands/preview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn preview(
script_id, session, https as u8, preview_host
);

let client = http::client(None);
let client = http::client();

let worker_res = match method {
HTTPMethod::Get => get(cookie, &client)?,
Expand Down
13 changes: 5 additions & 8 deletions 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 Expand Up @@ -97,8 +97,7 @@ pub fn upload(
failure::bail!(SITES_UNAUTH_PREVIEW_ERR)
}

let client = http::client(None);
unauthenticated_upload(&client, &target)?
unauthenticated_upload(&target)?
}
}
None => {
Expand All @@ -114,9 +113,7 @@ pub fn upload(
failure::bail!(SITES_UNAUTH_PREVIEW_ERR)
}

let client = http::client(None);

unauthenticated_upload(&client, &target)?
unauthenticated_upload(&target)?
}
};

Expand Down Expand Up @@ -179,7 +176,7 @@ fn authenticated_upload(
Ok(Preview::from(response.result))
}

fn unauthenticated_upload(client: &Client, target: &Target) -> Result<Preview, failure::Error> {
fn unauthenticated_upload(target: &Target) -> Result<Preview, failure::Error> {
let create_address = "https://cloudflareworkers.com/script";
log::info!("address: {}", create_address);

Expand All @@ -196,7 +193,7 @@ fn unauthenticated_upload(client: &Client, target: &Target) -> Result<Preview, f
} else {
upload::form::build(&target, None)?
};

let client = http::client();
let res = client
.post(create_address)
.multipart(script_upload_form)
Expand Down
11 changes: 5 additions & 6 deletions src/commands/publish.rs → src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::commands::kv;
use crate::commands::kv::bucket::{sync, upload_files};
use crate::commands::kv::bulk::delete::delete_bulk;
use crate::deploy;
use crate::http;
use crate::http::{self, Feature};
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::{DeployConfig, KvNamespace, Target};
use crate::terminal::{emoji, message};
Expand Down 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("site"), user);
let upload_client = http::featured_legacy_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("bucket")
http::featured_legacy_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
5 changes: 2 additions & 3 deletions src/commands/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ extern crate serde_json;

use cloudflare::endpoints::workers::{DeleteRoute, ListRoutes};
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::HttpApiClientConfig;

use crate::http;
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_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_client(user)?;

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

let result = client.request(&DeleteRoute {
zone_identifier: &zone_identifier,
Expand Down
9 changes: 4 additions & 5 deletions src/commands/secret/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cloudflare::endpoints::workers::{CreateSecret, CreateSecretParams, DeleteSecret, ListSecrets};
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::response::ApiFailure;
use cloudflare::framework::HttpApiClientConfig;

use crate::http;
use crate::settings::global_user::GlobalUser;
Expand Down Expand Up @@ -58,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 @@ -85,7 +84,7 @@ pub fn create_secret(name: &str, user: &GlobalUser, target: &Target) -> Result<(
target.name
));

let client = http::cf_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_client(user)?;

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

let client = http::cf_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_client(user)?;

let response = client.request(&DeleteSecret {
account_identifier: &target.account_id,
Expand All @@ -162,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_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_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_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_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
11 changes: 5 additions & 6 deletions src/deploy/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use serde::Serialize;

use cloudflare::endpoints::workers::{CreateRoute, CreateRouteParams, ListRoutes};
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::HttpApiClientConfig;

use crate::http::{cf_v4_api_client, format_error};
use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::{Route, Zoned};

Expand All @@ -29,11 +28,11 @@ pub fn publish_routes(
}

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

let routes: Vec<Route> = match client.request(&ListRoutes { zone_identifier }) {
Ok(success) => success.result.iter().map(Route::from).collect(),
Err(e) => failure::bail!("{}", format_error(e, None)), // TODO: add suggestion fn
Err(e) => failure::bail!("{}", http::format_error(e, None)), // TODO: add suggestion fn
};

Ok(routes)
Expand All @@ -44,7 +43,7 @@ fn create(
zone_identifier: &str,
route: &Route,
) -> Result<Route, failure::Error> {
let client = cf_v4_api_client(user, HttpApiClientConfig::default())?;
let client = http::cf_v4_client(user)?;

log::info!("Creating your route {:#?}", &route.pattern,);
match client.request(&CreateRoute {
Expand All @@ -59,7 +58,7 @@ fn create(
pattern: route.pattern.clone(),
script: route.script.clone(),
}),
Err(e) => failure::bail!("{}", format_error(e, Some(&routes_error_help))),
Err(e) => failure::bail!("{}", http::format_error(e, Some(&routes_error_help))),
}
}

Expand Down
Loading

0 comments on commit 9b2c231

Please sign in to comment.