Skip to content

Commit 3713c03

Browse files
committed
Add default global HTTP headers (#85)
This commit adds the ability to add global HTTP headers to the TransportBuilder, to set HTTP headers for all API calls made by the client. HTTP headers can be overridden on a per API call basis by using the header function exposed on each builder struct. Closes #83 (cherry picked from commit 1e619d8)
1 parent 10ef56b commit 3713c03

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

elasticsearch/src/http/transport.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
error::Error,
77
http::{
88
headers::{
9-
HeaderMap, HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_TYPE, DEFAULT_ACCEPT,
9+
HeaderMap, HeaderName, HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_TYPE, DEFAULT_ACCEPT,
1010
DEFAULT_CONTENT_TYPE, DEFAULT_USER_AGENT, USER_AGENT,
1111
},
1212
request::Body,
@@ -86,6 +86,7 @@ pub struct TransportBuilder {
8686
proxy: Option<Url>,
8787
proxy_credentials: Option<Credentials>,
8888
disable_proxy: bool,
89+
headers: HeaderMap
8990
}
9091

9192
impl TransportBuilder {
@@ -103,6 +104,7 @@ impl TransportBuilder {
103104
proxy: None,
104105
proxy_credentials: None,
105106
disable_proxy: false,
107+
headers: HeaderMap::new()
106108
}
107109
}
108110

@@ -142,10 +144,32 @@ impl TransportBuilder {
142144
self
143145
}
144146

147+
/// Adds a HTTP header that will be added to all client API calls.
148+
///
149+
/// A default HTTP header can be overridden on a per API call basis.
150+
pub fn header(mut self, key: HeaderName, value: HeaderValue) -> Self {
151+
self.headers.insert(key, value);
152+
self
153+
}
154+
155+
/// Adds HTTP headers that will be added to all client API calls.
156+
///
157+
/// Default HTTP headers can be overridden on a per API call basis.
158+
pub fn headers(mut self, headers: HeaderMap) -> Self {
159+
for (key, value) in headers.iter() {
160+
self.headers.insert(key, value.clone());
161+
}
162+
self
163+
}
164+
145165
/// Builds a [Transport] to use to send API calls to Elasticsearch.
146166
pub fn build(self) -> Result<Transport, BuildError> {
147167
let mut client_builder = self.client_builder;
148168

169+
if self.headers.len() > 0 {
170+
client_builder = client_builder.default_headers(self.headers);
171+
}
172+
149173
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
150174
{
151175
if let Some(creds) = &self.credentials {

elasticsearch/tests/client.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,43 @@ async fn default_user_agent_content_type_accept_headers() -> Result<(), failure:
3030
Ok(())
3131
}
3232

33+
#[tokio::test]
34+
async fn default_header() -> Result<(), failure::Error> {
35+
let server = server::http(move |req| async move {
36+
assert_eq!(req.headers()["x-opaque-id"], "foo");
37+
http::Response::default()
38+
});
39+
40+
let builder = client::create_builder(format!("http://{}", server.addr()).as_ref())
41+
.header(HeaderName::from_static(X_OPAQUE_ID),
42+
HeaderValue::from_static("foo"));
43+
44+
let client = client::create(builder);
45+
let _response = client.ping().send().await?;
46+
47+
Ok(())
48+
}
49+
50+
#[tokio::test]
51+
async fn override_default_header() -> Result<(), failure::Error> {
52+
let server = server::http(move |req| async move {
53+
assert_eq!(req.headers()["x-opaque-id"], "bar");
54+
http::Response::default()
55+
});
56+
57+
let builder = client::create_builder(format!("http://{}", server.addr()).as_ref())
58+
.header(HeaderName::from_static(X_OPAQUE_ID),
59+
HeaderValue::from_static("foo"));
60+
61+
let client = client::create(builder);
62+
let _response = client.ping()
63+
.header(HeaderName::from_static(X_OPAQUE_ID),
64+
HeaderValue::from_static("bar"))
65+
.send().await?;
66+
67+
Ok(())
68+
}
69+
3370
#[tokio::test]
3471
async fn x_opaque_id_header() -> Result<(), failure::Error> {
3572
let server = server::http(move |req| async move {

0 commit comments

Comments
 (0)