diff --git a/src/qdrant_client/mod.rs b/src/qdrant_client/mod.rs index ae131be..ac9a4a4 100644 --- a/src/qdrant_client/mod.rs +++ b/src/qdrant_client/mod.rs @@ -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, value: impl Into) -> 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. diff --git a/tests/snippet_tests/mod.rs b/tests/snippet_tests/mod.rs index 221bc81..9253d8b 100644 --- a/tests/snippet_tests/mod.rs +++ b/tests/snippet_tests/mod.rs @@ -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; \ No newline at end of file diff --git a/tests/snippet_tests/test_with_header.rs b/tests/snippet_tests/test_with_header.rs new file mode 100644 index 0000000..b9934c6 --- /dev/null +++ b/tests/snippet_tests/test_with_header.rs @@ -0,0 +1,32 @@ + +#[tokio::test] +async fn test_with_header() { + async fn with_header() -> Result<(), Box> { + // 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; +} diff --git a/tests/snippets/with_header.rs b/tests/snippets/with_header.rs new file mode 100644 index 0000000..161a982 --- /dev/null +++ b/tests/snippets/with_header.rs @@ -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())] +); \ No newline at end of file