forked from intercom/intercom-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntercomBuilder.java
More file actions
206 lines (183 loc) · 6.45 KB
/
Copy pathIntercomBuilder.java
File metadata and controls
206 lines (183 loc) · 6.45 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.intercom.api;
import com.intercom.api.core.ClientOptions;
import com.intercom.api.core.Environment;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import okhttp3.OkHttpClient;
public class IntercomBuilder {
private Optional<Integer> timeout = Optional.empty();
private Optional<Integer> maxRetries = Optional.empty();
private final Map<String, String> customHeaders = new HashMap<>();
private String token = System.getenv("INTERCOM_API_KEY");
private Environment environment = Environment.US_PRODUCTION;
private OkHttpClient httpClient;
/**
* Sets token.
* Defaults to the INTERCOM_API_KEY environment variable.
*/
public IntercomBuilder token(String token) {
this.token = token;
return this;
}
public IntercomBuilder environment(Environment environment) {
this.environment = environment;
return this;
}
public IntercomBuilder url(String url) {
this.environment = Environment.custom(url);
return this;
}
/**
* Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
*/
public IntercomBuilder timeout(int timeout) {
this.timeout = Optional.of(timeout);
return this;
}
/**
* Sets the maximum number of retries for the client. Defaults to 2 retries.
*/
public IntercomBuilder maxRetries(int maxRetries) {
this.maxRetries = Optional.of(maxRetries);
return this;
}
/**
* Sets the underlying OkHttp client
*/
public IntercomBuilder httpClient(OkHttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
/**
* Add a custom header to be sent with all requests.
* For headers that need to be computed dynamically or conditionally, use the setAdditional() method override instead.
*
* @param name The header name
* @param value The header value
* @return This builder for method chaining
*/
public IntercomBuilder addHeader(String name, String value) {
this.customHeaders.put(name, value);
return this;
}
protected ClientOptions buildClientOptions() {
ClientOptions.Builder builder = ClientOptions.builder();
setEnvironment(builder);
setAuthentication(builder);
setHttpClient(builder);
setTimeouts(builder);
setRetries(builder);
for (Map.Entry<String, String> header : this.customHeaders.entrySet()) {
builder.addHeader(header.getKey(), header.getValue());
}
setAdditional(builder);
return builder.build();
}
/**
* Sets the environment configuration for the client.
* Override this method to modify URLs or add environment-specific logic.
*
* @param builder The ClientOptions.Builder to configure
*/
protected void setEnvironment(ClientOptions.Builder builder) {
builder.environment(this.environment);
}
/**
* Override this method to customize authentication.
* This method is called during client options construction to set up authentication headers.
*
* @param builder The ClientOptions.Builder to configure
*
* Example:
* <pre>{@code
* @Override
* protected void setAuthentication(ClientOptions.Builder builder) {
* super.setAuthentication(builder); // Keep existing auth
* builder.addHeader("X-API-Key", this.apiKey);
* }
* }</pre>
*/
protected void setAuthentication(ClientOptions.Builder builder) {
if (this.token != null) {
builder.addHeader("Authorization", "Bearer " + this.token);
}
}
/**
* Sets the request timeout configuration.
* Override this method to customize timeout behavior.
*
* @param builder The ClientOptions.Builder to configure
*/
protected void setTimeouts(ClientOptions.Builder builder) {
if (this.timeout.isPresent()) {
builder.timeout(this.timeout.get());
}
}
/**
* Sets the retry configuration for failed requests.
* Override this method to implement custom retry strategies.
*
* @param builder The ClientOptions.Builder to configure
*/
protected void setRetries(ClientOptions.Builder builder) {
if (this.maxRetries.isPresent()) {
builder.maxRetries(this.maxRetries.get());
}
}
/**
* Sets the OkHttp client configuration.
* Override this method to customize HTTP client behavior (interceptors, connection pools, etc).
*
* @param builder The ClientOptions.Builder to configure
*/
protected void setHttpClient(ClientOptions.Builder builder) {
if (this.httpClient != null) {
builder.httpClient(this.httpClient);
}
}
/**
* Override this method to add any additional configuration to the client.
* This method is called at the end of the configuration chain, allowing you to add
* custom headers, modify settings, or perform any other client customization.
*
* @param builder The ClientOptions.Builder to configure
*
* Example:
* <pre>{@code
* @Override
* protected void setAdditional(ClientOptions.Builder builder) {
* builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
* builder.addHeader("X-Client-Version", "1.0.0");
* }
* }</pre>
*/
protected void setAdditional(ClientOptions.Builder builder) {}
/**
* Override this method to add custom validation logic before the client is built.
* This method is called at the beginning of the build() method to ensure the configuration is valid.
* Throw an exception to prevent client creation if validation fails.
*
* Example:
* <pre>{@code
* @Override
* protected void validateConfiguration() {
* super.validateConfiguration(); // Run parent validations
* if (tenantId == null || tenantId.isEmpty()) {
* throw new IllegalStateException("tenantId is required");
* }
* }
* }</pre>
*/
protected void validateConfiguration() {}
public Intercom build() {
if (token == null) {
throw new RuntimeException("Please provide token or set the INTERCOM_API_KEY environment variable.");
}
validateConfiguration();
return new Intercom(buildClientOptions());
}
}