Skip to content

Commit

Permalink
Merge pull request #14 from taboola/custom-interceptor
Browse files Browse the repository at this point in the history
Allow customized interceptors
  • Loading branch information
vladimanaev authored Sep 13, 2024
2 parents e808f45 + 755e6e6 commit 80af417
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.taboola</groupId>
<artifactId>api-java-client-core</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>API Java client core</description>
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/taboola/rest/api/RestAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import com.taboola.rest.api.internal.serialization.SerializationMapperCreator;
import com.taboola.rest.api.model.CommunicationInterceptor;
import com.taboola.rest.api.model.HttpLoggingLevel;
import com.taboola.rest.api.model.MultiRequestHeadersSupplier;
import com.taboola.rest.api.model.NoOpCommunicationInterceptor;
import com.taboola.rest.api.model.RequestHeader;
import com.taboola.rest.api.model.RequestHeadersSupplier;
import com.taboola.rest.api.model.StringResponseFactory;

/**
Expand Down Expand Up @@ -51,7 +53,7 @@ public static RestAPIClientBuilder builder() {
}

public static class RestAPIClientBuilder {
private static final String VERSION = "1.0.6";
private static final String VERSION = "1.0.7";
private static final Integer DEFAULT_MAX_IDLE_CONNECTIONS = 5;
private static final Long DEFAULT_KEEP_ALIVE_DURATION_MILLIS = 300_000L;
private static final SerializationConfig DEFAULT_SERIALIZATION_CONFIG = new SerializationConfig();
Expand All @@ -70,6 +72,7 @@ public static class RestAPIClientBuilder {
private Boolean debug;
private SerializationConfig serializationConfig;
private Collection<RequestHeader> headers;
private RequestHeadersSupplier headersSupplier;
private String userAgentPrefix;
private String userAgentSuffix;
private String restAPIVersion;
Expand Down Expand Up @@ -139,6 +142,11 @@ public RestAPIClientBuilder setHeaders(Collection<RequestHeader> headers) {
return this;
}

public RestAPIClientBuilder setHeadersSupplier(RequestHeadersSupplier headersSupplier) {
this.headersSupplier = headersSupplier;
return this;
}

public RestAPIClientBuilder setAPIVersion(String restAPIVersion) {
this.restAPIVersion = restAPIVersion;
return this;
Expand Down Expand Up @@ -168,8 +176,10 @@ public RestAPIClient build() {
organizeState();
String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix);
Collection<RequestHeader> headers = getAllHeaders(this.headers, finalUserAgent);
RequestHeadersSupplier multiRequestHeadersSupplier = new MultiRequestHeadersSupplier(() -> headers, headersSupplier);

CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections,
keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor);
keepAliveDurationMillis, multiRequestHeadersSupplier, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor);
return new RestAPIClient(new CommunicationFactory(config));
}

Expand Down Expand Up @@ -241,7 +251,7 @@ private void organizeState() {
loggingLevel = HttpLoggingLevel.BASIC;
}

if(communicationInterceptor == null) {
if (communicationInterceptor == null) {
communicationInterceptor = DEFAULT_COMMUNICATION_INTERCEPTOR;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private Retrofit.Builder createRetrofitBuilder(CommunicationConfig config) {

private OkHttpClient createOkHttpClient(CommunicationConfig config) {
return new OkHttpClient.Builder()
.addInterceptor(new HeadersInterceptor(config.getHeaders()))
.addInterceptor(new HeadersInterceptor(config.getRequestHeadersSupplier()))
.addInterceptor(createLoggingInterceptor(config))
.addInterceptor(new ImmutableRequestResponseInterceptor(config.getCommunicationInterceptor()))
.readTimeout(config.getReadTimeoutMillis(), TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.taboola.rest.api.internal.StringResponseFactories;
import com.taboola.rest.api.model.CommunicationInterceptor;
import com.taboola.rest.api.model.HttpLoggingLevel;
import com.taboola.rest.api.model.RequestHeader;
import com.taboola.rest.api.model.RequestHeadersSupplier;

/**
* Created by vladi
Expand All @@ -25,7 +25,7 @@ public class CommunicationConfig {
private final int maxIdleConnections;
private final long keepAliveDurationMillis;
private final boolean debug;
private final Collection<RequestHeader> headers;
private final RequestHeadersSupplier requestHeadersSupplier;
private final ExceptionFactory exceptionFactory;
private final ObjectMapper objectMapper;
private final StringResponseFactories stringResponseFactories;
Expand All @@ -34,7 +34,7 @@ public class CommunicationConfig {

public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis,
Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis,
Collection<RequestHeader> headers, boolean debug, ExceptionFactory exceptionFactory,
RequestHeadersSupplier requestHeadersSupplier, boolean debug, ExceptionFactory exceptionFactory,
ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel,
CommunicationInterceptor communicationInterceptor) {
this.baseUrl = baseUrl;
Expand All @@ -43,7 +43,7 @@ public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long re
this.writeTimeoutMillis = writeTimeoutMillis;
this.maxIdleConnections = maxIdleConnections;
this.keepAliveDurationMillis = keepAliveDurationMillis;
this.headers = headers;
this.requestHeadersSupplier = requestHeadersSupplier;
this.debug = debug;
this.exceptionFactory = exceptionFactory;
this.objectMapper = objectMapper;
Expand Down Expand Up @@ -76,8 +76,8 @@ public long getKeepAliveDurationMillis() {
return keepAliveDurationMillis;
}

public Collection<RequestHeader> getHeaders() {
return headers;
public RequestHeadersSupplier getRequestHeadersSupplier() {
return requestHeadersSupplier;
}

public boolean isDebug() {
Expand Down Expand Up @@ -114,7 +114,7 @@ public String toString() {
", maxIdleConnections=" + maxIdleConnections +
", keepAliveDurationMillis=" + keepAliveDurationMillis +
", debug=" + debug +
", headers=" + headers +
", requestHeadersSupplier=" + requestHeadersSupplier +
", exceptionFactory=" + exceptionFactory +
", objectMapper=" + objectMapper +
", stringResponseFactories=" + stringResponseFactories +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.taboola.rest.api.internal.interceptors;

import java.io.IOException;
import java.util.Collection;

import com.taboola.rest.api.model.RequestHeader;
import com.taboola.rest.api.model.RequestHeadersSupplier;

import okhttp3.Interceptor;
import okhttp3.Request;
Expand All @@ -13,22 +13,23 @@
* Created on 10/21/20.
*/
public class HeadersInterceptor implements Interceptor {
private final Collection<RequestHeader> headers;
private final RequestHeadersSupplier requestHeadersSupplier;

public HeadersInterceptor(Collection<RequestHeader> headers) {
this.headers = headers;
public HeadersInterceptor(RequestHeadersSupplier requestHeadersSupplier) {
this.requestHeadersSupplier = requestHeadersSupplier;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
headers.stream()
requestHeadersSupplier.get()
.stream()
.filter(this::validateHeader)
.forEach(header -> builder.header(header.getName(), header.getValue()));
return chain.proceed(builder.build());
}

private boolean validateHeader(RequestHeader header){
private boolean validateHeader(RequestHeader header) {
return header.getName() != null && header.getValue() != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.taboola.rest.api.model;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MultiRequestHeadersSupplier implements RequestHeadersSupplier {
private final Collection<RequestHeadersSupplier> suppliers;

public MultiRequestHeadersSupplier(RequestHeadersSupplier... suppliers) {
this.suppliers = Stream.of(suppliers == null ? new RequestHeadersSupplier[0] : suppliers)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

@Override
public Collection<RequestHeader> get() {
return suppliers.stream()
.map(RequestHeadersSupplier::get)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.taboola.rest.api.model;

import java.util.Collection;
import java.util.function.Supplier;

public interface RequestHeadersSupplier extends Supplier<Collection<RequestHeader>> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface TestEndpoint {
public void beforeTest() {
CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost",
1L, 1L, 1L, 1, 60L,
Collections.singleton(new UserAgentHeader("Dummy-Agent")),true, new DefaultExceptionFactory(),
() -> Collections.singleton(new UserAgentHeader("Dummy-Agent")), true, new DefaultExceptionFactory(),
new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor());
testInstance = new CommunicationFactory(communicationConfig);
}
Expand Down

0 comments on commit 80af417

Please sign in to comment.