This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sends custom user agent with cloudflare-rs
- Loading branch information
1 parent
4d0168e
commit 3be4e7f
Showing
11 changed files
with
175 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; | ||
|
||
use crate::install; | ||
|
||
#[derive(Debug, Copy, Clone, PartialEq)] | ||
pub enum Feature { | ||
Sites, | ||
KV, | ||
} | ||
|
||
pub(super) fn headers(feature: Option<Feature>) -> HeaderMap { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
USER_AGENT, | ||
HeaderValue::from_str(&get_user_agent(feature)).unwrap(), | ||
); | ||
headers | ||
} | ||
|
||
fn get_user_agent(feature: Option<Feature>) -> String { | ||
let version = if install::target::DEBUG { | ||
"dev" | ||
} else { | ||
env!("CARGO_PKG_VERSION") | ||
}; | ||
|
||
let mut agent = format!("wrangler/{}", version); | ||
if let Some(feature) = feature { | ||
agent.push_str("/"); | ||
match feature { | ||
Feature::Sites => agent.push_str("sites"), | ||
Feature::KV => agent.push_str("kv"), | ||
} | ||
} | ||
agent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use reqwest::blocking::{Client, ClientBuilder}; | ||
use reqwest::header::{HeaderMap, HeaderValue}; | ||
use reqwest::redirect::Policy; | ||
use std::time::Duration; | ||
|
||
use crate::http::{feature::headers, Feature}; | ||
use crate::settings::global_user::GlobalUser; | ||
|
||
// TODO: remove this and replace it entirely with cloudflare-rs | ||
pub fn client(feature: Option<Feature>) -> Client { | ||
builder() | ||
.default_headers(headers(feature)) | ||
.build() | ||
.expect("could not create http client") | ||
} | ||
|
||
pub fn auth_client(feature: Option<Feature>, user: &GlobalUser) -> Client { | ||
let mut headers = headers(feature); | ||
add_auth_headers(&mut headers, user); | ||
|
||
builder() | ||
.default_headers(headers) | ||
.redirect(Policy::none()) | ||
.build() | ||
.expect("could not create authenticated http client") | ||
} | ||
|
||
fn builder() -> ClientBuilder { | ||
let builder = reqwest::blocking::Client::builder(); | ||
builder | ||
.connect_timeout(Duration::from_secs(10)) | ||
.timeout(Duration::from_secs(30)) | ||
} | ||
|
||
fn add_auth_headers<'a>(headers: &'a mut HeaderMap, user: &GlobalUser) { | ||
match user { | ||
GlobalUser::TokenAuth { api_token } => { | ||
headers.insert( | ||
"Authorization", | ||
HeaderValue::from_str(&format!("Bearer {}", &api_token)).unwrap(), | ||
); | ||
} | ||
GlobalUser::GlobalKeyAuth { email, api_key } => { | ||
headers.insert("X-Auth-Email", HeaderValue::from_str(&email).unwrap()); | ||
headers.insert("X-Auth-Key", HeaderValue::from_str(&api_key).unwrap()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub(self) mod feature; | ||
pub(self) mod legacy; | ||
pub(self) mod v4; | ||
|
||
pub use feature::Feature; | ||
pub use legacy::{auth_client, client}; | ||
pub use v4::{cf_v4_api_client, format_error}; |
Oops, something went wrong.