Skip to content

Commit

Permalink
feat: Support overriding default interceptor options when creating Ok…
Browse files Browse the repository at this point in the history
…Http clients with authentication enabled
  • Loading branch information
Ndiritu committed Nov 12, 2024
1 parent cc7596d commit 7876bab
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,20 @@ private KiotaClientFactory() {}
*/
@Nonnull public static OkHttpClient.Builder create(
@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) {
return create(authenticationProvider, new RequestOption[0]);
}

/**
* Creates an OkHttpClient Builder with the Authorization handler and optional custom default middleware configurations
* @param authenticationProvider authentication provider to use for the AuthorizationHandler.
* @param requestOptions The request options to use for the interceptors.
* @return an OkHttpClient Builder instance.
*/
@Nonnull public static OkHttpClient.Builder create(
@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider,
@Nonnull final RequestOption[] requestOptions) {
ArrayList<Interceptor> interceptors =
new ArrayList<>(Arrays.asList(createDefaultInterceptors()));
new ArrayList<>(Arrays.asList(createDefaultInterceptors(requestOptions)));
interceptors.add(new AuthorizationHandler(authenticationProvider));
return create(interceptors);
}
Expand Down Expand Up @@ -173,7 +185,7 @@ private KiotaClientFactory() {}
* @deprecated Use {@link #createDefaultInterceptors()} instead.
*/
@Deprecated
@Nonnull public static List<Interceptor> createDefaultInterceptorsAsList() {
@Nonnull private static List<Interceptor> createDefaultInterceptorsAsList() {
return new ArrayList<>(Arrays.asList(createDefaultInterceptors()));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.microsoft.kiota.http;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

import com.microsoft.kiota.RequestOption;
import com.microsoft.kiota.authentication.AccessTokenProvider;
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
import com.microsoft.kiota.http.middleware.AuthorizationHandler;
import com.microsoft.kiota.http.middleware.ChaosHandler;
import com.microsoft.kiota.http.middleware.HeadersInspectionHandler;
import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler;
Expand Down Expand Up @@ -99,6 +103,56 @@ void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException {
}
}

@Test
void testCreateWithAuthProviderAndRequestOptions() throws IOException {
RetryHandlerOption retryHandlerOption =
new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0);
UrlReplaceHandlerOption urlReplaceHandlerOption =
new UrlReplaceHandlerOption(new HashMap<>(), false);

final ArrayList<RequestOption> options = new ArrayList<>();
options.add(urlReplaceHandlerOption);
options.add(retryHandlerOption);

OkHttpClient client =
KiotaClientFactory.create(
new BaseBearerTokenAuthenticationProvider(
mock(AccessTokenProvider.class)),
options.toArray(new RequestOption[0]))
.build();
List<Interceptor> clientInterceptors = client.interceptors();
assertNotNull(clientInterceptors);
// including the Authorization Handler
assertEquals(7, 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
|| interceptor instanceof ChaosHandler
|| interceptor instanceof AuthorizationHandler,
"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);
Expand Down

0 comments on commit 7876bab

Please sign in to comment.