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.
Merge pull request #267 from Electroid/http-auth
Add an authenticated HTTP client
- Loading branch information
Showing
4 changed files
with
40 additions
and
49 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
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") | ||
} |