Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## Rest API SDK Core

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.taboola/api-java-client-core/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/com.taboola/api-java-client-core)
[![Build Status](https://travis-ci.com/taboola/api-java-client-core.svg?branch=master)](https://travis-ci.com/taboola/api-java-client-core)

### Table of Contents
1. [Getting Started](#1-getting-started)
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/taboola/rest/api/RestAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.taboola.rest.api.internal.config.SerializationConfig;
import com.taboola.rest.api.internal.config.UserAgentHeader;
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.NoOpCommunicationInterceptor;
import com.taboola.rest.api.model.RequestHeader;
import com.taboola.rest.api.model.StringResponseFactory;

Expand Down Expand Up @@ -57,6 +59,7 @@ public static class RestAPIClientBuilder {
private static final String DEFAULT_USER_AGENT_SUFFIX = "UNDEFINED";
private static final String DEFAULT_USER_AGENT_PREFIX = "UNDEFINED";
private static final ExceptionFactory DEFAULT_EXCEPTION_FACTORY = new DefaultExceptionFactory();
private static final CommunicationInterceptor DEFAULT_COMMUNICATION_INTERCEPTOR = new NoOpCommunicationInterceptor();

private String baseUrl;
private Long writeTimeoutMillis;
Expand All @@ -74,6 +77,7 @@ public static class RestAPIClientBuilder {
private ObjectMapper objectMapper;
private final StringResponseFactories stringResponseFactories = new StringResponseFactories();
private HttpLoggingLevel loggingLevel;
private CommunicationInterceptor communicationInterceptor;

public RestAPIClientBuilder setLoggingLevel(HttpLoggingLevel loggingLevel) {
this.loggingLevel = loggingLevel;
Expand Down Expand Up @@ -150,6 +154,11 @@ public RestAPIClientBuilder setObjectMapper(ObjectMapper objectMapper) {
return this;
}

public RestAPIClientBuilder setCommunicationInterceptor(CommunicationInterceptor communicationInterceptor) {
this.communicationInterceptor = communicationInterceptor;
return this;
}

public RestAPIClientBuilder addStringBodyResponseFactory(Class<?> clazz, StringResponseFactory stringResponseFactory) {
stringResponseFactories.addFactory(clazz, stringResponseFactory);
return this;
Expand All @@ -160,7 +169,7 @@ public RestAPIClient build() {
String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix);
Collection<RequestHeader> headers = getAllHeaders(this.headers, finalUserAgent);
CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections,
keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel);
keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor);
return new RestAPIClient(new CommunicationFactory(config));
}

Expand Down Expand Up @@ -231,6 +240,10 @@ private void organizeState() {
if (loggingLevel == null) {
loggingLevel = HttpLoggingLevel.BASIC;
}

if(communicationInterceptor == null) {
communicationInterceptor = DEFAULT_COMMUNICATION_INTERCEPTOR;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.taboola.rest.api.internal.config.CommunicationConfig;
import com.taboola.rest.api.internal.interceptors.CommunicationInterceptor;
import com.taboola.rest.api.internal.interceptors.HeadersInterceptor;
import com.taboola.rest.api.internal.interceptors.ImmutableRequestResponseInterceptor;
import com.taboola.rest.api.model.HttpLoggingLevel;

import okhttp3.ConnectionPool;
Expand Down Expand Up @@ -58,6 +59,7 @@ private OkHttpClient createOkHttpClient(CommunicationConfig config) {
return new OkHttpClient.Builder()
.addInterceptor(new HeadersInterceptor(config.getHeaders()))
.addInterceptor(createLoggingInterceptor(config))
.addInterceptor(new ImmutableRequestResponseInterceptor(config.getCommunicationInterceptor()))
.readTimeout(config.getReadTimeoutMillis(), TimeUnit.MILLISECONDS)
.writeTimeout(config.getWriteTimeoutMillis(), TimeUnit.MILLISECONDS)
.connectTimeout(config.getConnectionTimeoutMillis(), TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taboola.rest.api.exceptions.factories.ExceptionFactory;
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;

Expand All @@ -29,11 +30,13 @@ public class CommunicationConfig {
private final ObjectMapper objectMapper;
private final StringResponseFactories stringResponseFactories;
private final HttpLoggingLevel loggingLevel;
private final CommunicationInterceptor communicationInterceptor;

public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis,
Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis,
Collection<RequestHeader> headers, boolean debug, ExceptionFactory exceptionFactory,
ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel) {
ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel,
CommunicationInterceptor communicationInterceptor) {
this.baseUrl = baseUrl;
this.connectionTimeoutMillis = connectionTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
Expand All @@ -46,6 +49,7 @@ public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long re
this.objectMapper = objectMapper;
this.stringResponseFactories = stringResponseFactories;
this.loggingLevel = loggingLevel;
this.communicationInterceptor = communicationInterceptor;
}

public String getBaseUrl() {
Expand Down Expand Up @@ -96,6 +100,10 @@ public HttpLoggingLevel getLoggingLevel() {
return loggingLevel;
}

public CommunicationInterceptor getCommunicationInterceptor() {
return communicationInterceptor;
}

@Override
public String toString() {
return "CommunicationConfig{" +
Expand All @@ -111,6 +119,7 @@ public String toString() {
", objectMapper=" + objectMapper +
", stringResponseFactories=" + stringResponseFactories +
", loggingLevel=" + loggingLevel +
", communicationInterceptor=" + communicationInterceptor +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public class SerializationConfig {
private Map<Class<?>, Class<?>> mixins;
private boolean shouldIgnoreAnySetterAnnotation;
private boolean shouldDisableReadUnknownEnumValuesAsDefaultValue;
private boolean shouldUseSnakeCase;

public SerializationConfig() {
mixins = new HashMap<>();
shouldIgnoreAnySetterAnnotation = false;
shouldDisableReadUnknownEnumValuesAsDefaultValue = false;
shouldUseSnakeCase = false;
}

public SerializationConfig setMixins(Map<Class<?>, Class<?>> mixins) {
Expand All @@ -29,6 +31,11 @@ public SerializationConfig setShouldDisableReadUnknownEnumValuesAsDefaultValue()
return this;
}

public SerializationConfig setShouldUseSnakeCase() {
this.shouldUseSnakeCase = true;
return this;
}

public Map<Class<?>, Class<?>> getMixins() {
return mixins;
}
Expand All @@ -39,12 +46,16 @@ public boolean shouldIgnoreAnySetterAnnotation() {
public boolean shouldDisableReadUnknownEnumValuesAsDefaultValue() {
return shouldDisableReadUnknownEnumValuesAsDefaultValue;
}
public boolean shouldUseSnakeCase() {
return shouldUseSnakeCase;
}

@Override
public String toString() {
return "SerializationConfig{" +
"mixins=" + mixins +
", shouldIgnoreAnySetterAnnotation=" + shouldIgnoreAnySetterAnnotation +
", shouldUseSnakeCase=" + shouldUseSnakeCase +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.taboola.rest.api.internal.interceptors;

import okhttp3.Interceptor;
import okhttp3.Response;

import java.io.IOException;
import java.util.Objects;

import com.taboola.rest.api.model.CommunicationInterceptor;

/**
* Created by vladi.m
* Date 20/11/2023
* Time 15:20
* Copyright Taboola
*/
public class ImmutableRequestResponseInterceptor implements Interceptor {

private final CommunicationInterceptor interceptor;

public ImmutableRequestResponseInterceptor(CommunicationInterceptor interceptor) {
Objects.requireNonNull(interceptor);
this.interceptor = interceptor;
}

@Override
public Response intercept(Chain chain) throws IOException {
interceptor.before(chain.request());
Response response = null;
try {
response = chain.proceed(chain.request());
return response;
} finally {
interceptor.after(chain.request(), response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
public class SerializationMapperCreator {
public static ObjectMapper createObjectMapper(SerializationConfig serializationConfig) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
if(serializationConfig.shouldUseSnakeCase()) {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
} else {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
}
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.taboola.rest.api.model;

import okhttp3.Request;
import okhttp3.Response;

/**
* Created by vladi.m
* Date 02/02/2024
* Time 10:01
* Copyright Taboola
*/
public interface CommunicationInterceptor {

void before(final Request request);

void after(final Request request, final Response response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.taboola.rest.api.model;

import okhttp3.Request;
import okhttp3.Response;

/**
* Created by vladi.m
* Date 02/02/2024
* Time 10:35
* Copyright Taboola
*/
public class NoOpCommunicationInterceptor implements CommunicationInterceptor{
@Override
public void before(Request request) {}

@Override
public void after(Request request, Response response) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.taboola.rest.api.internal.config.CommunicationConfig;
import com.taboola.rest.api.internal.config.UserAgentHeader;
import com.taboola.rest.api.model.HttpLoggingLevel;
import com.taboola.rest.api.model.NoOpCommunicationInterceptor;

import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
Expand All @@ -39,7 +39,7 @@ public void beforeTest() {
CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost",
1L, 1L, 1L, 1, 60L,
Collections.singleton(new UserAgentHeader("Dummy-Agent")),true, new DefaultExceptionFactory(),
new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC);
new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor());
testInstance = new CommunicationFactory(communicationConfig);
}

Expand Down