Skip to content

Commit

Permalink
chore: Store interceptors as fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Mar 22, 2022
1 parent 0739bc4 commit 95771c7
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import java.net.http.WebSocket;
import java.time.Clock;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -43,11 +41,9 @@
import java.util.function.Function;

public class ExtendedHttpClient extends HttpClient {
private final Cache cache;
private final boolean isNullCache;
private final CompressionInterceptor compressionInterceptor;
private final CachingInterceptor cachingInterceptor;

private final boolean transparentEncoding;
private final Clock clock;
private final HttpClient delegate;

ExtendedHttpClient(HttpClient delegate, Cache cache, Clock clock) {
Expand All @@ -56,10 +52,8 @@ public class ExtendedHttpClient extends HttpClient {

ExtendedHttpClient(HttpClient delegate, Cache cache, boolean transparentEncoding, Clock clock) {
this.delegate = delegate;
this.cache = cache;
this.isNullCache = cache instanceof NullCache;
this.transparentEncoding = transparentEncoding;
this.clock = clock;
this.cachingInterceptor = cache instanceof NullCache ? null : new CachingInterceptor(cache, clock);
this.compressionInterceptor = transparentEncoding ? new CompressionInterceptor() : null;
}

/**
Expand Down Expand Up @@ -177,20 +171,9 @@ private <T> CompletableFuture<HttpResponse<T>> send0(HttpRequest request, BodyHa
}

private <T> Chain<T> buildAndExecute(RequestContext ctx) {
List<Interceptor> interceptors = new ArrayList<>(2);
if (transparentEncoding) {
interceptors.add(new CompressionInterceptor());
}
if (!isNullCache) {
interceptors.add(new CachingInterceptor(cache, clock));
}

Chain<T> chain = Chain.of(ctx);
if (!interceptors.isEmpty()) {
for (var interceptor : interceptors) {
chain = interceptor.intercept(chain);
}
}
chain = compressionInterceptor != null ? compressionInterceptor.intercept(chain) : chain;
chain = cachingInterceptor != null ? cachingInterceptor.intercept(chain) : chain;

return chain;
}
Expand Down

0 comments on commit 95771c7

Please sign in to comment.