From b14d844a6336a3bc48c59caabd0ccb4f07f45acd Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Wed, 2 Jun 2021 14:04:44 -0400 Subject: [PATCH] support custom request header --- src/api.rs | 8 +++++++- src/config.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 6c1d3b423a..09b4d8185b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -420,8 +420,9 @@ impl Api { handle.transfer_encoding(self.config.allow_transfer_encoding())?; let env = self.config.get_pipeline_env(); + let custom_header = self.config.get_custom_header(); - ApiRequest::create(handle, &method, &url, auth, env) + ApiRequest::create(handle, &method, &url, auth, env, custom_header) } /// Convenience method that performs a `GET` request. @@ -1519,6 +1520,7 @@ impl ApiRequest { url: &str, auth: Option<&Auth>, pipeline_env: Option, + custom_header: Option, ) -> ApiResult { debug!("request {} {}", method, url); @@ -1539,6 +1541,10 @@ impl ApiRequest { } } + if let Some(custom_header) = custom_header { + headers.append(&custom_header).ok(); + } + match method { Method::Get => handle.get(true)?, Method::Head => { diff --git a/src/config.rs b/src/config.rs index b560465382..fcdb2ad52a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -315,6 +315,15 @@ impl Config { }) } + /// Return the custom header added to all requests. + pub fn get_custom_header(&self) -> Option { + env::var("CUSTOM_HEADER").ok().or_else(|| { + self.ini + .get_from(Some("defaults"), "custom_header") + .map(str::to_owned) + }) + } + /// Returns the defaults for org and project. pub fn get_org_and_project_defaults(&self) -> (Option, Option) { (