-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClientOptions.java
More file actions
105 lines (87 loc) · 3.2 KB
/
ClientOptions.java
File metadata and controls
105 lines (87 loc) · 3.2 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.seam.api.core;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import okhttp3.OkHttpClient;
public final class ClientOptions {
private final Environment environment;
private final Map<String, String> headers;
private final Map<String, Supplier<String>> headerSuppliers;
private final OkHttpClient httpClient;
private ClientOptions(
Environment environment,
Map<String, String> headers,
Map<String, Supplier<String>> headerSuppliers,
OkHttpClient httpClient) {
this.environment = environment;
this.headers = new HashMap<>();
this.headers.putAll(headers);
this.headers.putAll(new HashMap<String, String>() {
{
put("X-Fern-Language", "JAVA");
put("X-Fern-SDK-Name", "com.seam.fern:api-sdk");
put("X-Fern-SDK-Version", "0.3.0");
}
});
this.headerSuppliers = headerSuppliers;
this.httpClient = httpClient;
}
public Environment environment() {
return this.environment;
}
public Map<String, String> headers(RequestOptions requestOptions) {
Map<String, String> values = new HashMap<>(this.headers);
headerSuppliers.forEach((key, supplier) -> {
values.put(key, supplier.get());
});
if (requestOptions != null) {
values.putAll(requestOptions.getHeaders());
}
return values;
}
public OkHttpClient httpClient() {
return this.httpClient;
}
public OkHttpClient httpClientWithTimeout(RequestOptions requestOptions) {
if (requestOptions == null) {
return this.httpClient;
}
return this.httpClient
.newBuilder()
.callTimeout(requestOptions.getTimeout().get(), requestOptions.getTimeoutTimeUnit())
.connectTimeout(0, TimeUnit.SECONDS)
.writeTimeout(0, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.SECONDS)
.build();
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private Environment environment;
private final Map<String, String> headers = new HashMap<>();
private final Map<String, Supplier<String>> headerSuppliers = new HashMap<>();
public Builder environment(Environment environment) {
this.environment = environment;
return this;
}
public Builder addHeader(String key, String value) {
this.headers.put(key, value);
return this;
}
public Builder addHeader(String key, Supplier<String> value) {
this.headerSuppliers.put(key, value);
return this;
}
public ClientOptions build() {
OkHttpClient okhttpClient = new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(3))
.build();
return new ClientOptions(environment, headers, headerSuppliers, okhttpClient);
}
}
}