Skip to content

Commit

Permalink
Provide with_header method on client
Browse files Browse the repository at this point in the history
This allows arbitrary headers to be included in requests to ClickHouse.
  • Loading branch information
jongiddy committed Jun 12, 2024
1 parent 24b39c7 commit 1af9bcf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ thiserror = "1.0.16"
serde = "1.0.106"
bytes = "1.5.0"
tokio = { version = "1.0.1", features = ["rt", "macros"] }
http = "0.2"
hyper = { version = "0.14", features = ["client", "tcp", "http1", "stream"] }
hyper-tls = { version = "0.5.0", optional = true }
url = "2.1.1"
Expand Down
4 changes: 4 additions & 0 deletions src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ impl<T> Insert<T> {

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

for (name, value) in &client.headers {
builder = builder.header(name.clone(), value.clone());
}

if let Some(user) = &client.user {
builder = builder.header("X-ClickHouse-User", user);
}
Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate static_assertions;

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

use http::{HeaderMap, HeaderName, HeaderValue};
use hyper::client::connect::HttpConnector;
#[cfg(feature = "tls")]
use hyper_tls::HttpsConnector;
Expand Down Expand Up @@ -57,6 +58,7 @@ pub struct Client {
password: Option<String>,
compression: Compression,
options: HashMap<String, String>,
headers: HeaderMap,
}

impl Default for Client {
Expand Down Expand Up @@ -93,6 +95,7 @@ impl Client {
password: None,
compression: Compression::default(),
options: HashMap::new(),
headers: HeaderMap::new(),
}
}

Expand Down Expand Up @@ -170,6 +173,22 @@ impl Client {
self
}

/// Used to specify a header that will be passed to all queries.
///
/// # Example
/// ```
/// # use clickhouse::Client;
/// Client::default().with_option("allow_nondeterministic_mutations", "1");
/// ```
pub fn with_header(
mut self,
name: impl Into<HeaderName>,
value: impl Into<HeaderValue>,
) -> Self {
self.headers.append(name.into(), value.into());
self
}

/// Starts a new INSERT statement.
///
/// # Panics
Expand Down
4 changes: 4 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ impl Query {

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

for (name, value) in &self.client.headers {
builder = builder.header(name.clone(), value.clone());
}

if content_length == 0 {
builder = builder.header(CONTENT_LENGTH, "0");
} else {
Expand Down

0 comments on commit 1af9bcf

Please sign in to comment.