Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/qdrant_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ impl Qdrant {
Ok(result)
}

/// Create a lightweight clone of this client with an additional header.
///
/// The returned client shares the same connection pool but sends
/// the extra header with every request. Useful for per-request
/// tracing IDs or other contextual metadata.
///
/// ```no_run
/// use qdrant_client::Qdrant;
///
///# async fn example(client: &Qdrant) -> Result<(), qdrant_client::QdrantError> {
/// let traced = client.with_header("x-request-id", "abc-123");
/// // traced shares the same connection pool, but adds the header to every request
///# Ok(())
///# }
/// ```
pub fn with_header(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
let mut clone = self.clone();
clone.config.custom_headers.push((key.into(), value.into()));
clone
}

/// Health check.
///
/// Do a health check and fetch server information such as the current version and commit.
Expand Down
1 change: 1 addition & 0 deletions tests/snippet_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ mod test_upsert_points;
mod test_upsert_points_fallback_shard_key;
mod test_upsert_points_insert_only;
mod test_upsert_points_with_condition;
mod test_with_header;
32 changes: 32 additions & 0 deletions tests/snippet_tests/test_with_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#[tokio::test]
async fn test_with_header() {
async fn with_header() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/with_header.rs` file
use qdrant_client::Qdrant;

let client = Qdrant::from_url("http://localhost:6334")
.header("x-base", "base-value")
.skip_compatibility_check()
.build()
.unwrap();

let traced = client.with_header("x-request-id", "abc-123");

assert_eq!(
traced.config.custom_headers,
vec![
("x-base".to_string(), "base-value".to_string()),
("x-request-id".to_string(), "abc-123".to_string()),
]
);
// Original still has only the base header
assert_eq!(
client.config.custom_headers,
vec![("x-base".to_string(), "base-value".to_string())]
);
Ok(())
}
let _ = with_header().await;
}
22 changes: 22 additions & 0 deletions tests/snippets/with_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use qdrant_client::Qdrant;

let client = Qdrant::from_url("http://localhost:6334")
.header("x-base", "base-value")
.skip_compatibility_check()
.build()
.unwrap();

let traced = client.with_header("x-request-id", "abc-123");

assert_eq!(
traced.config.custom_headers,
vec![
("x-base".to_string(), "base-value".to_string()),
("x-request-id".to_string(), "abc-123".to_string()),
]
);
// Original still has only the base header
assert_eq!(
client.config.custom_headers,
vec![("x-base".to_string(), "base-value".to_string())]
);
Loading