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 #267 from Electroid/http-auth
Browse files Browse the repository at this point in the history
Add an authenticated HTTP client
  • Loading branch information
xtuc authored Jul 1, 2019
2 parents d7289ca + f3fd8cc commit 819f147
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
21 changes: 4 additions & 17 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn create_kv_namespaces(user: &GlobalUser, project: &Project) -> Result<(),
project.account_id,
);

let client = http::client();
let client = http::auth_client(user);

if let Some(namespaces) = &project.kv_namespaces {
for namespace in namespaces {
Expand All @@ -54,12 +54,7 @@ pub fn create_kv_namespaces(user: &GlobalUser, project: &Project) -> Result<(),
let mut map = HashMap::new();
map.insert("title", namespace);

let request = client
.post(&kv_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.json(&map)
.send();
let request = client.post(&kv_addr).json(&map).send();

if let Err(error) = request {
// A 400 is returned if the account already owns a namespace with this title.
Expand Down Expand Up @@ -94,25 +89,21 @@ fn publish_script(
project.account_id, project.name,
);

let client = http::client();
let client = http::auth_client(user);

let project_type = &project.project_type;
let mut res = match project_type {
ProjectType::Rust => {
info!("Rust project detected. Publishing...");
client
.put(&worker_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.multipart(build_multipart_script()?)
.send()?
}
ProjectType::JavaScript => {
info!("JavaScript project detected. Publishing...");
client
.put(&worker_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.header("Content-Type", "application/javascript")
.body(build_js_script()?)
.send()?
Expand All @@ -121,8 +112,6 @@ fn publish_script(
info!("Webpack project detected. Publishing...");
client
.put(&worker_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.multipart(build_webpack_form()?)
.send()?
}
Expand Down Expand Up @@ -162,13 +151,11 @@ fn make_public_on_subdomain(project: &Project, user: &GlobalUser) -> Result<(),
project.account_id, project.name,
);

let client = http::client();
let client = http::auth_client(user);

info!("Making public on subdomain...");
let mut res = client
.post(&sd_worker_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.header("Content-type", "application/json")
.body(build_subdomain_request())
.send()?;
Expand Down
12 changes: 3 additions & 9 deletions src/commands/publish/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ impl Route {
fn get_routes(user: &GlobalUser, project: &Project) -> Result<Vec<Route>, failure::Error> {
let routes_addr = get_routes_addr(project)?;

let client = http::client();
let client = http::auth_client(user);

let mut res = client
.get(&routes_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.send()?;
let mut res = client.get(&routes_addr).send()?;

if !res.status().is_success() {
let msg = format!(
Expand All @@ -87,16 +83,14 @@ fn get_routes(user: &GlobalUser, project: &Project) -> Result<Vec<Route>, failur
}

fn create(user: &GlobalUser, project: &Project, route: &Route) -> Result<(), failure::Error> {
let client = http::client();
let client = http::auth_client(user);
let body = serde_json::to_string(&route)?;

let routes_addr = get_routes_addr(project)?;

info!("Creating your route {:#?}", &route.pattern,);
let mut res = client
.post(&routes_addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.header(CONTENT_TYPE, "application/json")
.body(body)
.send()?;
Expand Down
17 changes: 4 additions & 13 deletions src/commands/subdomain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ impl Subdomain {
pub fn get(account_id: &str, user: &GlobalUser) -> Result<String, failure::Error> {
let addr = subdomain_addr(account_id);

let client = http::client();
let client = http::auth_client(user);

let mut res = client
.get(&addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.send()?;
let mut res = client.get(&addr).send()?;

if !res.status().is_success() {
failure::bail!(
Expand Down Expand Up @@ -80,14 +76,9 @@ pub fn subdomain(name: &str, user: &GlobalUser, project: &Project) -> Result<(),
};
let sd_request = serde_json::to_string(&sd)?;

let client = http::client();
let client = http::auth_client(user);

let mut res = client
.put(&addr)
.header("X-Auth-Key", &*user.api_key)
.header("X-Auth-Email", &*user.email)
.body(sd_request)
.send()?;
let mut res = client.put(&addr).body(sd_request).send()?;

let msg;
if !res.status().is_success() {
Expand Down
39 changes: 29 additions & 10 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use reqwest::Client;
use reqwest::{Client, ClientBuilder, RedirectPolicy};
use std::time::Duration;

use crate::install;
use crate::settings::global_user::GlobalUser;

fn client_headers() -> HeaderMap {
let user_agent = if install::target::DEBUG {
"wrangler/dev".to_string()
fn headers() -> HeaderMap {
let version = if install::target::DEBUG {
"dev"
} else {
let version = env!("CARGO_PKG_VERSION");
format!("wrangler/{}", version)
env!("CARGO_PKG_VERSION")
};
let user_agent = format!("wrangler/{}", version);

let value = HeaderValue::from_str(&user_agent).unwrap();
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, value);
headers.insert(USER_AGENT, HeaderValue::from_str(&user_agent).unwrap());
headers
}

pub fn client() -> Client {
fn builder() -> ClientBuilder {
let builder = reqwest::Client::builder();
builder
.default_headers(client_headers())
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(30))
}

pub fn client() -> Client {
builder()
.default_headers(headers())
.build()
.expect("could not create http client")
}

pub fn auth_client(user: &GlobalUser) -> Client {
let mut headers = headers();
headers.insert("X-Auth-Key", HeaderValue::from_str(&user.api_key).unwrap());
headers.insert("X-Auth-Email", HeaderValue::from_str(&user.email).unwrap());

builder()
.default_headers(headers)
.redirect(RedirectPolicy::none())
.build()
.expect("could not create authenticated http client")
}

0 comments on commit 819f147

Please sign in to comment.