11use std:: time:: Duration ;
22
3- use crate :: { Qdrant , QdrantError } ;
3+ use tonic:: service:: Interceptor ;
4+
5+ use crate :: { auth:: TokenInterceptor , Qdrant , QdrantError } ;
6+
7+ struct DefaultConfigValues {
8+ timeout : Duration ,
9+ connect_timeout : Duration ,
10+ keep_alive_while_idle : bool ,
11+ compression : Option < CompressionEncoding > ,
12+ check_compatibility : bool ,
13+ pool_size : usize ,
14+ }
15+
16+ impl Default for DefaultConfigValues {
17+ fn default ( ) -> Self {
18+ Self {
19+ timeout : Duration :: from_secs ( 5 ) ,
20+ connect_timeout : Duration :: from_secs ( 5 ) ,
21+ keep_alive_while_idle : true ,
22+ compression : None ,
23+ check_compatibility : true ,
24+ pool_size : 3 ,
25+ }
26+ }
27+ }
428
529/// Qdrant client configuration
630///
@@ -17,7 +41,7 @@ use crate::{Qdrant, QdrantError};
1741/// .build();
1842/// ```
1943#[ derive( Clone ) ]
20- pub struct QdrantConfig {
44+ pub struct QdrantConfig < I : Send + Sync + ' static + Clone + Interceptor = TokenInterceptor > {
2145 /// Qdrant server URI to connect to
2246 pub uri : String ,
2347
@@ -30,9 +54,6 @@ pub struct QdrantConfig {
3054 /// Whether to keep idle connections active
3155 pub keep_alive_while_idle : bool ,
3256
33- /// Optional API key or token to use for authorization
34- pub api_key : Option < String > ,
35-
3657 /// Optional compression schema to use for API requests
3758 pub compression : Option < CompressionEncoding > ,
3859
@@ -42,51 +63,28 @@ pub struct QdrantConfig {
4263 /// Amount of concurrent connections.
4364 /// If set to 0 or 1, connection pools will be disabled.
4465 pub pool_size : usize ,
66+
67+ /// The interceptor to use for modifying requests
68+ pub interceptor : I ,
4569}
4670
47- impl QdrantConfig {
48- /// Start configuring a Qdrant client with an URL
49- ///
50- /// ```rust,no_run
51- ///# use qdrant_client::config::QdrantConfig;
52- /// let client = QdrantConfig::from_url("http://localhost:6334").build();
53- /// ```
54- ///
55- /// This is normally done through [`Qdrant::from_url`](crate::Qdrant::from_url).
56- pub fn from_url ( url : & str ) -> Self {
57- QdrantConfig {
71+ impl < I : Send + Sync + ' static + Clone + Interceptor > QdrantConfig < I > {
72+ fn with_defaults ( url : & str , interceptor : I ) -> Self {
73+ let defaults = DefaultConfigValues :: default ( ) ;
74+ Self {
5875 uri : url. to_string ( ) ,
59- ..Self :: default ( )
76+ timeout : defaults. timeout ,
77+ connect_timeout : defaults. connect_timeout ,
78+ keep_alive_while_idle : defaults. keep_alive_while_idle ,
79+ compression : defaults. compression ,
80+ check_compatibility : defaults. check_compatibility ,
81+ pool_size : defaults. pool_size ,
82+ interceptor,
6083 }
6184 }
6285
63- /// Set an optional API key
64- ///
65- /// # Examples
66- ///
67- /// A typical use case might be getting the key from an environment variable:
68- ///
69- /// ```rust,no_run
70- /// use qdrant_client::Qdrant;
71- ///
72- /// let client = Qdrant::from_url("http://localhost:6334")
73- /// .api_key(std::env::var("QDRANT_API_KEY"))
74- /// .build();
75- /// ```
76- ///
77- /// Or you might get it from some configuration:
78- ///
79- /// ```rust,no_run
80- ///# use std::collections::HashMap;
81- ///# let config: HashMap<&str, String> = HashMap::new();
82- ///# use qdrant_client::Qdrant;
83- /// let client = Qdrant::from_url("http://localhost:6334")
84- /// .api_key(config.get("api_key"))
85- /// .build();
86- /// ```
87- pub fn api_key ( mut self , api_key : impl AsOptionApiKey ) -> Self {
88- self . api_key = api_key. api_key ( ) ;
89- self
86+ pub fn from_url_with_interceptor ( url : & str , interceptor : I ) -> Self {
87+ Self :: with_defaults ( url, interceptor)
9088 }
9189
9290 /// Keep the connection alive while idle
@@ -138,13 +136,6 @@ impl QdrantConfig {
138136 self
139137 }
140138
141- /// Set an API key
142- ///
143- /// Also see [`api_key()`](fn@Self::api_key).
144- pub fn set_api_key ( & mut self , api_key : & str ) {
145- self . api_key = Some ( api_key. to_string ( ) ) ;
146- }
147-
148139 /// Set the timeout for this client
149140 ///
150141 /// Also see [`timeout()`](fn@Self::timeout).
@@ -174,7 +165,7 @@ impl QdrantConfig {
174165 }
175166
176167 /// Build the configured [`Qdrant`] client
177- pub fn build ( self ) -> Result < Qdrant , QdrantError > {
168+ pub fn build ( self ) -> Result < Qdrant < I > , QdrantError > {
178169 Qdrant :: new ( self )
179170 }
180171
@@ -190,21 +181,52 @@ impl QdrantConfig {
190181 }
191182}
192183
184+ impl QdrantConfig < TokenInterceptor > {
185+ /// Start configuring a Qdrant client with an URL
186+ ///
187+ /// ```rust,no_run
188+ ///# use qdrant_client::config::QdrantConfig;
189+ /// let client = QdrantConfig::from_url("http://localhost:6334").build();
190+ /// ```
191+ ///
192+ /// This is normally done through [`Qdrant::from_url`](crate::Qdrant::from_url).
193+ pub fn from_url ( url : & str ) -> Self {
194+ Self :: with_defaults ( url, TokenInterceptor :: new ( None ) )
195+ }
196+
197+ /// Set an optional API key
198+ ///
199+ /// This method is only available when using the default TokenInterceptor.
200+ /// When you set an API key, it automatically configures the TokenInterceptor.
201+ ///
202+ /// # Examples
203+ ///
204+ /// ```rust,no_run
205+ /// use qdrant_client::Qdrant;
206+ ///
207+ /// let client = Qdrant::from_url("http://localhost:6334")
208+ /// .api_key(std::env::var("QDRANT_API_KEY"))
209+ /// .build();
210+ /// ```
211+ pub fn api_key ( mut self , api_key : impl AsOptionApiKey ) -> Self {
212+ self . interceptor = TokenInterceptor :: new ( api_key. api_key ( ) ) ;
213+ self
214+ }
215+
216+ /// Set an API key
217+ ///
218+ /// Also see [`api_key()`](fn@Self::api_key).
219+ pub fn set_api_key ( & mut self , api_key : & str ) {
220+ self . interceptor = TokenInterceptor :: new ( Some ( api_key. to_string ( ) ) ) ;
221+ }
222+ }
223+
193224/// Default Qdrant client configuration.
194225///
195226/// Connects to `http://localhost:6334` without an API key.
196- impl Default for QdrantConfig {
227+ impl Default for QdrantConfig < TokenInterceptor > {
197228 fn default ( ) -> Self {
198- Self {
199- uri : String :: from ( "http://localhost:6334" ) ,
200- timeout : Duration :: from_secs ( 5 ) ,
201- connect_timeout : Duration :: from_secs ( 5 ) ,
202- keep_alive_while_idle : true ,
203- api_key : None ,
204- compression : None ,
205- check_compatibility : true ,
206- pool_size : 3 ,
207- }
229+ Self :: with_defaults ( "http://localhost:6334" , TokenInterceptor :: new ( None ) )
208230 }
209231}
210232
0 commit comments