-
Notifications
You must be signed in to change notification settings - Fork 21
/
KiotaClientFactory.java
58 lines (54 loc) · 2.24 KB
/
KiotaClientFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.microsoft.kiota.http;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.time.Duration;
import com.microsoft.kiota.http.middleware.RedirectHandler;
import com.microsoft.kiota.http.middleware.RetryHandler;
import com.microsoft.kiota.http.middleware.UserAgentHandler;
import com.microsoft.kiota.http.middleware.HeadersInspectionHandler;
import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
/** This class is used to build the HttpClient instance used by the core service. */
public class KiotaClientFactory {
private KiotaClientFactory() { }
/**
* Creates an OkHttpClient Builder with the default configuration and middleware.
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create() {
return create(null);
}
/**
* 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.
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nullable final Interceptor[] interceptors) {
final OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(Duration.ofSeconds(100))
.readTimeout(Duration.ofSeconds(100))
.callTimeout(Duration.ofSeconds(100)); //TODO configure the default client options.
final Interceptor[] interceptorsOrDefault = interceptors != null ? interceptors : createDefaultInterceptors();
for (final Interceptor interceptor : interceptorsOrDefault) {
builder.addInterceptor(interceptor);
}
return builder;
}
/**
* Creates the default interceptors for the client.
* @return an array of interceptors.
*/
@Nonnull
public static Interceptor[] createDefaultInterceptors() {
return new Interceptor[] {
new RedirectHandler(),
new RetryHandler(),
new ParametersNameDecodingHandler(),
new UserAgentHandler(),
new HeadersInspectionHandler()
};
}
}