Skip to content

Commit

Permalink
chore: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Oct 11, 2024
1 parent 48beda2 commit 693a9e4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -95,7 +94,8 @@ private KiotaClientFactory() {}
return createDefaultInterceptors(null);
}

@Nonnull public static Interceptor[] createDefaultInterceptors(@Nullable final List<RequestOption> requestOptions) {
@Nonnull public static Interceptor[] createDefaultInterceptors(
@Nullable final List<RequestOption> requestOptions) {

UrlReplaceHandlerOption uriReplacementOption = null;
UserAgentHandlerOption userAgentHandlerOption = null;
Expand All @@ -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<Interceptor> 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]);
}
Expand All @@ -141,5 +162,4 @@ private KiotaClientFactory() {}
@Nonnull public static List<Interceptor> createDefaultInterceptorsAsList() {
return new ArrayList<>(Arrays.asList(createDefaultInterceptors()));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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<Interceptor> interceptors = client.interceptors();
assertNotNull(interceptors);
assertEquals(2, interceptors.size());
Expand All @@ -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<RequestOption> options = new ArrayList<>();
options.add(urlReplaceHandlerOption);
Expand All @@ -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;
}

}
}

0 comments on commit 693a9e4

Please sign in to comment.