Skip to content

Commit 730d744

Browse files
committed
feat: Support overriding default interceptors via request options
1 parent 1ddb88b commit 730d744

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.microsoft.graph.core.CoreConstants;
44
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
55
import com.microsoft.graph.core.requests.options.GraphClientOption;
6+
import com.microsoft.kiota.RequestOption;
67
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
78
import com.microsoft.kiota.http.KiotaClientFactory;
89
import com.microsoft.kiota.http.middleware.AuthorizationHandler;
@@ -58,8 +59,13 @@ public static OkHttpClient.Builder create(@Nonnull List<Interceptor> interceptor
5859
*/
5960
@Nonnull
6061
public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider) {
62+
return create(authenticationProvider, new RequestOption[0]);
63+
}
64+
65+
@Nonnull
66+
public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull RequestOption[] requestOptions) {
6167
final GraphClientOption graphClientOption = new GraphClientOption();
62-
final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption);
68+
final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption, requestOptions);
6369
final ArrayList<Interceptor> interceptorList = new ArrayList<>(Arrays.asList(interceptors));
6470
interceptorList.add(new AuthorizationHandler(authenticationProvider));
6571
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG);
@@ -75,7 +81,13 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication
7581
*/
7682
@Nonnull
7783
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) {
78-
return KiotaClientFactory.create(interceptors);
84+
var builder = KiotaClientFactory.create(interceptors);
85+
var customInterceptors = builder.interceptors();
86+
var telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler);
87+
if (!telemetryHandlerExists) {
88+
customInterceptors.add(new GraphTelemetryHandler(graphClientOption));
89+
}
90+
return builder;
7991
}
8092

8193
/**
@@ -97,9 +109,15 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
97109
*/
98110
@Nonnull
99111
public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption) {
112+
return create(graphClientOption, new RequestOption[0]);
113+
}
114+
115+
@Nonnull
116+
public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) {
100117
GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption();
101-
return KiotaClientFactory.create(createDefaultGraphInterceptors(options));
118+
return KiotaClientFactory.create(createDefaultGraphInterceptors(options, requestOptions));
102119
}
120+
103121
/**
104122
* Creates the default Interceptors for use with Graph.
105123
*
@@ -108,14 +126,31 @@ public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClien
108126
*/
109127
@Nonnull
110128
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption) {
111-
List<Interceptor> handlers = new ArrayList<>();
112-
addDefaultFeatureUsages(graphClientOption);
129+
return createDefaultGraphInterceptors(graphClientOption, new RequestOption[0]);
130+
}
131+
132+
@Nonnull
133+
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) {
134+
Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null");
135+
136+
UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs());
113137

114-
handlers.add(new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs())));
138+
for (RequestOption option : requestOptions) {
139+
if (option instanceof UrlReplaceHandlerOption) {
140+
urlReplaceHandlerOption = (UrlReplaceHandlerOption) option;
141+
}
142+
}
143+
144+
List<Interceptor> handlers = new ArrayList<>();
145+
handlers.add(urlReplaceHandlerOption == null ?
146+
new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs())) :
147+
new UrlReplaceHandler(urlReplaceHandlerOption));
115148
handlers.add(new GraphTelemetryHandler(graphClientOption));
116-
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors()));
149+
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions)));
150+
addDefaultFeatureUsages(graphClientOption);
117151
return handlers.toArray(new Interceptor[0]);
118152
}
153+
119154
//These are the default features used by the Graph Client
120155
private static void addDefaultFeatureUsages(GraphClientOption graphClientOption) {
121156
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.RETRY_HANDLER_FLAG);

src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010

1111
import java.io.IOException;
1212
import java.net.URI;
13+
import java.util.ArrayList;
1314

1415
import org.jetbrains.annotations.NotNull;
1516
import org.junit.jupiter.api.Assertions;
1617
import org.junit.jupiter.api.Test;
1718

19+
import com.azure.core.http.policy.RetryOptions;
1820
import com.microsoft.graph.core.CoreConstants;
1921
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
2022
import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider;
2123
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
24+
import com.microsoft.kiota.RequestOption;
2225
import com.microsoft.kiota.authentication.AccessTokenProvider;
2326
import com.microsoft.kiota.authentication.AllowedHostsValidator;
2427
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
@@ -50,6 +53,35 @@ void testCreateWithAuthenticationProvider() throws IOException {
5053
assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization"));
5154
}
5255

56+
@Test
57+
void testCreateWithAuthenticationProviderAndCustomRequestOptions() throws IOException {
58+
final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider =
59+
getMockAuthenticationProvider();
60+
var requestOptions = new ArrayList<RequestOption>();
61+
requestOptions.add(new RetryHandlerOption(null, 0, 0));
62+
OkHttpClient graphClient = GraphClientFactory.create(mockAuthenticationProvider, requestOptions.toArray(new RequestOption[0])).addInterceptor(new MockResponseHandler()).build();
63+
64+
var interceptors = graphClient.interceptors();
65+
for (Interceptor interceptor : interceptors) {
66+
if (interceptor instanceof RetryHandler) {
67+
RetryHandlerOption retryOptions = ((RetryHandler) interceptor).getRetryOptions();
68+
Assertions.assertEquals(0, retryOptions.maxRetries());
69+
Assertions.assertEquals(0, retryOptions.delay());
70+
}
71+
}
72+
73+
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me")
74+
.addHeader("CustomHeader", "CustomValue").build();
75+
Response response = graphClient.newCall(request).execute();
76+
77+
assertEquals(200, response.code());
78+
assertNotNull(response.request());
79+
assertTrue(response.request().headers().names().contains("Authorization"));
80+
assertTrue(response.request().headers().names().contains("CustomHeader"));
81+
assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization"));
82+
assertEquals("CustomValue", response.request().header("CustomHeader"));
83+
}
84+
5385
@Test
5486
void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException {
5587

0 commit comments

Comments
 (0)