fix: Don't send auth header if token specified is empty string

Not all endpoints need authorization, sometimes we don't have it!
pull/24376/head
Carol (Nichols || Goulding) 2021-03-25 16:26:54 -04:00
parent ebb6bbd13c
commit 27a45aa036
3 changed files with 23 additions and 26 deletions

View File

@ -35,13 +35,9 @@ mod tests {
#[tokio::test]
async fn ready() {
let token = "some-token";
let mock_server = mock("GET", "/ready").create();
let mock_server = mock("GET", "/ready")
.match_header("Authorization", format!("Token {}", token).as_str())
.create();
let client = Client::new(&mockito::server_url(), token);
let client = Client::new(&mockito::server_url(), "");
let _result = client.ready().await;

View File

@ -119,13 +119,9 @@ mod tests {
#[tokio::test]
async fn is_onboarding_allowed() {
let token = "some-token";
let mock_server = mock("GET", "/api/v2/setup").create();
let mock_server = mock("GET", "/api/v2/setup")
.match_header("Authorization", format!("Token {}", token).as_str())
.create();
let client = Client::new(&mockito::server_url(), token);
let client = Client::new(&mockito::server_url(), "");
let _result = client.is_onboarding_allowed().await;
@ -142,7 +138,6 @@ mod tests {
let retention_period_hrs = 1;
let mock_server = mock("POST", "/api/v2/setup")
.match_header("Authorization", format!("Token {}", token).as_str())
.match_body(
format!(
r#"{{"username":"{}","org":"{}","bucket":"{}","password":"{}","retentionPeriodHrs":{}}}"#,
@ -204,13 +199,11 @@ mod tests {
#[tokio::test]
async fn onboarding_opt() {
let token = "some-token";
let username = "some-user";
let org = "some-org";
let bucket = "some-bucket";
let mock_server = mock("POST", "/api/v2/setup")
.match_header("Authorization", format!("Token {}", token).as_str())
.match_body(
format!(
r#"{{"username":"{}","org":"{}","bucket":"{}"}}"#,
@ -220,7 +213,7 @@ mod tests {
)
.create();
let client = Client::new(&mockito::server_url(), token);
let client = Client::new(&mockito::server_url(), "");
let _result = client
.onboarding(username, org, bucket, None, None, None)

View File

@ -64,10 +64,7 @@ use futures::{Stream, StreamExt};
use reqwest::{Body, Method};
use serde::Serialize;
use snafu::{ResultExt, Snafu};
use std::{
fmt,
io::{self, Write},
};
use std::io::{self, Write};
pub mod data_point;
pub use data_point::{DataPoint, FieldValue, WriteDataPoint};
@ -106,7 +103,7 @@ pub enum RequestError {
pub struct Client {
/// The base URL this client sends requests to
pub url: String,
auth_header: String,
auth_header: Option<String>,
reqwest: reqwest::Client,
}
@ -120,19 +117,30 @@ impl Client {
/// ```
/// let client = influxdb2_client::Client::new("http://localhost:8888", "my-token");
/// ```
pub fn new(url: impl Into<String>, auth_token: impl fmt::Display) -> Self {
pub fn new(url: impl Into<String>, auth_token: impl Into<String>) -> Self {
let token = auth_token.into();
let auth_header = if token.is_empty() {
None
} else {
Some(format!("Token {}", token))
};
Self {
url: url.into(),
auth_header: format!("Token {}", auth_token),
auth_header,
reqwest: reqwest::Client::new(),
}
}
/// Consolidate common request building code
fn request(&self, method: Method, url: &str) -> reqwest::RequestBuilder {
self.reqwest
.request(method, url)
.header("Authorization", &self.auth_header)
let mut req = self.reqwest.request(method, url);
if let Some(auth) = &self.auth_header {
req = req.header("Authorization", auth);
}
req
}
/// Write line protocol data to the specified organization and bucket.