From 8e409c1aa68968c28e6f4bf0801eb26b015a6fd2 Mon Sep 17 00:00:00 2001 From: kirangodishala Date: Wed, 8 Jan 2025 20:21:15 +0530 Subject: [PATCH] feat(web): add a method each to ServiceClientFactory and ServiceClientProvider to support list of interceptors. --- .../kork/retrofit/RetrofitServiceFactory.java | 14 ++++++++++++ .../retrofit/Retrofit2ServiceFactory.java | 13 ++++++++++- .../config/DefaultServiceClientProvider.java | 11 ++++++++++ .../kork/client/ServiceClientFactory.java | 22 +++++++++++++++++-- .../kork/client/ServiceClientProvider.java | 22 +++++++++++++++++-- 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/RetrofitServiceFactory.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/RetrofitServiceFactory.java index 53b32c5ba..76ce6bf74 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/RetrofitServiceFactory.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/RetrofitServiceFactory.java @@ -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; @@ -62,4 +64,16 @@ public T create(Class type, ServiceEndpoint serviceEndpoint, ObjectMapper .build() .create(type); } + + @Override + public T create( + Class type, + ServiceEndpoint serviceEndpoint, + ObjectMapper objectMapper, + List interceptors) { + throw new IllegalArgumentException( + String.format( + "Retrofit1 client doesn't support okhttp3 Interceptors. Failed to build %s ", + type.getName())); + } } diff --git a/kork-retrofit2/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2ServiceFactory.java b/kork-retrofit2/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2ServiceFactory.java index 33bbb12b5..bc6a93f81 100644 --- a/kork-retrofit2/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2ServiceFactory.java +++ b/kork-retrofit2/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2ServiceFactory.java @@ -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; @@ -40,7 +42,16 @@ public Retrofit2ServiceFactory(OkHttpClientProvider clientProvider) { @Override public T create(Class type, ServiceEndpoint serviceEndpoint, ObjectMapper objectMapper) { - OkHttpClient okHttpClient = clientProvider.getClient(serviceEndpoint); + return create(type, serviceEndpoint, objectMapper, List.of()); + } + + @Override + public T create( + Class type, + ServiceEndpoint serviceEndpoint, + ObjectMapper objectMapper, + List interceptors) { + OkHttpClient okHttpClient = clientProvider.getClient(serviceEndpoint, interceptors); return new Retrofit.Builder() .baseUrl(Objects.requireNonNull(HttpUrl.parse(serviceEndpoint.getBaseUrl()))) diff --git a/kork-web/src/main/java/com/netflix/spinnaker/config/DefaultServiceClientProvider.java b/kork-web/src/main/java/com/netflix/spinnaker/config/DefaultServiceClientProvider.java index 00d2e9ff3..984272d38 100644 --- a/kork-web/src/main/java/com/netflix/spinnaker/config/DefaultServiceClientProvider.java +++ b/kork-web/src/main/java/com/netflix/spinnaker/config/DefaultServiceClientProvider.java @@ -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. */ @@ -54,6 +55,16 @@ public T getService( return serviceClientFactory.create(type, serviceEndpoint, objectMapper); } + @Override + public T getService( + Class type, + ServiceEndpoint serviceEndpoint, + ObjectMapper objectMapper, + List 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)) diff --git a/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientFactory.java b/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientFactory.java index b8c071419..f969b4966 100644 --- a/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientFactory.java +++ b/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientFactory.java @@ -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 { @@ -29,11 +31,27 @@ public interface ServiceClientFactory { * @param type client type * @param serviceEndpoint endpoint configuration * @param objectMapper mapper - * @param type of client , usually a interface with all the remote method definitions. - * @return a implementation of the type of client given. + * @param type of client , usually an interface with all the remote method definitions. + * @return an implementation of the type of client given. */ public T create(Class 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 type of client , usually an interface with all the remote method definitions. + * @return an implementation of the type of client given. + */ + public T create( + Class type, + ServiceEndpoint serviceEndpoint, + ObjectMapper objectMapper, + List interceptors); + /** * Decide if this factory can support the endpoint provided. * diff --git a/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientProvider.java b/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientProvider.java index 030a9e351..36f929fef 100644 --- a/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientProvider.java +++ b/kork-web/src/main/java/com/netflix/spinnaker/kork/client/ServiceClientProvider.java @@ -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 { @@ -29,7 +31,7 @@ public interface ServiceClientProvider { * * @param type retrofit interface type * @param serviceEndpoint endpoint definition - * @param type of client , usually a interface with all the remote method definitions. + * @param type of client , usually an interface with all the remote method definitions. * @return the retrofit interface implementation */ public T getService(Class type, ServiceEndpoint serviceEndpoint); @@ -40,9 +42,25 @@ public interface ServiceClientProvider { * @param type retrofit interface type * @param serviceEndpoint endpoint definition * @param objectMapper object mapper for conversion - * @param type of client , usually a interface with all the remote method definitions. + * @param type of client , usually an interface with all the remote method definitions. * @return the retrofit interface implementation */ public T getService( Class 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 type of client , usually an interface with all the remote method definitions. + * @return the retrofit interface implementation + */ + public T getService( + Class type, + ServiceEndpoint serviceEndpoint, + ObjectMapper objectMapper, + List interceptors); }