Skip to content

Commit 86fe62c

Browse files
committed
fix: rust docs
1 parent c44a99a commit 86fe62c

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/external_api_keys.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ impl Interceptor for ExternalApiKeysInterceptor {
1818
fn call(&mut self, mut request: Request<()>) -> anyhow::Result<Request<()>, Status> {
1919
if let Some(ext_api_keys) = &self.external_api_keys {
2020
for (k, v) in ext_api_keys {
21+
// Treat empty values as "missing key" (e.g. absent env var), so requests can proceed.
22+
if v.trim().is_empty() {
23+
continue;
24+
}
2125
let key = MetadataKey::from_bytes(k.as_bytes())
2226
.map_err(|_| Status::invalid_argument(format!("Invalid metadata key: {k}")))?;
2327
let value = MetadataValue::try_from(v.as_str()).map_err(|_| {
@@ -103,4 +107,28 @@ mod tests {
103107
assert_eq!(error.code(), tonic::Code::InvalidArgument);
104108
assert!(error.message().contains("Invalid metadata value"));
105109
}
110+
111+
#[test]
112+
fn skips_empty_external_api_key_values() {
113+
let api_keys = HashMap::from([
114+
("openai-api-key".to_string(), "".to_string()),
115+
("cohere-api-key".to_string(), "cohere-secret".to_string()),
116+
]);
117+
118+
let mut interceptor = ExternalApiKeysInterceptor::new(Some(api_keys));
119+
let request = interceptor
120+
.call(Request::new(()))
121+
.expect("interceptor must ignore empty external API key values");
122+
123+
assert!(request.metadata().get("openai-api-key").is_none());
124+
assert_eq!(
125+
request
126+
.metadata()
127+
.get("cohere-api-key")
128+
.expect("cohere-api-key header must exist")
129+
.to_str()
130+
.expect("cohere-api-key header must be valid ASCII"),
131+
"cohere-secret"
132+
);
133+
}
106134
}

src/qdrant_client/config.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ impl QdrantConfig {
9898
/// # Examples
9999
/// ```rust,no_run
100100
///# use std::collections::HashMap;
101-
///# let config: HashMap<&str, String> = HashMap::new();
101+
///# let mut config: HashMap<&str, HashMap<String, String>> = HashMap::new();
102+
///# config.insert(
103+
///# "external_api_keys",
104+
///# HashMap::from([("openai-api-key".to_string(), "<YOUR_OPENAI_API_KEY>".to_string())]),
105+
///# );
102106
///# use qdrant_client::Qdrant;
103107
/// let client = Qdrant::from_url("http://localhost:6334")
104-
/// .external_api_keys(config.get("external_api_keys"))
108+
/// .external_api_keys(config.get("external_api_keys").cloned())
105109
/// .build();
106110
/// ```
107111
pub fn external_api_keys(mut self, external_api_keys: impl AsOptionExternalApiKeys) -> Self {
@@ -325,25 +329,27 @@ impl<E: Sized> AsOptionApiKey for Result<String, E> {
325329
}
326330
}
327331

328-
/// Set an optional API key from various types
332+
/// Set optional external API keys from various types
329333
///
330334
/// For example:
331335
///
332336
/// ```rust
333-
///# use std::time::Duration;
334337
///# use qdrant_client::Qdrant;
335338
///# let mut config = Qdrant::from_url("http://localhost:6334");
336339
/// config
337340
/// .external_api_keys(("openai-api-key", "<YOUR_OPENAI_API_KEY>"))
338341
/// .external_api_keys((String::from("openai-api-key"), String::from("<YOUR_OPENAI_API_KEY>")))
339-
/// .external_api_keys((String::from("openai-api-key").unwrap(), std::env::var("OPENAI_API_KEY").unwrap()));
342+
/// .external_api_keys((String::from("openai-api-key"), "<YOUR_OPENAI_API_KEY>".to_string()));
340343
/// ```
341344
///
342-
/// /// ```rust
343-
///# use std::time::Duration;
345+
/// ```rust
346+
///# use std::collections::HashMap;
344347
///# use qdrant_client::Qdrant;
345348
///# let mut config = Qdrant::from_url("http://localhost:6334");
346-
///# let ext_api_keys = HashMap::from([("openai-api-key", "<YOUR_OPENAI_API_KEY>"), ("cohere-api-key", "<YOUR_COHERE_API_KEY>")])
349+
///# let ext_api_keys = HashMap::from([
350+
///# ("openai-api-key".to_string(), "<YOUR_OPENAI_API_KEY>".to_string()),
351+
///# ("cohere-api-key".to_string(), "<YOUR_COHERE_API_KEY>".to_string()),
352+
///# ]);
347353
/// config
348354
/// .external_api_keys(ext_api_keys);
349355
/// ```

0 commit comments

Comments
 (0)