-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountingAgedCreditorsOne.java
More file actions
401 lines (372 loc) · 19.7 KB
/
Copy pathAccountingAgedCreditorsOne.java
File metadata and controls
401 lines (372 loc) · 19.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
package com.apideck.unify.operations;
import static com.apideck.unify.operations.Operations.RequestOperation;
import static com.apideck.unify.utils.Exceptions.unchecked;
import static com.apideck.unify.operations.Operations.AsyncRequestOperation;
import com.apideck.unify.SDKConfiguration;
import com.apideck.unify.SecuritySource;
import com.apideck.unify.models.components.GetAgedCreditorsResponse;
import com.apideck.unify.models.components.UnexpectedErrorResponse;
import com.apideck.unify.models.errors.APIException;
import com.apideck.unify.models.errors.BadRequestResponse;
import com.apideck.unify.models.errors.NotFoundResponse;
import com.apideck.unify.models.errors.PaymentRequiredResponse;
import com.apideck.unify.models.errors.UnauthorizedResponse;
import com.apideck.unify.models.errors.UnprocessableResponse;
import com.apideck.unify.models.operations.AccountingAgedCreditorsOneRequest;
import com.apideck.unify.models.operations.AccountingAgedCreditorsOneResponse;
import com.apideck.unify.utils.AsyncRetries;
import com.apideck.unify.utils.BackoffStrategy;
import com.apideck.unify.utils.Blob;
import com.apideck.unify.utils.Globals;
import com.apideck.unify.utils.HTTPClient;
import com.apideck.unify.utils.HTTPRequest;
import com.apideck.unify.utils.Headers;
import com.apideck.unify.utils.Hook.AfterErrorContextImpl;
import com.apideck.unify.utils.Hook.AfterSuccessContextImpl;
import com.apideck.unify.utils.Hook.BeforeRequestContextImpl;
import com.apideck.unify.utils.NonRetryableException;
import com.apideck.unify.utils.Options;
import com.apideck.unify.utils.Retries;
import com.apideck.unify.utils.RetryConfig;
import com.apideck.unify.utils.Utils;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.InputStream;
import java.lang.Exception;
import java.lang.String;
import java.lang.Throwable;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
public class AccountingAgedCreditorsOne {
static abstract class Base {
final SDKConfiguration sdkConfiguration;
final String baseUrl;
final SecuritySource securitySource;
final List<String> retryStatusCodes;
final RetryConfig retryConfig;
final HTTPClient client;
final Headers _headers;
final Globals operationGlobals;
public Base(
SDKConfiguration sdkConfiguration, Optional<Options> options,
Headers _headers) {
this.sdkConfiguration = sdkConfiguration;
this._headers =_headers;
this.baseUrl = this.sdkConfiguration.serverUrl();
this.securitySource = this.sdkConfiguration.securitySource();
options
.ifPresent(o -> o.validate(List.of(Options.Option.RETRY_CONFIG)));
this.retryStatusCodes = List.of("408", "500", "502", "503", "504");
this.retryConfig = options
.flatMap(Options::retryConfig)
.or(sdkConfiguration::retryConfig)
.orElse(RetryConfig.builder().backoff(BackoffStrategy.builder()
.initialInterval(500, TimeUnit.MILLISECONDS)
.maxInterval(60000, TimeUnit.MILLISECONDS)
.baseFactor((double) (1.5))
.maxElapsedTime(900000, TimeUnit.MILLISECONDS)
.retryConnectError(true)
.build())
.build());
this.client = this.sdkConfiguration.client();
this.operationGlobals = new Globals();
this.sdkConfiguration.globals.getParam("header", "x-apideck-consumer-id")
.ifPresent(param -> operationGlobals.putParam("header", "x-apideck-consumer-id", param));
this.sdkConfiguration.globals.getParam("header", "x-apideck-app-id")
.ifPresent(param -> operationGlobals.putParam("header", "x-apideck-app-id", param));
}
Optional<SecuritySource> securitySource() {
return Optional.ofNullable(this.securitySource);
}
BeforeRequestContextImpl createBeforeRequestContext() {
return new BeforeRequestContextImpl(
this.sdkConfiguration,
this.baseUrl,
"accounting.agedCreditorsOne",
java.util.Optional.empty(),
securitySource());
}
AfterSuccessContextImpl createAfterSuccessContext() {
return new AfterSuccessContextImpl(
this.sdkConfiguration,
this.baseUrl,
"accounting.agedCreditorsOne",
java.util.Optional.empty(),
securitySource());
}
AfterErrorContextImpl createAfterErrorContext() {
return new AfterErrorContextImpl(
this.sdkConfiguration,
this.baseUrl,
"accounting.agedCreditorsOne",
java.util.Optional.empty(),
securitySource());
}
<T>HttpRequest buildRequest(T request, Class<T> klass) throws Exception {
String url = Utils.generateURL(
this.baseUrl,
"/accounting/aged-creditors");
HTTPRequest req = new HTTPRequest(url, "GET");
req.addHeader("Accept", "application/json")
.addHeader("user-agent", SDKConfiguration.USER_AGENT);
_headers.forEach((k, list) -> list.forEach(v -> req.addHeader(k, v)));
req.addQueryParams(Utils.getQueryParams(
klass,
request,
this.operationGlobals));
req.addHeaders(Utils.getHeadersFromMetadata(request, this.operationGlobals));
Utils.configureSecurity(req, this.sdkConfiguration.securitySource().getSecurity());
return req.build();
}
}
public static class Sync extends Base
implements RequestOperation<AccountingAgedCreditorsOneRequest, AccountingAgedCreditorsOneResponse> {
public Sync(
SDKConfiguration sdkConfiguration, Optional<Options> options,
Headers _headers) {
super(
sdkConfiguration, options,
_headers);
}
private HttpRequest onBuildRequest(AccountingAgedCreditorsOneRequest request) throws Exception {
HttpRequest req = buildRequest(request, AccountingAgedCreditorsOneRequest.class);
return sdkConfiguration.hooks().beforeRequest(createBeforeRequestContext(), req);
}
private HttpResponse<InputStream> onError(HttpResponse<InputStream> response, Exception error) throws Exception {
return sdkConfiguration.hooks().afterError(
createAfterErrorContext(),
Optional.ofNullable(response),
Optional.ofNullable(error));
}
private HttpResponse<InputStream> onSuccess(HttpResponse<InputStream> response) throws Exception {
return sdkConfiguration.hooks().afterSuccess(createAfterSuccessContext(), response);
}
@Override
public HttpResponse<InputStream> doRequest(AccountingAgedCreditorsOneRequest request) {
Retries retries = Retries.builder()
.action(() -> {
HttpRequest r;
try {
r = onBuildRequest(request);
} catch (Exception e) {
throw new NonRetryableException(e);
}
try {
HttpResponse<InputStream> httpRes = client.send(r);
if (Utils.statusCodeMatches(httpRes.statusCode(), "400", "401", "402", "404", "422", "4XX", "5XX")) {
return onError(httpRes, null);
}
return httpRes;
} catch (Exception e) {
return onError(null, e);
}
})
.retryConfig(retryConfig)
.statusCodes(retryStatusCodes)
.build();
return unchecked(() -> onSuccess(retries.run())).get();
}
@Override
public AccountingAgedCreditorsOneResponse handleResponse(HttpResponse<InputStream> response) {
String contentType = response
.headers()
.firstValue("Content-Type")
.orElse("application/octet-stream");
AccountingAgedCreditorsOneResponse.Builder resBuilder =
AccountingAgedCreditorsOneResponse
.builder()
.contentType(contentType)
.statusCode(response.statusCode())
.rawResponse(response);
AccountingAgedCreditorsOneResponse res = resBuilder.build();
if (Utils.statusCodeMatches(response.statusCode(), "200")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return res.withGetAgedCreditorsResponse(Utils.unmarshal(response, new TypeReference<GetAgedCreditorsResponse>() {}));
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "400")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
throw BadRequestResponse.from(response);
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "401")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
throw UnauthorizedResponse.from(response);
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "402")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
throw PaymentRequiredResponse.from(response);
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "404")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
throw NotFoundResponse.from(response);
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "422")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
throw UnprocessableResponse.from(response);
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "4XX")) {
// no content
throw APIException.from("API error occurred", response);
}
if (Utils.statusCodeMatches(response.statusCode(), "5XX")) {
// no content
throw APIException.from("API error occurred", response);
}
if (Utils.statusCodeMatches(response.statusCode(), "default")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return res.withUnexpectedErrorResponse(Utils.unmarshal(response, new TypeReference<UnexpectedErrorResponse>() {}));
} else {
throw APIException.from("Unexpected content-type received: " + contentType, response);
}
}
throw APIException.from("Unexpected status code received: " + response.statusCode(), response);
}
}
public static class Async extends Base
implements AsyncRequestOperation<AccountingAgedCreditorsOneRequest, com.apideck.unify.models.operations.async.AccountingAgedCreditorsOneResponse> {
private final ScheduledExecutorService retryScheduler;
public Async(
SDKConfiguration sdkConfiguration, Optional<Options> options,
ScheduledExecutorService retryScheduler, Headers _headers) {
super(
sdkConfiguration, options,
_headers);
this.retryScheduler = retryScheduler;
}
private CompletableFuture<HttpRequest> onBuildRequest(AccountingAgedCreditorsOneRequest request) throws Exception {
HttpRequest req = buildRequest(request, AccountingAgedCreditorsOneRequest.class);
return this.sdkConfiguration.asyncHooks().beforeRequest(createBeforeRequestContext(), req);
}
private CompletableFuture<HttpResponse<Blob>> onError(HttpResponse<Blob> response, Throwable error) {
return this.sdkConfiguration.asyncHooks().afterError(createAfterErrorContext(), response, error);
}
private CompletableFuture<HttpResponse<Blob>> onSuccess(HttpResponse<Blob> response) {
return this.sdkConfiguration.asyncHooks().afterSuccess(createAfterSuccessContext(), response);
}
@Override
public CompletableFuture<HttpResponse<Blob>> doRequest(AccountingAgedCreditorsOneRequest request) {
AsyncRetries retries = AsyncRetries.builder()
.retryConfig(retryConfig)
.statusCodes(retryStatusCodes)
.scheduler(retryScheduler)
.build();
return retries.retry(() -> unchecked(() -> onBuildRequest(request)).get().thenCompose(client::sendAsync)
.handle((resp, err) -> {
if (err != null) {
return onError(null, err);
}
if (Utils.statusCodeMatches(resp.statusCode(), "400", "401", "402", "404", "422", "4XX", "5XX")) {
return onError(resp, null);
}
return CompletableFuture.completedFuture(resp);
})
.thenCompose(Function.identity()))
.thenCompose(this::onSuccess);
}
@Override
public CompletableFuture<com.apideck.unify.models.operations.async.AccountingAgedCreditorsOneResponse> handleResponse(
HttpResponse<Blob> response) {
String contentType = response
.headers()
.firstValue("Content-Type")
.orElse("application/octet-stream");
com.apideck.unify.models.operations.async.AccountingAgedCreditorsOneResponse.Builder resBuilder =
com.apideck.unify.models.operations.async.AccountingAgedCreditorsOneResponse
.builder()
.contentType(contentType)
.statusCode(response.statusCode())
.rawResponse(response);
com.apideck.unify.models.operations.async.AccountingAgedCreditorsOneResponse res = resBuilder.build();
if (Utils.statusCodeMatches(response.statusCode(), "200")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return Utils.unmarshalAsync(response, new TypeReference<GetAgedCreditorsResponse>() {})
.thenApply(res::withGetAgedCreditorsResponse);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "400")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return BadRequestResponse.fromAsync(response)
.thenCompose(CompletableFuture::failedFuture);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "401")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return UnauthorizedResponse.fromAsync(response)
.thenCompose(CompletableFuture::failedFuture);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "402")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return PaymentRequiredResponse.fromAsync(response)
.thenCompose(CompletableFuture::failedFuture);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "404")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return NotFoundResponse.fromAsync(response)
.thenCompose(CompletableFuture::failedFuture);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "422")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return UnprocessableResponse.fromAsync(response)
.thenCompose(CompletableFuture::failedFuture);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
if (Utils.statusCodeMatches(response.statusCode(), "4XX")) {
// no content
return Utils.createAsyncApiError(response, "API error occurred");
}
if (Utils.statusCodeMatches(response.statusCode(), "5XX")) {
// no content
return Utils.createAsyncApiError(response, "API error occurred");
}
if (Utils.statusCodeMatches(response.statusCode(), "default")) {
if (Utils.contentTypeMatches(contentType, "application/json")) {
return Utils.unmarshalAsync(response, new TypeReference<UnexpectedErrorResponse>() {})
.thenApply(res::withUnexpectedErrorResponse);
} else {
return Utils.createAsyncApiError(response, "Unexpected content-type received: " + contentType);
}
}
return Utils.createAsyncApiError(response, "Unexpected status code received: " + response.statusCode());
}
}
}