Skip to content

Commit 81a2f04

Browse files
Add with interceptors function to Builder (#86)
* Add with interceptors function to Builder * lint
1 parent 62767a0 commit 81a2f04

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main/java/ai/nightfall/scan/NightfallClient.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import okhttp3.Call;
2020
import okhttp3.ConnectionPool;
2121
import okhttp3.Headers;
22+
import okhttp3.Interceptor;
2223
import okhttp3.MediaType;
2324
import okhttp3.OkHttpClient;
2425
import okhttp3.Request;
@@ -30,7 +31,9 @@
3031
import java.io.InputStream;
3132
import java.time.Duration;
3233
import java.time.Instant;
34+
import java.util.ArrayList;
3335
import java.util.Arrays;
36+
import java.util.List;
3437
import java.util.UUID;
3538
import java.util.concurrent.ExecutorService;
3639
import java.util.concurrent.Executors;
@@ -456,6 +459,7 @@ public static class Builder {
456459
private Duration writeTimeout = Duration.ofSeconds(60);
457460
private int maxIdleConnections = 100;
458461
private Duration keepAliveDuration = Duration.ofSeconds(30);
462+
private List<Interceptor> interceptors = new ArrayList<Interceptor>();
459463

460464
/**
461465
* Builds and returns the client with all default values. The API key is loaded from the environment variable
@@ -586,6 +590,17 @@ public Builder withKeepAliveDuration(Duration keepAliveDuration) {
586590
return this;
587591
}
588592

593+
/**
594+
* Adds an interceptor to the HTTP client used to preform requests.
595+
*
596+
* @param interceptor a OkHttpClient interceptor
597+
* @return the builder
598+
*/
599+
public Builder withInterceptor(Interceptor interceptor) {
600+
this.interceptors.add(interceptor);
601+
return this;
602+
}
603+
589604
/**
590605
* Builds the client using the configured values, falling back on defaults if any values
591606
* were not explicitly set.
@@ -600,12 +615,16 @@ public NightfallClient build() {
600615

601616
ConnectionPool cxnPool = new ConnectionPool(this.maxIdleConnections,
602617
this.keepAliveDuration.toMillis(), TimeUnit.MILLISECONDS);
603-
OkHttpClient httpClient = new OkHttpClient.Builder()
618+
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
604619
.connectTimeout(this.connectionTimeout)
605620
.readTimeout(this.readTimeout)
606621
.writeTimeout(this.writeTimeout)
607-
.connectionPool(cxnPool)
608-
.build();
622+
.connectionPool(cxnPool);
623+
for (Interceptor interceptor : this.interceptors) {
624+
httpClientBuilder = httpClientBuilder.addInterceptor(interceptor);
625+
}
626+
627+
OkHttpClient httpClient = httpClientBuilder.build();
609628
return new NightfallClient(API_HOST, this.apiKey, this.fileUploadConcurrency, httpClient);
610629
}
611630

0 commit comments

Comments
 (0)