From 99c10123f6891fe9425051677b3ee6cf3522432e Mon Sep 17 00:00:00 2001 From: Raghu Sammeta Date: Fri, 4 Oct 2024 10:22:57 -0400 Subject: [PATCH 01/15] Fix default handler overide , when user passed in interceptor should overide any default implementations --- .../kiota/http/KiotaClientFactory.java | 65 ++++++++++++++++++- .../kiota/http/KiotaClientFactoryTest.java | 64 ++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 5f86565e4..d917627f8 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -6,11 +6,14 @@ import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; import com.microsoft.kiota.http.middleware.RedirectHandler; import com.microsoft.kiota.http.middleware.RetryHandler; +import com.microsoft.kiota.http.middleware.UrlReplaceHandler; import com.microsoft.kiota.http.middleware.UserAgentHandler; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; +import java.util.HashSet; +import java.util.Set; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -46,7 +49,7 @@ private KiotaClientFactory() {} 100)); // TODO configure the default client options. final Interceptor[] interceptorsOrDefault = - interceptors != null ? interceptors : createDefaultInterceptors(); + interceptors != null ? createOveridableDefaultInterceptors( interceptors ): createDefaultInterceptors(); for (final Interceptor interceptor : interceptorsOrDefault) { builder.addInterceptor(interceptor); } @@ -84,6 +87,7 @@ private KiotaClientFactory() {} */ @Nonnull public static Interceptor[] createDefaultInterceptors() { return new Interceptor[] { + new UrlReplaceHandler(), new RedirectHandler(), new RetryHandler(), new ParametersNameDecodingHandler(), @@ -92,6 +96,30 @@ private KiotaClientFactory() {} }; } + /** + * Creates the default interceptors for the client. + * @return an array of interceptors. + */ + @Nonnull public static Interceptor[] createOveridableDefaultInterceptors( @Nonnull Interceptor[] interceptors) { + + List handlers = new ArrayList<>(Arrays.asList(interceptors)); + Set defaultHandlerTypes = getDefaultHandler(); + + for (Interceptor interceptor : interceptors) { + defaultHandlerTypes.remove(interceptor.getClass().toString()); + } + // Add any remaining default interceptors + for (String handlerType : defaultHandlerTypes) { + + handlers.add(createHandler(handlerType)); + } + + return handlers.toArray(new Interceptor[0]); + } + + + + /** * Creates the default interceptors for the client. * @return an array of interceptors. @@ -99,4 +127,39 @@ private KiotaClientFactory() {} @Nonnull public static List createDefaultInterceptorsAsList() { return new ArrayList<>(Arrays.asList(createDefaultInterceptors())); } + + + /** + * Gets the default handler types. + * + * @return A list of all the default handlers classnames + * + */ + private static Set getDefaultHandler() { + return new HashSet<>(Arrays.asList( + UrlReplaceHandler.class.toString(), + RedirectHandler.class.toString(), + RetryHandler.class.toString(), + ParametersNameDecodingHandler.class.toString(), + UserAgentHandler.class.toString(), + HeadersInspectionHandler.class.toString())); + } + private static Interceptor createHandler(String handlerType) { + switch (handlerType) { + case "class com.microsoft.kiota.http.middleware.RetryHandler": + return new RetryHandler(); + case "class com.microsoft.kiota.http.middleware.RedirectHandler": + return new RedirectHandler(); + case "class com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler": + return new ParametersNameDecodingHandler(); + case "class com.microsoft.kiota.http.middleware.UserAgentHandler": + return new UserAgentHandler(); + case "class com.microsoft.kiota.http.middleware.HeadersInspectionHandler": + return new HeadersInspectionHandler(); + case "class com.microsoft.kiota.http.middleware.UrlReplaceHandler": + return new UrlReplaceHandler(); + default: + return null; // Handle unknown types as necessary + } + } } diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java new file mode 100644 index 000000000..b609495d7 --- /dev/null +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java @@ -0,0 +1,64 @@ +package com.microsoft.kiota.http; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +import com.microsoft.kiota.http.middleware.ChaosHandler; +import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; +import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; +import com.microsoft.kiota.http.middleware.RedirectHandler; +import com.microsoft.kiota.http.middleware.RetryHandler; +import com.microsoft.kiota.http.middleware.UrlReplaceHandler; +import com.microsoft.kiota.http.middleware.UserAgentHandler; +import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; +import java.io.IOException; +import java.util.List; +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.junit.jupiter.api.Test; + +public class KiotaClientFactoryTest { + + @Test + void testCreatesDefaultInterceptors() throws IOException { + OkHttpClient client = KiotaClientFactory.create().build(); + assertNotNull(client.interceptors()); + assertEquals(6, client.interceptors().size()); + } + + @Test + void testOverideDefaultInterceptors() throws IOException { + OkHttpClient client = KiotaClientFactory.create( + new Interceptor[]{getDisabledRetryHandler(), new ChaosHandler()}).build(); + List interceptors = client.interceptors(); + assertNotNull(interceptors); + assertEquals(7, interceptors.size()); + for (Interceptor interceptor : interceptors) { + if (interceptor instanceof RetryHandler) { + RetryHandlerOption handlerOption = ((RetryHandler) interceptor).getRetryOptions(); + assertEquals(0, handlerOption.delay()); + assertEquals(0, handlerOption.maxRetries()); + } + + assertTrue(interceptor instanceof UrlReplaceHandler || interceptor instanceof RedirectHandler || + interceptor instanceof RetryHandler || interceptor instanceof ParametersNameDecodingHandler + || interceptor instanceof UserAgentHandler || interceptor instanceof HeadersInspectionHandler + || interceptor instanceof ChaosHandler, + "Array should contain instances of UrlReplaceHandler,RedirectHandler,RetryHandler,ParametersNameDecodingHandler,UserAgentHandler, HeadersInspectionHandler, and ChaosHandler"); + + + } + + + } + + private static RetryHandler getDisabledRetryHandler() { + RetryHandlerOption retryHandlerOption = new RetryHandlerOption( + (delay, executionCount, request, response) -> false, 0, 0); + RetryHandler retryHandler = new RetryHandler(retryHandlerOption); + return retryHandler; + } + +} \ No newline at end of file From f3b7bc1868d448f9074525b43e9a97b768ccef46 Mon Sep 17 00:00:00 2001 From: Raghu Sammeta Date: Tue, 8 Oct 2024 10:56:48 -0400 Subject: [PATCH 02/15] fix code based on comments --- .../kiota/http/KiotaClientFactory.java | 92 +++++++++---------- .../kiota/http/KiotaClientFactoryTest.java | 44 ++++++++- 2 files changed, 85 insertions(+), 51 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index d917627f8..b22a5e955 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -1,5 +1,6 @@ package com.microsoft.kiota.http; +import com.microsoft.kiota.RequestOption; import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; import com.microsoft.kiota.http.middleware.AuthorizationHandler; import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; @@ -9,6 +10,12 @@ import com.microsoft.kiota.http.middleware.UrlReplaceHandler; import com.microsoft.kiota.http.middleware.UserAgentHandler; +import com.microsoft.kiota.http.middleware.options.HeadersInspectionOption; +import com.microsoft.kiota.http.middleware.options.ParametersNameDecodingOption; +import com.microsoft.kiota.http.middleware.options.RedirectHandlerOption; +import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; +import com.microsoft.kiota.http.middleware.options.UrlReplaceHandlerOption; +import com.microsoft.kiota.http.middleware.options.UserAgentHandlerOption; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; @@ -48,8 +55,7 @@ private KiotaClientFactory() {} Duration.ofSeconds( 100)); // TODO configure the default client options. - final Interceptor[] interceptorsOrDefault = - interceptors != null ? createOveridableDefaultInterceptors( interceptors ): createDefaultInterceptors(); + final Interceptor[] interceptorsOrDefault = createDefaultInterceptors(interceptors); for (final Interceptor interceptor : interceptorsOrDefault) { builder.addInterceptor(interceptor); } @@ -100,26 +106,50 @@ private KiotaClientFactory() {} * Creates the default interceptors for the client. * @return an array of interceptors. */ - @Nonnull public static Interceptor[] createOveridableDefaultInterceptors( @Nonnull Interceptor[] interceptors) { + @Nonnull public static Interceptor[] createDefaultInterceptors( @Nullable Interceptor[] interceptors) { + if(interceptors == null || interceptors.length == 0){ + return createDefaultInterceptors(); + } + return interceptors; - List handlers = new ArrayList<>(Arrays.asList(interceptors)); - Set defaultHandlerTypes = getDefaultHandler(); + } - for (Interceptor interceptor : interceptors) { - defaultHandlerTypes.remove(interceptor.getClass().toString()); + @Nonnull public static Interceptor[] createDefaultInterceptors(List requestOptions) { + + UrlReplaceHandlerOption uriReplacementOption = null; + UserAgentHandlerOption userAgentHandlerOption = null; + RetryHandlerOption retryHandlerOption = null; + RedirectHandlerOption redirectHandlerOption = null; + ParametersNameDecodingOption parametersNameDecodingOption = null; + HeadersInspectionOption headersInspectionHandlerOption = null; + + for (RequestOption option : requestOptions) { + if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { + uriReplacementOption = (UrlReplaceHandlerOption) option; + } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { + retryHandlerOption = (RetryHandlerOption) option; + } else if (redirectHandlerOption == null && option instanceof RedirectHandlerOption) { + redirectHandlerOption = (RedirectHandlerOption) option; + } else if (parametersNameDecodingOption == null && option instanceof ParametersNameDecodingOption) { + parametersNameDecodingOption = (ParametersNameDecodingOption) option; + } else if (userAgentHandlerOption == null && option instanceof UserAgentHandlerOption) { + userAgentHandlerOption = (UserAgentHandlerOption) option; + } else if (headersInspectionHandlerOption == null && option instanceof HeadersInspectionOption) { + headersInspectionHandlerOption = (HeadersInspectionOption) option; + } } - // Add any remaining default interceptors - for (String handlerType : defaultHandlerTypes) { - handlers.add(createHandler(handlerType)); - } + List handlers = new ArrayList<>(); + handlers.add(uriReplacementOption != null ? new UrlReplaceHandler(uriReplacementOption) : new UrlReplaceHandler()); + handlers.add(retryHandlerOption != null ? new RetryHandler(retryHandlerOption) : new RetryHandler()); + handlers.add(redirectHandlerOption != null ? new RedirectHandler(redirectHandlerOption) : new RedirectHandler()); + handlers.add(parametersNameDecodingOption != null ? new ParametersNameDecodingHandler(parametersNameDecodingOption) : new ParametersNameDecodingHandler()); + handlers.add(userAgentHandlerOption != null ? new UserAgentHandler(userAgentHandlerOption) : new UserAgentHandler()); + handlers.add(headersInspectionHandlerOption != null ? new HeadersInspectionHandler(headersInspectionHandlerOption) : new HeadersInspectionHandler()); return handlers.toArray(new Interceptor[0]); } - - - /** * Creates the default interceptors for the client. * @return an array of interceptors. @@ -128,38 +158,4 @@ private KiotaClientFactory() {} return new ArrayList<>(Arrays.asList(createDefaultInterceptors())); } - - /** - * Gets the default handler types. - * - * @return A list of all the default handlers classnames - * - */ - private static Set getDefaultHandler() { - return new HashSet<>(Arrays.asList( - UrlReplaceHandler.class.toString(), - RedirectHandler.class.toString(), - RetryHandler.class.toString(), - ParametersNameDecodingHandler.class.toString(), - UserAgentHandler.class.toString(), - HeadersInspectionHandler.class.toString())); - } - private static Interceptor createHandler(String handlerType) { - switch (handlerType) { - case "class com.microsoft.kiota.http.middleware.RetryHandler": - return new RetryHandler(); - case "class com.microsoft.kiota.http.middleware.RedirectHandler": - return new RedirectHandler(); - case "class com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler": - return new ParametersNameDecodingHandler(); - case "class com.microsoft.kiota.http.middleware.UserAgentHandler": - return new UserAgentHandler(); - case "class com.microsoft.kiota.http.middleware.HeadersInspectionHandler": - return new HeadersInspectionHandler(); - case "class com.microsoft.kiota.http.middleware.UrlReplaceHandler": - return new UrlReplaceHandler(); - default: - return null; // Handle unknown types as necessary - } - } } diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java index b609495d7..449ceb85c 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.when; +import com.microsoft.kiota.RequestOption; import com.microsoft.kiota.http.middleware.ChaosHandler; import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; @@ -10,9 +11,14 @@ import com.microsoft.kiota.http.middleware.RetryHandler; import com.microsoft.kiota.http.middleware.UrlReplaceHandler; import com.microsoft.kiota.http.middleware.UserAgentHandler; +import com.microsoft.kiota.http.middleware.options.RedirectHandlerOption; import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; +import com.microsoft.kiota.http.middleware.options.UrlReplaceHandlerOption; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -29,12 +35,12 @@ void testCreatesDefaultInterceptors() throws IOException { } @Test - void testOverideDefaultInterceptors() throws IOException { + void testDefaultInterceptorsWhenPassedIn() throws IOException { OkHttpClient client = KiotaClientFactory.create( new Interceptor[]{getDisabledRetryHandler(), new ChaosHandler()}).build(); List interceptors = client.interceptors(); assertNotNull(interceptors); - assertEquals(7, interceptors.size()); + assertEquals(2, interceptors.size()); for (Interceptor interceptor : interceptors) { if (interceptor instanceof RetryHandler) { RetryHandlerOption handlerOption = ((RetryHandler) interceptor).getRetryOptions(); @@ -42,6 +48,39 @@ void testOverideDefaultInterceptors() throws IOException { assertEquals(0, handlerOption.maxRetries()); } + assertTrue(interceptor instanceof RetryHandler || interceptor instanceof ChaosHandler, + "Array should contain instances of RetryHandler and ChaosHandler"); + + } + } + @Test + void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException { + RetryHandlerOption retryHandlerOption = new RetryHandlerOption( + (delay, executionCount, request, response) -> false, 0, 0); + UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(new HashMap<>(),false); + + List options = new ArrayList<>(); + options.add(urlReplaceHandlerOption); + options.add(retryHandlerOption); + + Interceptor[] interceptors = KiotaClientFactory.createDefaultInterceptors(options); + OkHttpClient client = KiotaClientFactory.create(interceptors).build(); + List clientInterceptors = client.interceptors(); + assertNotNull(interceptors); + assertEquals(6, clientInterceptors.size()); + for (Interceptor interceptor : clientInterceptors) { + if (interceptor instanceof RetryHandler) { + RetryHandlerOption handlerOption = ((RetryHandler) interceptor).getRetryOptions(); + assertEquals(0, handlerOption.delay()); + assertEquals(0, handlerOption.maxRetries()); + } + + if (interceptor instanceof UrlReplaceHandler) { + UrlReplaceHandlerOption handlerOption = ((UrlReplaceHandler) interceptor).getUrlReplaceHandlerOption(); + assertTrue( handlerOption.getReplacementPairs().isEmpty()); + assertFalse(handlerOption.isEnabled()); + } + assertTrue(interceptor instanceof UrlReplaceHandler || interceptor instanceof RedirectHandler || interceptor instanceof RetryHandler || interceptor instanceof ParametersNameDecodingHandler || interceptor instanceof UserAgentHandler || interceptor instanceof HeadersInspectionHandler @@ -51,7 +90,6 @@ void testOverideDefaultInterceptors() throws IOException { } - } private static RetryHandler getDisabledRetryHandler() { From 1d904bc600664f7c37b3c40f3ca81a567d5a2e1a Mon Sep 17 00:00:00 2001 From: raghucha Date: Tue, 8 Oct 2024 12:23:57 -0400 Subject: [PATCH 03/15] Update components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java Co-authored-by: Vincent Biret --- .../main/java/com/microsoft/kiota/http/KiotaClientFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index b22a5e955..c34b3898a 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -114,7 +114,7 @@ private KiotaClientFactory() {} } - @Nonnull public static Interceptor[] createDefaultInterceptors(List requestOptions) { + @Nonnull public static Interceptor[] createDefaultInterceptors(@Nullable final List requestOptions) { UrlReplaceHandlerOption uriReplacementOption = null; UserAgentHandlerOption userAgentHandlerOption = null; From c4fe774698838fea2976043c33d30a78580c6eb2 Mon Sep 17 00:00:00 2001 From: raghucha Date: Tue, 8 Oct 2024 12:24:06 -0400 Subject: [PATCH 04/15] Update components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java Co-authored-by: Vincent Biret --- .../main/java/com/microsoft/kiota/http/KiotaClientFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index c34b3898a..acffdad97 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -139,7 +139,7 @@ private KiotaClientFactory() {} } } - List handlers = new ArrayList<>(); + final List handlers = new ArrayList<>(); handlers.add(uriReplacementOption != null ? new UrlReplaceHandler(uriReplacementOption) : new UrlReplaceHandler()); handlers.add(retryHandlerOption != null ? new RetryHandler(retryHandlerOption) : new RetryHandler()); handlers.add(redirectHandlerOption != null ? new RedirectHandler(redirectHandlerOption) : new RedirectHandler()); From 7bc06091280de6eca375466cd34ad1b5e7bc7b8f Mon Sep 17 00:00:00 2001 From: raghucha Date: Tue, 8 Oct 2024 12:24:18 -0400 Subject: [PATCH 05/15] Update components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java Co-authored-by: Vincent Biret --- .../main/java/com/microsoft/kiota/http/KiotaClientFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index acffdad97..b84e3196d 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -123,7 +123,7 @@ private KiotaClientFactory() {} ParametersNameDecodingOption parametersNameDecodingOption = null; HeadersInspectionOption headersInspectionHandlerOption = null; - for (RequestOption option : requestOptions) { + for (final RequestOption option : requestOptions) { if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { uriReplacementOption = (UrlReplaceHandlerOption) option; } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { From fe5211e636f6b4b4b67c6033a39dda22a16a6dba Mon Sep 17 00:00:00 2001 From: Raghu Sammeta Date: Wed, 9 Oct 2024 13:39:59 -0400 Subject: [PATCH 06/15] fix comments --- .../kiota/http/KiotaClientFactory.java | 58 +++++++------------ 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index b84e3196d..c70667769 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -19,8 +19,8 @@ import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; -import java.util.HashSet; -import java.util.Set; +import java.util.HashMap; +import java.util.Map; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -55,7 +55,7 @@ private KiotaClientFactory() {} Duration.ofSeconds( 100)); // TODO configure the default client options. - final Interceptor[] interceptorsOrDefault = createDefaultInterceptors(interceptors); + final Interceptor[] interceptorsOrDefault = interceptors == null? createDefaultInterceptors(): interceptors; for (final Interceptor interceptor : interceptorsOrDefault) { builder.addInterceptor(interceptor); } @@ -92,26 +92,7 @@ private KiotaClientFactory() {} * @return an array of interceptors. */ @Nonnull public static Interceptor[] createDefaultInterceptors() { - return new Interceptor[] { - new UrlReplaceHandler(), - new RedirectHandler(), - new RetryHandler(), - new ParametersNameDecodingHandler(), - new UserAgentHandler(), - new HeadersInspectionHandler() - }; - } - - /** - * Creates the default interceptors for the client. - * @return an array of interceptors. - */ - @Nonnull public static Interceptor[] createDefaultInterceptors( @Nullable Interceptor[] interceptors) { - if(interceptors == null || interceptors.length == 0){ - return createDefaultInterceptors(); - } - return interceptors; - + return createDefaultInterceptors(null); } @Nonnull public static Interceptor[] createDefaultInterceptors(@Nullable final List requestOptions) { @@ -123,21 +104,24 @@ private KiotaClientFactory() {} ParametersNameDecodingOption parametersNameDecodingOption = null; HeadersInspectionOption headersInspectionHandlerOption = null; - for (final RequestOption option : requestOptions) { - if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { - uriReplacementOption = (UrlReplaceHandlerOption) option; - } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { - retryHandlerOption = (RetryHandlerOption) option; - } else if (redirectHandlerOption == null && option instanceof RedirectHandlerOption) { - redirectHandlerOption = (RedirectHandlerOption) option; - } else if (parametersNameDecodingOption == null && option instanceof ParametersNameDecodingOption) { - parametersNameDecodingOption = (ParametersNameDecodingOption) option; - } else if (userAgentHandlerOption == null && option instanceof UserAgentHandlerOption) { - userAgentHandlerOption = (UserAgentHandlerOption) option; - } else if (headersInspectionHandlerOption == null && option instanceof HeadersInspectionOption) { - headersInspectionHandlerOption = (HeadersInspectionOption) option; + if(requestOptions !=null){ + for (final RequestOption option : requestOptions) { + if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { + uriReplacementOption = (UrlReplaceHandlerOption) option; + } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { + retryHandlerOption = (RetryHandlerOption) option; + } else if (redirectHandlerOption == null && option instanceof RedirectHandlerOption) { + redirectHandlerOption = (RedirectHandlerOption) option; + } else if (parametersNameDecodingOption == null && option instanceof ParametersNameDecodingOption) { + parametersNameDecodingOption = (ParametersNameDecodingOption) option; + } else if (userAgentHandlerOption == null && option instanceof UserAgentHandlerOption) { + userAgentHandlerOption = (UserAgentHandlerOption) option; + } else if (headersInspectionHandlerOption == null && option instanceof HeadersInspectionOption) { + headersInspectionHandlerOption = (HeadersInspectionOption) option; + } + } } - } + final List handlers = new ArrayList<>(); handlers.add(uriReplacementOption != null ? new UrlReplaceHandler(uriReplacementOption) : new UrlReplaceHandler()); From 693a9e4ef8caa9839b2bde96ce7c5893f988586e Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 07:58:56 -0400 Subject: [PATCH 07/15] chore: linting --- .../kiota/http/KiotaClientFactory.java | 76 ++++++++++++------- .../kiota/http/KiotaClientFactoryTest.java | 65 ++++++++-------- 2 files changed, 83 insertions(+), 58 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index c70667769..2284b8574 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -9,18 +9,16 @@ import com.microsoft.kiota.http.middleware.RetryHandler; import com.microsoft.kiota.http.middleware.UrlReplaceHandler; import com.microsoft.kiota.http.middleware.UserAgentHandler; - import com.microsoft.kiota.http.middleware.options.HeadersInspectionOption; import com.microsoft.kiota.http.middleware.options.ParametersNameDecodingOption; import com.microsoft.kiota.http.middleware.options.RedirectHandlerOption; import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; import com.microsoft.kiota.http.middleware.options.UrlReplaceHandlerOption; import com.microsoft.kiota.http.middleware.options.UserAgentHandlerOption; + import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; -import java.util.HashMap; -import java.util.Map; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -55,7 +53,8 @@ private KiotaClientFactory() {} Duration.ofSeconds( 100)); // TODO configure the default client options. - final Interceptor[] interceptorsOrDefault = interceptors == null? createDefaultInterceptors(): interceptors; + final Interceptor[] interceptorsOrDefault = + interceptors == null ? createDefaultInterceptors() : interceptors; for (final Interceptor interceptor : interceptorsOrDefault) { builder.addInterceptor(interceptor); } @@ -95,7 +94,8 @@ private KiotaClientFactory() {} return createDefaultInterceptors(null); } - @Nonnull public static Interceptor[] createDefaultInterceptors(@Nullable final List requestOptions) { + @Nonnull public static Interceptor[] createDefaultInterceptors( + @Nullable final List requestOptions) { UrlReplaceHandlerOption uriReplacementOption = null; UserAgentHandlerOption userAgentHandlerOption = null; @@ -104,32 +104,53 @@ private KiotaClientFactory() {} ParametersNameDecodingOption parametersNameDecodingOption = null; HeadersInspectionOption headersInspectionHandlerOption = null; - if(requestOptions !=null){ - for (final RequestOption option : requestOptions) { - if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { - uriReplacementOption = (UrlReplaceHandlerOption) option; - } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { - retryHandlerOption = (RetryHandlerOption) option; - } else if (redirectHandlerOption == null && option instanceof RedirectHandlerOption) { - redirectHandlerOption = (RedirectHandlerOption) option; - } else if (parametersNameDecodingOption == null && option instanceof ParametersNameDecodingOption) { - parametersNameDecodingOption = (ParametersNameDecodingOption) option; - } else if (userAgentHandlerOption == null && option instanceof UserAgentHandlerOption) { - userAgentHandlerOption = (UserAgentHandlerOption) option; - } else if (headersInspectionHandlerOption == null && option instanceof HeadersInspectionOption) { - headersInspectionHandlerOption = (HeadersInspectionOption) option; - } + if (requestOptions != null) { + for (final RequestOption option : requestOptions) { + if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { + uriReplacementOption = (UrlReplaceHandlerOption) option; + } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { + retryHandlerOption = (RetryHandlerOption) option; + } else if (redirectHandlerOption == null + && option instanceof RedirectHandlerOption) { + redirectHandlerOption = (RedirectHandlerOption) option; + } else if (parametersNameDecodingOption == null + && option instanceof ParametersNameDecodingOption) { + parametersNameDecodingOption = (ParametersNameDecodingOption) option; + } else if (userAgentHandlerOption == null + && option instanceof UserAgentHandlerOption) { + userAgentHandlerOption = (UserAgentHandlerOption) option; + } else if (headersInspectionHandlerOption == null + && option instanceof HeadersInspectionOption) { + headersInspectionHandlerOption = (HeadersInspectionOption) option; } } - + } final List handlers = new ArrayList<>(); - handlers.add(uriReplacementOption != null ? new UrlReplaceHandler(uriReplacementOption) : new UrlReplaceHandler()); - handlers.add(retryHandlerOption != null ? new RetryHandler(retryHandlerOption) : new RetryHandler()); - handlers.add(redirectHandlerOption != null ? new RedirectHandler(redirectHandlerOption) : new RedirectHandler()); - handlers.add(parametersNameDecodingOption != null ? new ParametersNameDecodingHandler(parametersNameDecodingOption) : new ParametersNameDecodingHandler()); - handlers.add(userAgentHandlerOption != null ? new UserAgentHandler(userAgentHandlerOption) : new UserAgentHandler()); - handlers.add(headersInspectionHandlerOption != null ? new HeadersInspectionHandler(headersInspectionHandlerOption) : new HeadersInspectionHandler()); + handlers.add( + uriReplacementOption != null + ? new UrlReplaceHandler(uriReplacementOption) + : new UrlReplaceHandler()); + handlers.add( + retryHandlerOption != null + ? new RetryHandler(retryHandlerOption) + : new RetryHandler()); + handlers.add( + redirectHandlerOption != null + ? new RedirectHandler(redirectHandlerOption) + : new RedirectHandler()); + handlers.add( + parametersNameDecodingOption != null + ? new ParametersNameDecodingHandler(parametersNameDecodingOption) + : new ParametersNameDecodingHandler()); + handlers.add( + userAgentHandlerOption != null + ? new UserAgentHandler(userAgentHandlerOption) + : new UserAgentHandler()); + handlers.add( + headersInspectionHandlerOption != null + ? new HeadersInspectionHandler(headersInspectionHandlerOption) + : new HeadersInspectionHandler()); return handlers.toArray(new Interceptor[0]); } @@ -141,5 +162,4 @@ private KiotaClientFactory() {} @Nonnull public static List createDefaultInterceptorsAsList() { return new ArrayList<>(Arrays.asList(createDefaultInterceptors())); } - } diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java index 449ceb85c..724f60ccf 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java @@ -1,7 +1,6 @@ package com.microsoft.kiota.http; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.when; import com.microsoft.kiota.RequestOption; import com.microsoft.kiota.http.middleware.ChaosHandler; @@ -11,19 +10,18 @@ import com.microsoft.kiota.http.middleware.RetryHandler; import com.microsoft.kiota.http.middleware.UrlReplaceHandler; import com.microsoft.kiota.http.middleware.UserAgentHandler; -import com.microsoft.kiota.http.middleware.options.RedirectHandlerOption; import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; import com.microsoft.kiota.http.middleware.options.UrlReplaceHandlerOption; + +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; + +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Map; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; -import org.junit.jupiter.api.Test; public class KiotaClientFactoryTest { @@ -36,8 +34,10 @@ void testCreatesDefaultInterceptors() throws IOException { @Test void testDefaultInterceptorsWhenPassedIn() throws IOException { - OkHttpClient client = KiotaClientFactory.create( - new Interceptor[]{getDisabledRetryHandler(), new ChaosHandler()}).build(); + OkHttpClient client = + KiotaClientFactory.create( + new Interceptor[] {getDisabledRetryHandler(), new ChaosHandler()}) + .build(); List interceptors = client.interceptors(); assertNotNull(interceptors); assertEquals(2, interceptors.size()); @@ -48,16 +48,18 @@ void testDefaultInterceptorsWhenPassedIn() throws IOException { assertEquals(0, handlerOption.maxRetries()); } - assertTrue(interceptor instanceof RetryHandler || interceptor instanceof ChaosHandler, - "Array should contain instances of RetryHandler and ChaosHandler"); - + assertTrue( + interceptor instanceof RetryHandler || interceptor instanceof ChaosHandler, + "Array should contain instances of RetryHandler and ChaosHandler"); } } + @Test void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException { - RetryHandlerOption retryHandlerOption = new RetryHandlerOption( - (delay, executionCount, request, response) -> false, 0, 0); - UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(new HashMap<>(),false); + RetryHandlerOption retryHandlerOption = + new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0); + UrlReplaceHandlerOption urlReplaceHandlerOption = + new UrlReplaceHandlerOption(new HashMap<>(), false); List options = new ArrayList<>(); options.add(urlReplaceHandlerOption); @@ -76,27 +78,30 @@ void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException { } if (interceptor instanceof UrlReplaceHandler) { - UrlReplaceHandlerOption handlerOption = ((UrlReplaceHandler) interceptor).getUrlReplaceHandlerOption(); - assertTrue( handlerOption.getReplacementPairs().isEmpty()); + UrlReplaceHandlerOption handlerOption = + ((UrlReplaceHandler) interceptor).getUrlReplaceHandlerOption(); + assertTrue(handlerOption.getReplacementPairs().isEmpty()); assertFalse(handlerOption.isEnabled()); } - assertTrue(interceptor instanceof UrlReplaceHandler || interceptor instanceof RedirectHandler || - interceptor instanceof RetryHandler || interceptor instanceof ParametersNameDecodingHandler - || interceptor instanceof UserAgentHandler || interceptor instanceof HeadersInspectionHandler - || interceptor instanceof ChaosHandler, - "Array should contain instances of UrlReplaceHandler,RedirectHandler,RetryHandler,ParametersNameDecodingHandler,UserAgentHandler, HeadersInspectionHandler, and ChaosHandler"); - - + assertTrue( + interceptor instanceof UrlReplaceHandler + || interceptor instanceof RedirectHandler + || interceptor instanceof RetryHandler + || interceptor instanceof ParametersNameDecodingHandler + || interceptor instanceof UserAgentHandler + || interceptor instanceof HeadersInspectionHandler + || interceptor instanceof ChaosHandler, + "Array should contain instances of" + + " UrlReplaceHandler,RedirectHandler,RetryHandler,ParametersNameDecodingHandler,UserAgentHandler," + + " HeadersInspectionHandler, and ChaosHandler"); } - } private static RetryHandler getDisabledRetryHandler() { - RetryHandlerOption retryHandlerOption = new RetryHandlerOption( - (delay, executionCount, request, response) -> false, 0, 0); + RetryHandlerOption retryHandlerOption = + new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0); RetryHandler retryHandler = new RetryHandler(retryHandlerOption); return retryHandler; } - -} \ No newline at end of file +} From 0450954f1f8b38043eeaa78f44533cd2bcd456df Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:06:26 -0400 Subject: [PATCH 08/15] fix: makes options non nullable Signed-off-by: Vincent Biret --- .../kiota/http/KiotaClientFactory.java | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 2284b8574..0d82a76f3 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -25,7 +25,9 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Objects; /** This class is used to build the HttpClient instance used by the core service. */ public class KiotaClientFactory { @@ -91,11 +93,12 @@ private KiotaClientFactory() {} * @return an array of interceptors. */ @Nonnull public static Interceptor[] createDefaultInterceptors() { - return createDefaultInterceptors(null); + return createDefaultInterceptors(Collections.emptyList()); } @Nonnull public static Interceptor[] createDefaultInterceptors( - @Nullable final List requestOptions) { + @Nonnull final List requestOptions) { + Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); UrlReplaceHandlerOption uriReplacementOption = null; UserAgentHandlerOption userAgentHandlerOption = null; @@ -104,25 +107,23 @@ private KiotaClientFactory() {} ParametersNameDecodingOption parametersNameDecodingOption = null; HeadersInspectionOption headersInspectionHandlerOption = null; - if (requestOptions != null) { - for (final RequestOption option : requestOptions) { - if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { - uriReplacementOption = (UrlReplaceHandlerOption) option; - } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { - retryHandlerOption = (RetryHandlerOption) option; - } else if (redirectHandlerOption == null - && option instanceof RedirectHandlerOption) { - redirectHandlerOption = (RedirectHandlerOption) option; - } else if (parametersNameDecodingOption == null - && option instanceof ParametersNameDecodingOption) { - parametersNameDecodingOption = (ParametersNameDecodingOption) option; - } else if (userAgentHandlerOption == null - && option instanceof UserAgentHandlerOption) { - userAgentHandlerOption = (UserAgentHandlerOption) option; - } else if (headersInspectionHandlerOption == null - && option instanceof HeadersInspectionOption) { - headersInspectionHandlerOption = (HeadersInspectionOption) option; - } + for (final RequestOption option : requestOptions) { + if (uriReplacementOption == null && option instanceof UrlReplaceHandlerOption) { + uriReplacementOption = (UrlReplaceHandlerOption) option; + } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { + retryHandlerOption = (RetryHandlerOption) option; + } else if (redirectHandlerOption == null + && option instanceof RedirectHandlerOption) { + redirectHandlerOption = (RedirectHandlerOption) option; + } else if (parametersNameDecodingOption == null + && option instanceof ParametersNameDecodingOption) { + parametersNameDecodingOption = (ParametersNameDecodingOption) option; + } else if (userAgentHandlerOption == null + && option instanceof UserAgentHandlerOption) { + userAgentHandlerOption = (UserAgentHandlerOption) option; + } else if (headersInspectionHandlerOption == null + && option instanceof HeadersInspectionOption) { + headersInspectionHandlerOption = (HeadersInspectionOption) option; } } From 44bb987a61eaad112cbe09613b2f65480b1317e3 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:08:42 -0400 Subject: [PATCH 09/15] chore: linting Signed-off-by: Vincent Biret --- .../main/java/com/microsoft/kiota/http/KiotaClientFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 0d82a76f3..d4a173efa 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -73,7 +73,7 @@ private KiotaClientFactory() {} return create(); } return create( - (new ArrayList<>(interceptors)).toArray(new Interceptor[interceptors.size()])); + (new ArrayList<>(interceptors)).toArray(new Interceptor[0])); } /** From a658626e5593486f5f65f78630cc37f3a8711cd4 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:11:41 -0400 Subject: [PATCH 10/15] chore: linting Signed-off-by: Vincent Biret --- .../com/microsoft/kiota/http/KiotaClientFactory.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index d4a173efa..42d7c2ff4 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -72,8 +72,7 @@ private KiotaClientFactory() {} if (interceptors == null) { return create(); } - return create( - (new ArrayList<>(interceptors)).toArray(new Interceptor[0])); + return create((new ArrayList<>(interceptors)).toArray(new Interceptor[0])); } /** @@ -112,14 +111,12 @@ private KiotaClientFactory() {} uriReplacementOption = (UrlReplaceHandlerOption) option; } else if (retryHandlerOption == null && option instanceof RetryHandlerOption) { retryHandlerOption = (RetryHandlerOption) option; - } else if (redirectHandlerOption == null - && option instanceof RedirectHandlerOption) { + } else if (redirectHandlerOption == null && option instanceof RedirectHandlerOption) { redirectHandlerOption = (RedirectHandlerOption) option; } else if (parametersNameDecodingOption == null && option instanceof ParametersNameDecodingOption) { parametersNameDecodingOption = (ParametersNameDecodingOption) option; - } else if (userAgentHandlerOption == null - && option instanceof UserAgentHandlerOption) { + } else if (userAgentHandlerOption == null && option instanceof UserAgentHandlerOption) { userAgentHandlerOption = (UserAgentHandlerOption) option; } else if (headersInspectionHandlerOption == null && option instanceof HeadersInspectionOption) { From e276bb685147d5ec156a3e68b6fa5197405b568b Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:15:45 -0400 Subject: [PATCH 11/15] fix: adds missing doc comment fix: deprecates extraneous method Signed-off-by: Vincent Biret --- .../java/com/microsoft/kiota/http/KiotaClientFactory.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 42d7c2ff4..177c07711 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -95,6 +95,11 @@ private KiotaClientFactory() {} return createDefaultInterceptors(Collections.emptyList()); } + /** + * Creates the default interceptors for the client. + * @param requestOptions The request options to use for the interceptors. + * @return an array of interceptors. + */ @Nonnull public static Interceptor[] createDefaultInterceptors( @Nonnull final List requestOptions) { Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); @@ -156,7 +161,9 @@ private KiotaClientFactory() {} /** * Creates the default interceptors for the client. * @return an array of interceptors. + * @deprecated Use {@link #createDefaultInterceptors()} instead. */ + @Deprecated @Nonnull public static List createDefaultInterceptorsAsList() { return new ArrayList<>(Arrays.asList(createDefaultInterceptors())); } From 507049d6d124a0edd69cacda0c4b2cbcaa7495bd Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:21:54 -0400 Subject: [PATCH 12/15] fix: misaliagments between methods API surface --- .../kiota/http/KiotaClientFactory.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 177c07711..3b810e948 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -17,7 +17,6 @@ import com.microsoft.kiota.http.middleware.options.UserAgentHandlerOption; import jakarta.annotation.Nonnull; -import jakarta.annotation.Nullable; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -46,7 +45,8 @@ private KiotaClientFactory() {} * @param interceptors The interceptors to add to the client. Will default to createDefaultInterceptors() if null. * @return an OkHttpClient Builder instance. */ - @Nonnull public static OkHttpClient.Builder create(@Nullable final Interceptor[] interceptors) { + @Nonnull public static OkHttpClient.Builder create(@Nonnull final Interceptor[] interceptors) { + Objects.requireNonNull(interceptors, "parameter interceptors cannot be null"); final OkHttpClient.Builder builder = new OkHttpClient.Builder() .connectTimeout(Duration.ofSeconds(100)) @@ -55,9 +55,7 @@ private KiotaClientFactory() {} Duration.ofSeconds( 100)); // TODO configure the default client options. - final Interceptor[] interceptorsOrDefault = - interceptors == null ? createDefaultInterceptors() : interceptors; - for (final Interceptor interceptor : interceptorsOrDefault) { + for (final Interceptor interceptor : interceptors) { builder.addInterceptor(interceptor); } return builder; @@ -68,10 +66,8 @@ private KiotaClientFactory() {} * @param interceptors The interceptors to add to the client. Will default to createDefaultInterceptors() if null. * @return an OkHttpClient Builder instance. */ - @Nonnull public static OkHttpClient.Builder create(@Nullable final List interceptors) { - if (interceptors == null) { - return create(); - } + @Nonnull public static OkHttpClient.Builder create(@Nonnull final List interceptors) { + Objects.requireNonNull(interceptors, "parameter interceptors cannot be null"); return create((new ArrayList<>(interceptors)).toArray(new Interceptor[0])); } @@ -82,7 +78,8 @@ private KiotaClientFactory() {} */ @Nonnull public static OkHttpClient.Builder create( @Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) { - ArrayList interceptors = new ArrayList<>(createDefaultInterceptorsAsList()); + ArrayList interceptors = + new ArrayList<>(Arrays.asList(createDefaultInterceptors())); interceptors.add(new AuthorizationHandler(authenticationProvider)); return create(interceptors); } From f8617cc6249e53ab1ae6fedf1318947c130fe890 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:24:27 -0400 Subject: [PATCH 13/15] feat: adds create method for options Signed-off-by: Vincent Biret --- .../com/microsoft/kiota/http/KiotaClientFactory.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 3b810e948..d9762f56a 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -40,6 +40,16 @@ private KiotaClientFactory() {} return create(createDefaultInterceptors()); } + /** + * Creates an OkHttpClient Builder with the default configuration and middleware options. + * @param requestOptions The request options to use for the interceptors. + * @return an OkHttpClient Builder instance. + */ + @Nonnull public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) { + Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); + return create(createDefaultInterceptors(Arrays.asList(requestOptions))); + } + /** * Creates an OkHttpClient Builder with the default configuration and middleware. * @param interceptors The interceptors to add to the client. Will default to createDefaultInterceptors() if null. From 210773802b4ddc4e9783279cb343219dbf16aa83 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:30:53 -0400 Subject: [PATCH 14/15] fix: aligns API surface parameter types Signed-off-by: Vincent Biret --- .../java/com/microsoft/kiota/http/KiotaClientFactory.java | 7 +++---- .../com/microsoft/kiota/http/KiotaClientFactoryTest.java | 5 +++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index d9762f56a..5996739dc 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -24,7 +24,6 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -47,7 +46,7 @@ private KiotaClientFactory() {} */ @Nonnull public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) { Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); - return create(createDefaultInterceptors(Arrays.asList(requestOptions))); + return create(createDefaultInterceptors(requestOptions)); } /** @@ -99,7 +98,7 @@ private KiotaClientFactory() {} * @return an array of interceptors. */ @Nonnull public static Interceptor[] createDefaultInterceptors() { - return createDefaultInterceptors(Collections.emptyList()); + return createDefaultInterceptors(new RequestOption[0]); } /** @@ -108,7 +107,7 @@ private KiotaClientFactory() {} * @return an array of interceptors. */ @Nonnull public static Interceptor[] createDefaultInterceptors( - @Nonnull final List requestOptions) { + @Nonnull final RequestOption[] requestOptions) { Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); UrlReplaceHandlerOption uriReplacementOption = null; diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java index 724f60ccf..7dd3eda0f 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java @@ -61,11 +61,12 @@ void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException { UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(new HashMap<>(), false); - List options = new ArrayList<>(); + final ArrayList options = new ArrayList<>(); options.add(urlReplaceHandlerOption); options.add(retryHandlerOption); - Interceptor[] interceptors = KiotaClientFactory.createDefaultInterceptors(options); + Interceptor[] interceptors = + KiotaClientFactory.createDefaultInterceptors(options.toArray(new RequestOption[0])); OkHttpClient client = KiotaClientFactory.create(interceptors).build(); List clientInterceptors = client.interceptors(); assertNotNull(interceptors); From bfde2a1daa345eb6e65edde09696bea275cd4227 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 11 Oct 2024 08:49:33 -0400 Subject: [PATCH 15/15] fix; interceptors order Signed-off-by: Vincent Biret --- .../kiota/http/KiotaClientFactory.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 5996739dc..992bb0ca6 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -136,30 +136,33 @@ private KiotaClientFactory() {} } final List handlers = new ArrayList<>(); + // orders matter as they are executed in a chain + // interceptors that only modify the request should be added first + // interceptors that read the response should be added last handlers.add( - uriReplacementOption != null - ? new UrlReplaceHandler(uriReplacementOption) - : new UrlReplaceHandler()); - handlers.add( - retryHandlerOption != null - ? new RetryHandler(retryHandlerOption) - : new RetryHandler()); - handlers.add( - redirectHandlerOption != null - ? new RedirectHandler(redirectHandlerOption) - : new RedirectHandler()); + userAgentHandlerOption != null + ? new UserAgentHandler(userAgentHandlerOption) + : new UserAgentHandler()); handlers.add( parametersNameDecodingOption != null ? new ParametersNameDecodingHandler(parametersNameDecodingOption) : new ParametersNameDecodingHandler()); handlers.add( - userAgentHandlerOption != null - ? new UserAgentHandler(userAgentHandlerOption) - : new UserAgentHandler()); + uriReplacementOption != null + ? new UrlReplaceHandler(uriReplacementOption) + : new UrlReplaceHandler()); handlers.add( headersInspectionHandlerOption != null ? new HeadersInspectionHandler(headersInspectionHandlerOption) : new HeadersInspectionHandler()); + handlers.add( + redirectHandlerOption != null + ? new RedirectHandler(redirectHandlerOption) + : new RedirectHandler()); + handlers.add( + retryHandlerOption != null + ? new RetryHandler(retryHandlerOption) + : new RetryHandler()); return handlers.toArray(new Interceptor[0]); }