Skip to content

Commit 696f8a7

Browse files
committed
- adds the ability to configure the http client used by the service client
1 parent 969ba11 commit 696f8a7

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/main/java/com/microsoft/graph/core/DefaultClientConfig.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.microsoft.graph.serializer.DefaultSerializer;
3333
import com.microsoft.graph.serializer.ISerializer;
3434

35+
import okhttp3.OkHttpClient;
36+
3537
/**
3638
* The default configuration for a service client
3739
*/
@@ -55,7 +57,7 @@ public abstract class DefaultClientConfig implements IClientConfig {
5557
/**
5658
* The serializer instance
5759
*/
58-
private DefaultSerializer serializer;
60+
private ISerializer serializer;
5961

6062
/**
6163
* Creates an instance of this configuration with an authentication provider
@@ -97,11 +99,22 @@ public IAuthenticationProvider getAuthenticationProvider() {
9799
*/
98100
@Override
99101
public IHttpProvider getHttpProvider() {
102+
return this.getHttpProvider(null);
103+
}
104+
105+
/**
106+
* Gets the HTTP provider
107+
*
108+
* @param httpClient the http client to pass to the http provider when building it
109+
* @param <T1> the http client type
110+
* @return the HTTP provider
111+
*/
112+
@Override
113+
public <T1> IHttpProvider getHttpProvider(final T1 httpClient) {
100114
if (httpProvider == null) {
101-
httpProvider = new CoreHttpProvider(getSerializer(),
102-
getAuthenticationProvider(),
103-
getExecutors(),
104-
getLogger());
115+
httpProvider = (httpClient instanceof OkHttpClient) ?
116+
new CoreHttpProvider(this, (OkHttpClient) httpClient) :
117+
new CoreHttpProvider(this, null);
105118
getLogger().logDebug("Created CoreHttpProvider");
106119
}
107120
return httpProvider;

src/main/java/com/microsoft/graph/core/IClientConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public interface IClientConfig {
5353
*/
5454
IHttpProvider getHttpProvider();
5555

56+
/**
57+
* Gets the HTTP provider
58+
*
59+
* @param httpClient the http client to pass to the http provider when building it
60+
* @param <T1> the http client type
61+
* @return the HTTP provider
62+
*/
63+
<T1> IHttpProvider getHttpProvider(final T1 httpClient);
64+
5665
/**
5766
* Gets the logger
5867
*

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.microsoft.graph.core.ClientException;
4747
import com.microsoft.graph.core.Constants;
4848
import com.microsoft.graph.core.DefaultConnectionConfig;
49+
import com.microsoft.graph.core.IClientConfig;
4950
import com.microsoft.graph.core.IConnectionConfig;
5051
import com.microsoft.graph.httpcore.HttpClients;
5152
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
@@ -119,6 +120,18 @@ public CoreHttpProvider(final ISerializer serializer,
119120
this.logger = logger;
120121
}
121122

123+
/**
124+
* Creates the DefaultHttpProvider
125+
*
126+
* @param clientConfig the client configuration to use for the provider
127+
* @param httpClient the http client to execute the requests with
128+
*/
129+
public CoreHttpProvider(final IClientConfig clientConfig,
130+
final OkHttpClient httpClient) {
131+
this(clientConfig.getSerializer(), clientConfig.getAuthenticationProvider(), clientConfig.getExecutors(), clientConfig.getLogger());
132+
this.corehttpClient = httpClient;
133+
}
134+
122135
/**
123136
* Gets the serializer for this HTTP provider
124137
*
@@ -144,9 +157,9 @@ public <Result, Body> void send(final IHttpRequest request,
144157
final ICallback<? super Result> callback,
145158
final Class<Result> resultClass,
146159
final Body serializable) {
147-
final IProgressCallback<Result> progressCallback;
160+
final IProgressCallback<? super Result> progressCallback;
148161
if (callback instanceof IProgressCallback) {
149-
progressCallback = (IProgressCallback<Result>) callback;
162+
progressCallback = (IProgressCallback<? super Result>) callback;
150163
} else {
151164
progressCallback = null;
152165
}
@@ -221,7 +234,7 @@ public <Result, Body, DeserializeType> Result send(final IHttpRequest request,
221234
public <Result, Body> Request getHttpRequest(final IHttpRequest request,
222235
final Class<Result> resultClass,
223236
final Body serializable,
224-
final IProgressCallback<Result> progress) throws ClientException {
237+
final IProgressCallback<? super Result> progress) throws ClientException {
225238
final int defaultBufferSize = 4096;
226239

227240
final URL requestUrl = request.getRequestUrl();
@@ -354,7 +367,7 @@ public MediaType contentType() {
354367
private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRequest request,
355368
final Class<Result> resultClass,
356369
final Body serializable,
357-
final IProgressCallback<Result> progress,
370+
final IProgressCallback<? super Result> progress,
358371
final IStatefulResponseHandler<Result, DeserializeType> handler)
359372
throws ClientException {
360373

@@ -374,11 +387,11 @@ public Request authenticateRequest(Request request) {
374387
.newBuilder()
375388
.connectTimeout(connectionConfig.getConnectTimeout(), TimeUnit.MILLISECONDS)
376389
.readTimeout(connectionConfig.getReadTimeout(), TimeUnit.MILLISECONDS)
377-
.followRedirects(false)
390+
.followRedirects(false) //TODO those are duplicated for the time being with core, remove in 3.0
378391
.protocols(Arrays.asList(Protocol.HTTP_1_1)) //https://stackoverflow.com/questions/62031298/sockettimeout-on-java-11-but-not-on-java-8
379392
.build();
380393
}
381-
if (authenticationProvider != null) {
394+
if (authenticationProvider != null) { //TODO remove the authentication provider as this is done by the interceptor anyway
382395
authenticationProvider.authenticateRequest(request);
383396
}
384397
Request coreHttpRequest = getHttpRequest(request, resultClass, serializable, progress);

0 commit comments

Comments
 (0)