20
20
* #L%
21
21
*/
22
22
23
+ import com .datastax .astra .client .model .HttpClientOptions ;
24
+ import com .datastax .astra .internal .command .CommandObserver ;
25
+ import com .datastax .astra .internal .command .LoggingCommandObserver ;
23
26
import com .datastax .astra .internal .utils .Assert ;
24
27
import lombok .Getter ;
25
28
import lombok .Setter ;
26
29
import lombok .extern .slf4j .Slf4j ;
27
30
28
31
import java .net .http .HttpClient ;
32
+ import java .util .Map ;
33
+ import java .util .TreeMap ;
29
34
30
35
/**
31
36
* Options to set up the client for DataApiClient.
@@ -52,10 +57,10 @@ public class DataAPIOptions {
52
57
DataAPIOptions .class .getPackage ().getImplementationVersion () : "dev" ;
53
58
54
59
/** Default timeout for initiating connection. */
55
- public static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 20 ;
60
+ public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS_SECONDS = 20 ;
56
61
57
62
/** Default timeout for initiating connection. */
58
- public static final int DEFAULT_REQUEST_TIMEOUT_SECONDS = 20 ;
63
+ public static final int DEFAULT_REQUEST_TIMEOUT_MILLIS_SECONDS = 20000 ;
59
64
60
65
/** Default retry count. */
61
66
public static final int DEFAULT_RETRY_COUNT = 3 ;
@@ -84,6 +89,11 @@ public class DataAPIOptions {
84
89
/** The maximum number of documents that can be inserted in a single operation. */
85
90
final int maxDocumentsInInsert ;
86
91
92
+ /** The maximum number of documents that can be inserted in a single operation. */
93
+ final String embeddingAPIKey ;
94
+
95
+ final Map <String , CommandObserver > observers ;
96
+
87
97
/**
88
98
* Initializer for the builder.
89
99
*
@@ -106,15 +116,17 @@ private DataAPIOptions(DataAPIClientOptionsBuilder builder) {
106
116
this .maxDocumentCount = builder .maxDocumentCount ;
107
117
this .maxPageSize = builder .maxPageSize ;
108
118
this .maxDocumentsInInsert = builder .maxDocumentsInInsert ;
119
+ this .embeddingAPIKey = builder .embeddingAPIKey ;
109
120
this .httpClientOptions = new HttpClientOptions ();
121
+ this .observers = builder .observers ;
110
122
httpClientOptions .setHttpVersion (builder .httpVersion );
111
123
httpClientOptions .setHttpRedirect (builder .httpRedirect );
112
124
httpClientOptions .setRetryCount (builder .retryCount );
113
125
httpClientOptions .setRetryDelay (builder .retryDelay );
114
126
httpClientOptions .setUserAgentCallerName (builder .userAgentCallerName );
115
127
httpClientOptions .setUserAgentCallerVersion (builder .userAgentCallerVersion );
116
- httpClientOptions .setConnectionRequestTimeoutInSeconds (builder .httpRequestTimeout );
117
- httpClientOptions .setResponseTimeoutInSeconds (builder .httpConnectTimeout );
128
+ httpClientOptions .setConnectionRequestTimeoutInSeconds (builder .httpConnectTimeout );
129
+ httpClientOptions .setMaxTimeMS (builder .maxTimeMS );
118
130
httpClientOptions .setProxy (builder .httpProxy );
119
131
}
120
132
@@ -154,64 +166,6 @@ public enum DataAPIDestination {
154
166
OTHERS
155
167
}
156
168
157
- /**
158
- * Options to set up http Client.
159
- */
160
- @ Getter @ Setter
161
- public static class HttpClientOptions {
162
-
163
- /** Default user agent. */
164
- public static final String DEFAULT_USER_AGENT = "stargate-sdk" ;
165
-
166
- /** Default timeout for initiating connection. */
167
- public static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 20 ;
168
-
169
- /** Default timeout for initiating connection. */
170
- public static final int DEFAULT_REQUEST_TIMEOUT_SECONDS = 20 ;
171
-
172
- /** Default retry count. */
173
- public static final int DEFAULT_RETRY_COUNT = 3 ;
174
-
175
- /** Default retry delay. */
176
- public static final int DEFAULT_RETRY_DELAY_MILLIS = 100 ;
177
-
178
- /** Caller name in User agent. */
179
- String userAgentCallerName = DEFAULT_USER_AGENT ;
180
-
181
- /** Caller version in User agent. */
182
- String userAgentCallerVersion = HttpClientOptions .class .getPackage ().getImplementationVersion () != null ?
183
- HttpClientOptions .class .getPackage ().getImplementationVersion () : "dev" ;
184
-
185
- /** Http Connection timeout. */
186
- long connectionRequestTimeoutInSeconds = DEFAULT_CONNECT_TIMEOUT_SECONDS ;
187
-
188
- /** Http Connection timeout. */
189
- long responseTimeoutInSeconds = DEFAULT_REQUEST_TIMEOUT_SECONDS ;
190
-
191
- /** Enable retry count. */
192
- int retryCount = DEFAULT_RETRY_COUNT ;
193
-
194
- /** How much to wait in between 2 calls. */
195
- int retryDelay = DEFAULT_RETRY_DELAY_MILLIS ;
196
-
197
- /** The http client could work through a proxy. */
198
- HttpProxy proxy ;
199
-
200
- /** Moving to HTTP/2. */
201
- HttpClient .Version httpVersion = HttpClient .Version .HTTP_2 ;
202
-
203
- /** Redirect */
204
- HttpClient .Redirect httpRedirect = HttpClient .Redirect .NORMAL ;
205
-
206
- /**
207
- * Default constructor.
208
- */
209
- public HttpClientOptions () {
210
- // left blanks as default values are set
211
- }
212
-
213
- }
214
-
215
169
/**
216
170
* Subclass to represent an http proxy.
217
171
*/
@@ -254,10 +208,10 @@ public static class DataAPIClientOptionsBuilder {
254
208
private String userAgentCallerVersion = DEFAULT_CALLER_VERSION ;
255
209
256
210
/** Http Connection timeout. */
257
- private long httpRequestTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS ;
211
+ private long maxTimeMS = DEFAULT_CONNECT_TIMEOUT_MILLIS_SECONDS ;
258
212
259
213
/** Http Connection timeout. */
260
- private long httpConnectTimeout = DEFAULT_REQUEST_TIMEOUT_SECONDS ;
214
+ private long httpConnectTimeout = DEFAULT_REQUEST_TIMEOUT_MILLIS_SECONDS ;
261
215
262
216
/** Enable retry count. */
263
217
private int retryCount = DEFAULT_RETRY_COUNT ;
@@ -286,6 +240,12 @@ public static class DataAPIClientOptionsBuilder {
286
240
/** The maximum number of documents that can be inserted in a single operation. */
287
241
private int maxDocumentsInInsert = DEFAULT_MAX_CHUNKSIZE ;
288
242
243
+ /** The embedding service API key can be provided at top level. */
244
+ private String embeddingAPIKey ;
245
+
246
+ /** Observers for the commands. */
247
+ private final Map <String , CommandObserver > observers = new TreeMap <>();
248
+
289
249
/**
290
250
* Default constructor.
291
251
*/
@@ -295,7 +255,7 @@ public DataAPIClientOptionsBuilder() {
295
255
296
256
/**
297
257
* Builder pattern, update caller information.
298
- *
258
+ *o
299
259
* @param callerName
300
260
* caller name in the user agent
301
261
* @param callerVersion
@@ -376,15 +336,28 @@ public DataAPIClientOptionsBuilder withHttpRetryCount(int retryCount) {
376
336
}
377
337
378
338
/**
379
- * Builder pattern, update http connection Timeout
339
+ * Builder pattern, update http request Timeout in millis
380
340
*
381
341
* @param connectTimeout
382
- * http connection timeout
342
+ * http request timeout in millis
383
343
* @return
384
344
* self reference
385
345
*/
386
- public DataAPIClientOptionsBuilder withHttpConnectTimeout (int connectTimeout ) {
387
- this .httpRequestTimeout = connectTimeout ;
346
+ public DataAPIClientOptionsBuilder withMaxTimeMS (long connectTimeout ) {
347
+ this .maxTimeMS = connectTimeout ;
348
+ return this ;
349
+ }
350
+
351
+ /**
352
+ * Builder pattern, update http connection Timeout
353
+ *
354
+ * @param embeddingAPIKey
355
+ * embedding API Key
356
+ * @return
357
+ * self reference
358
+ */
359
+ public DataAPIClientOptionsBuilder withEmbeddingAPIKey (String embeddingAPIKey ) {
360
+ this .embeddingAPIKey = embeddingAPIKey ;
388
361
return this ;
389
362
}
390
363
@@ -396,7 +369,7 @@ public DataAPIClientOptionsBuilder withHttpConnectTimeout(int connectTimeout) {
396
369
* @return
397
370
* self reference
398
371
*/
399
- public DataAPIClientOptionsBuilder withHttpRequestTimeout (int requestTimeout ) {
372
+ public DataAPIClientOptionsBuilder withHttpConnectTimeout (int requestTimeout ) {
400
373
this .httpConnectTimeout = requestTimeout ;
401
374
return this ;
402
375
}
@@ -540,6 +513,42 @@ public DataAPIClientOptionsBuilder withMaxDocumentsInInsert(int maxDocumentsInIn
540
513
return this ;
541
514
}
542
515
516
+ /**
517
+ * Allow to register a listener for the command.
518
+ * @param name
519
+ * name of the observer
520
+ * @param observer
521
+ * observer to register
522
+ * @return
523
+ * instance of the command options
524
+ */
525
+ public DataAPIClientOptionsBuilder withObserver (String name , CommandObserver observer ) {
526
+ observers .put (name , observer );
527
+ return this ;
528
+ }
529
+
530
+ /**
531
+ * Register an observer with its className.
532
+ *
533
+ * @param observer
534
+ * command observer
535
+ * @return
536
+ * instance of the command options
537
+ */
538
+ public DataAPIClientOptionsBuilder withObserver (CommandObserver observer ) {
539
+ return withObserver (observer .getClass ().getSimpleName (), observer );
540
+ }
541
+
542
+ /**
543
+ * Help to enable loggin requests.
544
+ *
545
+ * @return current reference
546
+ *
547
+ */
548
+ public DataAPIClientOptionsBuilder logRequests () {
549
+ return withObserver (new LoggingCommandObserver (DataAPIClient .class ));
550
+ }
551
+
543
552
/**
544
553
* Build the options.
545
554
*
0 commit comments