|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | +package com.pipedream.api.resources.usage; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 7 | +import com.pipedream.api.core.BaseClientApiException; |
| 8 | +import com.pipedream.api.core.BaseClientException; |
| 9 | +import com.pipedream.api.core.BaseClientHttpResponse; |
| 10 | +import com.pipedream.api.core.ClientOptions; |
| 11 | +import com.pipedream.api.core.ObjectMappers; |
| 12 | +import com.pipedream.api.core.QueryStringMapper; |
| 13 | +import com.pipedream.api.core.RequestOptions; |
| 14 | +import com.pipedream.api.errors.BadRequestError; |
| 15 | +import com.pipedream.api.errors.TooManyRequestsError; |
| 16 | +import com.pipedream.api.resources.usage.requests.UsageListRequest; |
| 17 | +import com.pipedream.api.types.ConnectUsageResponse; |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import okhttp3.Call; |
| 21 | +import okhttp3.Callback; |
| 22 | +import okhttp3.Headers; |
| 23 | +import okhttp3.HttpUrl; |
| 24 | +import okhttp3.OkHttpClient; |
| 25 | +import okhttp3.Request; |
| 26 | +import okhttp3.Response; |
| 27 | +import okhttp3.ResponseBody; |
| 28 | +import org.jetbrains.annotations.NotNull; |
| 29 | + |
| 30 | +public class AsyncRawUsageClient { |
| 31 | + protected final ClientOptions clientOptions; |
| 32 | + |
| 33 | + public AsyncRawUsageClient(ClientOptions clientOptions) { |
| 34 | + this.clientOptions = clientOptions; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Retrieve Connect usage records for a time window |
| 39 | + */ |
| 40 | + public CompletableFuture<BaseClientHttpResponse<ConnectUsageResponse>> list(UsageListRequest request) { |
| 41 | + return list(request, null); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Retrieve Connect usage records for a time window |
| 46 | + */ |
| 47 | + public CompletableFuture<BaseClientHttpResponse<ConnectUsageResponse>> list( |
| 48 | + UsageListRequest request, RequestOptions requestOptions) { |
| 49 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 50 | + .newBuilder() |
| 51 | + .addPathSegments("v1/connect/usage"); |
| 52 | + QueryStringMapper.addQueryParameter(httpUrl, "start_ts", request.getStartTs(), false); |
| 53 | + QueryStringMapper.addQueryParameter(httpUrl, "end_ts", request.getEndTs(), false); |
| 54 | + Request.Builder _requestBuilder = new Request.Builder() |
| 55 | + .url(httpUrl.build()) |
| 56 | + .method("GET", null) |
| 57 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 58 | + .addHeader("Accept", "application/json"); |
| 59 | + Request okhttpRequest = _requestBuilder.build(); |
| 60 | + OkHttpClient client = clientOptions.httpClient(); |
| 61 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 62 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 63 | + } |
| 64 | + CompletableFuture<BaseClientHttpResponse<ConnectUsageResponse>> future = new CompletableFuture<>(); |
| 65 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 66 | + @Override |
| 67 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 68 | + try (ResponseBody responseBody = response.body()) { |
| 69 | + if (response.isSuccessful()) { |
| 70 | + future.complete(new BaseClientHttpResponse<>( |
| 71 | + ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ConnectUsageResponse.class), |
| 72 | + response)); |
| 73 | + return; |
| 74 | + } |
| 75 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 76 | + try { |
| 77 | + switch (response.code()) { |
| 78 | + case 400: |
| 79 | + future.completeExceptionally(new BadRequestError( |
| 80 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 81 | + response)); |
| 82 | + return; |
| 83 | + case 429: |
| 84 | + future.completeExceptionally(new TooManyRequestsError( |
| 85 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 86 | + response)); |
| 87 | + return; |
| 88 | + } |
| 89 | + } catch (JsonProcessingException ignored) { |
| 90 | + // unable to map error response, throwing generic error |
| 91 | + } |
| 92 | + future.completeExceptionally(new BaseClientApiException( |
| 93 | + "Error with status code " + response.code(), |
| 94 | + response.code(), |
| 95 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 96 | + response)); |
| 97 | + return; |
| 98 | + } catch (IOException e) { |
| 99 | + future.completeExceptionally(new BaseClientException("Network error executing HTTP request", e)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 105 | + future.completeExceptionally(new BaseClientException("Network error executing HTTP request", e)); |
| 106 | + } |
| 107 | + }); |
| 108 | + return future; |
| 109 | + } |
| 110 | +} |
0 commit comments