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 Aug 5, 2024
1 parent 0e6260b commit c106591
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl<T> Insert<T> {

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

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

if let Some(user) = &client.user {
builder = builder.header("X-ClickHouse-User", user);
}
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct Client {
password: Option<String>,
compression: Compression,
options: HashMap<String, String>,
headers: HashMap<String, String>,
}

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

Expand Down Expand Up @@ -180,6 +182,22 @@ impl Client {
self
}

/// Used to specify a header that will be passed to all queries.
///
/// # Example
/// ```
/// # use clickhouse::Client;
/// Client::default().with_header("Cookie", "A=1");
/// ```
pub fn with_header(
mut self,
name: impl Into<String>,
value: impl Into<String>,
) -> Self {
self.headers.insert(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 @@ -160,6 +160,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, value);
}

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

0 comments on commit c106591

Please sign in to comment.