-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncCloudPDFClientBuilder.java
More file actions
263 lines (232 loc) · 8.48 KB
/
Copy pathAsyncCloudPDFClientBuilder.java
File metadata and controls
263 lines (232 loc) · 8.48 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.cloudpdf.api;
import com.cloudpdf.api.core.ClientOptions;
import com.cloudpdf.api.core.Environment;
import com.cloudpdf.api.core.LogConfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import okhttp3.OkHttpClient;
public class AsyncCloudPDFClientBuilder {
private Optional<Integer> timeout = Optional.empty();
private Optional<Integer> maxRetries = Optional.empty();
private Optional<Long> initialRetryDelayMillis = Optional.empty();
private Optional<Long> maxRetryDelayMillis = Optional.empty();
private Optional<Double> retryJitterFactor = Optional.empty();
private final Map<String, String> customHeaders = new HashMap<>();
private String token = null;
private Environment environment;
private OkHttpClient httpClient;
private Optional<LogConfig> logging = Optional.empty();
/**
* Sets token
*/
public AsyncCloudPDFClientBuilder token(String token) {
this.token = token;
return this;
}
public AsyncCloudPDFClientBuilder url(String url) {
this.environment = Environment.custom(url);
return this;
}
/**
* Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
*/
public AsyncCloudPDFClientBuilder timeout(int timeout) {
this.timeout = Optional.of(timeout);
return this;
}
/**
* Sets the maximum number of retries for the client. Defaults to 2 retries.
*/
public AsyncCloudPDFClientBuilder maxRetries(int maxRetries) {
this.maxRetries = Optional.of(maxRetries);
return this;
}
/**
* Sets the initial delay (in milliseconds) used for exponential backoff between retries. Defaults to 1000 milliseconds.
*/
public AsyncCloudPDFClientBuilder initialRetryDelayMillis(long initialRetryDelayMillis) {
this.initialRetryDelayMillis = Optional.of(initialRetryDelayMillis);
return this;
}
/**
* Sets the maximum delay (in milliseconds) between retries. Defaults to 60000 milliseconds.
*/
public AsyncCloudPDFClientBuilder maxRetryDelayMillis(long maxRetryDelayMillis) {
this.maxRetryDelayMillis = Optional.of(maxRetryDelayMillis);
return this;
}
/**
* Sets the jitter factor (between 0 and 1) applied to retry delays. Defaults to 0.2.
*/
public AsyncCloudPDFClientBuilder retryJitterFactor(double retryJitterFactor) {
this.retryJitterFactor = Optional.of(retryJitterFactor);
return this;
}
/**
* Sets the underlying OkHttp client
*/
public AsyncCloudPDFClientBuilder httpClient(OkHttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
/**
* Configure logging for the SDK. Silent by default — no log output unless explicitly configured.
*/
public AsyncCloudPDFClientBuilder logging(LogConfig logging) {
this.logging = Optional.of(logging);
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 AsyncCloudPDFClientBuilder 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);
setLogging(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());
}
if (this.initialRetryDelayMillis.isPresent()) {
builder.initialRetryDelayMillis(this.initialRetryDelayMillis.get());
}
if (this.maxRetryDelayMillis.isPresent()) {
builder.maxRetryDelayMillis(this.maxRetryDelayMillis.get());
}
if (this.retryJitterFactor.isPresent()) {
builder.retryJitterFactor(this.retryJitterFactor.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);
}
}
/**
* Sets the logging configuration for the SDK.
* Override this method to customize logging behavior.
*
* @param builder The ClientOptions.Builder to configure
*/
protected void setLogging(ClientOptions.Builder builder) {
if (this.logging.isPresent()) {
builder.logging(this.logging.get());
}
}
/**
* 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 AsyncCloudPDFClient build() {
if (token == null) {
throw new RuntimeException("Please provide token");
}
validateConfiguration();
return new AsyncCloudPDFClient(buildClientOptions());
}
}