Skip to content

Commit 1af9bcf

Browse files
committed
Provide with_header method on client
This allows arbitrary headers to be included in requests to ClickHouse.
1 parent 24b39c7 commit 1af9bcf

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ thiserror = "1.0.16"
5151
serde = "1.0.106"
5252
bytes = "1.5.0"
5353
tokio = { version = "1.0.1", features = ["rt", "macros"] }
54+
http = "0.2"
5455
hyper = { version = "0.14", features = ["client", "tcp", "http1", "stream"] }
5556
hyper-tls = { version = "0.5.0", optional = true }
5657
url = "2.1.1"

src/insert.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<T> Insert<T> {
8787

8888
let mut builder = Request::post(url.as_str());
8989

90+
for (name, value) in &client.headers {
91+
builder = builder.header(name.clone(), value.clone());
92+
}
93+
9094
if let Some(user) = &client.user {
9195
builder = builder.header("X-ClickHouse-User", user);
9296
}

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate static_assertions;
88

99
use std::{collections::HashMap, sync::Arc, time::Duration};
1010

11+
use http::{HeaderMap, HeaderName, HeaderValue};
1112
use hyper::client::connect::HttpConnector;
1213
#[cfg(feature = "tls")]
1314
use hyper_tls::HttpsConnector;
@@ -57,6 +58,7 @@ pub struct Client {
5758
password: Option<String>,
5859
compression: Compression,
5960
options: HashMap<String, String>,
61+
headers: HeaderMap,
6062
}
6163

6264
impl Default for Client {
@@ -93,6 +95,7 @@ impl Client {
9395
password: None,
9496
compression: Compression::default(),
9597
options: HashMap::new(),
98+
headers: HeaderMap::new(),
9699
}
97100
}
98101

@@ -170,6 +173,22 @@ impl Client {
170173
self
171174
}
172175

176+
/// Used to specify a header that will be passed to all queries.
177+
///
178+
/// # Example
179+
/// ```
180+
/// # use clickhouse::Client;
181+
/// Client::default().with_option("allow_nondeterministic_mutations", "1");
182+
/// ```
183+
pub fn with_header(
184+
mut self,
185+
name: impl Into<HeaderName>,
186+
value: impl Into<HeaderValue>,
187+
) -> Self {
188+
self.headers.append(name.into(), value.into());
189+
self
190+
}
191+
173192
/// Starts a new INSERT statement.
174193
///
175194
/// # Panics

src/query.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ impl Query {
152152

153153
let mut builder = Request::builder().method(method).uri(url.as_str());
154154

155+
for (name, value) in &self.client.headers {
156+
builder = builder.header(name.clone(), value.clone());
157+
}
158+
155159
if content_length == 0 {
156160
builder = builder.header(CONTENT_LENGTH, "0");
157161
} else {

0 commit comments

Comments
 (0)