1919import okhttp3 .Call ;
2020import okhttp3 .ConnectionPool ;
2121import okhttp3 .Headers ;
22+ import okhttp3 .Interceptor ;
2223import okhttp3 .MediaType ;
2324import okhttp3 .OkHttpClient ;
2425import okhttp3 .Request ;
3031import java .io .InputStream ;
3132import java .time .Duration ;
3233import java .time .Instant ;
34+ import java .util .ArrayList ;
3335import java .util .Arrays ;
36+ import java .util .List ;
3437import java .util .UUID ;
3538import java .util .concurrent .ExecutorService ;
3639import 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