Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(retrofit2): Improvise OkHttpClientProvider to accept interceptors #1220

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(web): add a method each to ServiceClientFactory and ServiceClien…
…tProvider to support list of interceptors.
  • Loading branch information
kirangodishala committed Jan 9, 2025
commit 8e409c1aa68968c28e6f4bf0801eb26b015a6fd2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.netflix.spinnaker.kork.client.ServiceClientFactory;
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerRetrofitErrorHandler;
import com.netflix.spinnaker.retrofit.Slf4jRetrofitLogger;
import java.util.List;
import okhttp3.Interceptor;
import retrofit.Endpoint;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
Expand Down Expand Up @@ -62,4 +64,16 @@ public <T> T create(Class<T> type, ServiceEndpoint serviceEndpoint, ObjectMapper
.build()
.create(type);
}

@Override
public <T> T create(
Class<T> type,
ServiceEndpoint serviceEndpoint,
ObjectMapper objectMapper,
List<Interceptor> interceptors) {
throw new IllegalArgumentException(
String.format(
"Retrofit1 client doesn't support okhttp3 Interceptors. Failed to build %s ",
type.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.client.ServiceClientFactory;
import java.util.List;
import java.util.Objects;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
Expand All @@ -40,7 +42,16 @@ public Retrofit2ServiceFactory(OkHttpClientProvider clientProvider) {

@Override
public <T> T create(Class<T> type, ServiceEndpoint serviceEndpoint, ObjectMapper objectMapper) {
OkHttpClient okHttpClient = clientProvider.getClient(serviceEndpoint);
return create(type, serviceEndpoint, objectMapper, List.of());
}

@Override
public <T> T create(
Class<T> type,
ServiceEndpoint serviceEndpoint,
ObjectMapper objectMapper,
List<Interceptor> interceptors) {
OkHttpClient okHttpClient = clientProvider.getClient(serviceEndpoint, interceptors);

return new Retrofit.Builder()
.baseUrl(Objects.requireNonNull(HttpUrl.parse(serviceEndpoint.getBaseUrl())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.netflix.spinnaker.kork.client.ServiceClientProvider;
import com.netflix.spinnaker.kork.exceptions.SystemException;
import java.util.List;
import okhttp3.Interceptor;
import org.springframework.stereotype.Component;

/** Provider that returns a suitable service client capable of making http calls. */
Expand Down Expand Up @@ -54,6 +55,16 @@ public <T> T getService(
return serviceClientFactory.create(type, serviceEndpoint, objectMapper);
}

@Override
public <T> T getService(
Class<T> type,
ServiceEndpoint serviceEndpoint,
ObjectMapper objectMapper,
List<Interceptor> interceptors) {
ServiceClientFactory serviceClientFactory = findProvider(type, serviceEndpoint);
return serviceClientFactory.create(type, serviceEndpoint, objectMapper, interceptors);
}

private ServiceClientFactory findProvider(Class<?> type, ServiceEndpoint service) {
return serviceClientFactories.stream()
.filter(provider -> provider.supports(type, service))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.config.ServiceEndpoint;
import java.util.List;
import okhttp3.Interceptor;

/** Factory to build a client for a service. */
public interface ServiceClientFactory {
Expand All @@ -29,11 +31,27 @@ public interface ServiceClientFactory {
* @param type client type
* @param serviceEndpoint endpoint configuration
* @param objectMapper mapper
* @param <T> type of client , usually a interface with all the remote method definitions.
* @return a implementation of the type of client given.
* @param <T> type of client , usually an interface with all the remote method definitions.
* @return an implementation of the type of client given.
*/
public <T> T create(Class<T> type, ServiceEndpoint serviceEndpoint, ObjectMapper objectMapper);

/**
* Builds a concrete client capable of making HTTP calls.
*
* @param type client type
* @param serviceEndpoint endpoint configuration
* @param objectMapper mapper
* @param interceptors list of interceptors
* @param <T> type of client , usually an interface with all the remote method definitions.
* @return an implementation of the type of client given.
*/
public <T> T create(
Class<T> type,
ServiceEndpoint serviceEndpoint,
ObjectMapper objectMapper,
List<Interceptor> interceptors);

/**
* Decide if this factory can support the endpoint provided.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.config.ServiceEndpoint;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import java.util.List;
import okhttp3.Interceptor;

@NonnullByDefault
public interface ServiceClientProvider {
Expand All @@ -29,7 +31,7 @@ public interface ServiceClientProvider {
*
* @param type retrofit interface type
* @param serviceEndpoint endpoint definition
* @param <T> type of client , usually a interface with all the remote method definitions.
* @param <T> type of client , usually an interface with all the remote method definitions.
* @return the retrofit interface implementation
*/
public <T> T getService(Class<T> type, ServiceEndpoint serviceEndpoint);
Expand All @@ -40,9 +42,25 @@ public interface ServiceClientProvider {
* @param type retrofit interface type
* @param serviceEndpoint endpoint definition
* @param objectMapper object mapper for conversion
* @param <T> type of client , usually a interface with all the remote method definitions.
* @param <T> type of client , usually an interface with all the remote method definitions.
* @return the retrofit interface implementation
*/
public <T> T getService(
Class<T> type, ServiceEndpoint serviceEndpoint, ObjectMapper objectMapper);

/**
* Returns the concrete retrofit service client
*
* @param type retrofit interface type
* @param serviceEndpoint endpoint definition
* @param objectMapper object mapper for conversion
* @param interceptors list of interceptors
* @param <T> type of client , usually an interface with all the remote method definitions.
* @return the retrofit interface implementation
*/
public <T> T getService(
Class<T> type,
ServiceEndpoint serviceEndpoint,
ObjectMapper objectMapper,
List<Interceptor> interceptors);
}
Loading