|
16 | 16 | import so.trophy.core.TrophyApiApiException; |
17 | 17 | import so.trophy.core.TrophyApiException; |
18 | 18 | import so.trophy.core.TrophyApiHttpResponse; |
| 19 | +import so.trophy.errors.BadRequestError; |
19 | 20 | import so.trophy.errors.NotFoundError; |
20 | 21 | import so.trophy.errors.UnauthorizedError; |
21 | 22 | import so.trophy.errors.UnprocessableEntityError; |
|
37 | 38 | import org.jetbrains.annotations.NotNull; |
38 | 39 | import so.trophy.resources.admin.metrics.requests.MetricsDeleteRequest; |
39 | 40 | import so.trophy.resources.admin.metrics.requests.MetricsListRequest; |
| 41 | +import so.trophy.types.BatchEventsResponse; |
| 42 | +import so.trophy.types.BatchMetricEvent; |
40 | 43 | import so.trophy.types.CreateMetricRequestItem; |
41 | 44 | import so.trophy.types.CreateMetricsResponse; |
42 | 45 | import so.trophy.types.CreatedMetric; |
@@ -462,4 +465,87 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { |
462 | 465 | }); |
463 | 466 | return future; |
464 | 467 | } |
465 | | - } |
| 468 | + |
| 469 | + /** |
| 470 | + * Submit up to 1,000 metric events for asynchronous processing. |
| 471 | + */ |
| 472 | + public CompletableFuture<TrophyApiHttpResponse<BatchEventsResponse>> batchEvents( |
| 473 | + List<BatchMetricEvent> request) { |
| 474 | + return batchEvents(request,null); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * Submit up to 1,000 metric events for asynchronous processing. |
| 479 | + */ |
| 480 | + public CompletableFuture<TrophyApiHttpResponse<BatchEventsResponse>> batchEvents( |
| 481 | + List<BatchMetricEvent> request, RequestOptions requestOptions) { |
| 482 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getAdminURL()).newBuilder() |
| 483 | + |
| 484 | + .addPathSegments("metrics/events");if (requestOptions != null) { |
| 485 | + requestOptions.getQueryParameters().forEach((_key, _value) -> { |
| 486 | + httpUrl.addQueryParameter(_key, _value); |
| 487 | + } ); |
| 488 | + } |
| 489 | + RequestBody body; |
| 490 | + try { |
| 491 | + body = RequestBody.create(ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); |
| 492 | + } |
| 493 | + catch(JsonProcessingException e) { |
| 494 | + throw new TrophyApiException("Failed to serialize request", e); |
| 495 | + } |
| 496 | + Request okhttpRequest = new Request.Builder() |
| 497 | + .url(httpUrl.build()) |
| 498 | + .method("POST", body) |
| 499 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 500 | + .addHeader("Content-Type", "application/json") |
| 501 | + .addHeader("Accept", "application/json") |
| 502 | + .build(); |
| 503 | + OkHttpClient client = clientOptions.httpClient(); |
| 504 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 505 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 506 | + } |
| 507 | + if (requestOptions != null && requestOptions.getMaxRetries().isPresent()) { |
| 508 | + okhttpRequest = okhttpRequest.newBuilder().tag(RetryInterceptor.MaxRetriesOverride.class, new RetryInterceptor.MaxRetriesOverride(requestOptions.getMaxRetries().get())).build(); |
| 509 | + } |
| 510 | + CompletableFuture<TrophyApiHttpResponse<BatchEventsResponse>> future = new CompletableFuture<>(); |
| 511 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 512 | + @Override |
| 513 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 514 | + try (ResponseBody responseBody = response.body()) { |
| 515 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 516 | + if (response.isSuccessful()) { |
| 517 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchEventsResponse.class), response)); |
| 518 | + return; |
| 519 | + } |
| 520 | + try { |
| 521 | + switch (response.code()) { |
| 522 | + case 400:future.completeExceptionally(new BadRequestError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 523 | + return; |
| 524 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 525 | + return; |
| 526 | + case 404:future.completeExceptionally(new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 527 | + return; |
| 528 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 529 | + return; |
| 530 | + } |
| 531 | + } |
| 532 | + catch (JsonProcessingException ignored) { |
| 533 | + // unable to map error response, throwing generic error |
| 534 | + } |
| 535 | + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); |
| 536 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), errorBody, response)); |
| 537 | + return; |
| 538 | + } |
| 539 | + catch (IOException e) { |
| 540 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 541 | + } |
| 542 | + } |
| 543 | + |
| 544 | + @Override |
| 545 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 546 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 547 | + } |
| 548 | + }); |
| 549 | + return future; |
| 550 | + } |
| 551 | + } |
0 commit comments