|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | +package com.auth0.client.mgmt; |
| 5 | + |
| 6 | +import com.auth0.client.mgmt.core.ClientOptions; |
| 7 | +import com.auth0.client.mgmt.core.ManagementApiException; |
| 8 | +import com.auth0.client.mgmt.core.ManagementApiHttpResponse; |
| 9 | +import com.auth0.client.mgmt.core.ManagementException; |
| 10 | +import com.auth0.client.mgmt.core.ObjectMappers; |
| 11 | +import com.auth0.client.mgmt.core.QueryStringMapper; |
| 12 | +import com.auth0.client.mgmt.core.RequestOptions; |
| 13 | +import com.auth0.client.mgmt.core.ResponseBodyReader; |
| 14 | +import com.auth0.client.mgmt.core.Stream; |
| 15 | +import com.auth0.client.mgmt.errors.BadRequestError; |
| 16 | +import com.auth0.client.mgmt.errors.ForbiddenError; |
| 17 | +import com.auth0.client.mgmt.errors.GoneError; |
| 18 | +import com.auth0.client.mgmt.errors.NotFoundError; |
| 19 | +import com.auth0.client.mgmt.errors.TooManyRequestsError; |
| 20 | +import com.auth0.client.mgmt.errors.UnauthorizedError; |
| 21 | +import com.auth0.client.mgmt.types.EventStreamSubscribeEventsResponseContent; |
| 22 | +import com.auth0.client.mgmt.types.SubscribeEventsRequestParameters; |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import okhttp3.Call; |
| 28 | +import okhttp3.Callback; |
| 29 | +import okhttp3.Headers; |
| 30 | +import okhttp3.HttpUrl; |
| 31 | +import okhttp3.OkHttpClient; |
| 32 | +import okhttp3.Request; |
| 33 | +import okhttp3.Response; |
| 34 | +import okhttp3.ResponseBody; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | + |
| 37 | +public class AsyncRawEventsClient { |
| 38 | + protected final ClientOptions clientOptions; |
| 39 | + |
| 40 | + public AsyncRawEventsClient(ClientOptions clientOptions) { |
| 41 | + this.clientOptions = clientOptions; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Subscribe to events via Server-Sent Events (SSE) |
| 46 | + */ |
| 47 | + public CompletableFuture<ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>>> |
| 48 | + subscribe() { |
| 49 | + return subscribe(SubscribeEventsRequestParameters.builder().build()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Subscribe to events via Server-Sent Events (SSE) |
| 54 | + */ |
| 55 | + public CompletableFuture<ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>>> subscribe( |
| 56 | + RequestOptions requestOptions) { |
| 57 | + return subscribe(SubscribeEventsRequestParameters.builder().build(), requestOptions); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Subscribe to events via Server-Sent Events (SSE) |
| 62 | + */ |
| 63 | + public CompletableFuture<ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>>> subscribe( |
| 64 | + SubscribeEventsRequestParameters request) { |
| 65 | + return subscribe(request, null); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Subscribe to events via Server-Sent Events (SSE) |
| 70 | + */ |
| 71 | + public CompletableFuture<ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>>> subscribe( |
| 72 | + SubscribeEventsRequestParameters request, RequestOptions requestOptions) { |
| 73 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 74 | + .newBuilder() |
| 75 | + .addPathSegments("events"); |
| 76 | + if (!request.getFrom().isAbsent()) { |
| 77 | + QueryStringMapper.addQueryParameter( |
| 78 | + httpUrl, "from", request.getFrom().orElse(null), false); |
| 79 | + } |
| 80 | + if (!request.getFromTimestamp().isAbsent()) { |
| 81 | + QueryStringMapper.addQueryParameter( |
| 82 | + httpUrl, "from_timestamp", request.getFromTimestamp().orElse(null), false); |
| 83 | + } |
| 84 | + if (request.getEventType().isPresent()) { |
| 85 | + QueryStringMapper.addQueryParameter( |
| 86 | + httpUrl, "event_type", request.getEventType().get(), true); |
| 87 | + } |
| 88 | + if (requestOptions != null) { |
| 89 | + requestOptions.getQueryParameters().forEach((_key, _value) -> { |
| 90 | + httpUrl.addQueryParameter(_key, _value); |
| 91 | + }); |
| 92 | + } |
| 93 | + Request.Builder _requestBuilder = new Request.Builder() |
| 94 | + .url(httpUrl.build()) |
| 95 | + .method("GET", null) |
| 96 | + .headers(Headers.of(clientOptions.headers(requestOptions))); |
| 97 | + Request okhttpRequest = _requestBuilder.build(); |
| 98 | + OkHttpClient client = clientOptions.httpClient(); |
| 99 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 100 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 101 | + } |
| 102 | + client = client.newBuilder().callTimeout(0, TimeUnit.SECONDS).build(); |
| 103 | + CompletableFuture<ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>>> future = |
| 104 | + new CompletableFuture<>(); |
| 105 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 106 | + @Override |
| 107 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 108 | + try { |
| 109 | + ResponseBody responseBody = response.body(); |
| 110 | + if (response.isSuccessful()) { |
| 111 | + future.complete(new ManagementApiHttpResponse<>( |
| 112 | + Stream.fromSse( |
| 113 | + EventStreamSubscribeEventsResponseContent.class, |
| 114 | + new ResponseBodyReader(response)), |
| 115 | + response)); |
| 116 | + return; |
| 117 | + } |
| 118 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 119 | + try { |
| 120 | + switch (response.code()) { |
| 121 | + case 400: |
| 122 | + future.completeExceptionally(new BadRequestError( |
| 123 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 124 | + response)); |
| 125 | + return; |
| 126 | + case 401: |
| 127 | + future.completeExceptionally(new UnauthorizedError( |
| 128 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 129 | + response)); |
| 130 | + return; |
| 131 | + case 403: |
| 132 | + future.completeExceptionally(new ForbiddenError( |
| 133 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 134 | + response)); |
| 135 | + return; |
| 136 | + case 404: |
| 137 | + future.completeExceptionally(new NotFoundError( |
| 138 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 139 | + response)); |
| 140 | + return; |
| 141 | + case 410: |
| 142 | + future.completeExceptionally(new GoneError( |
| 143 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 144 | + response)); |
| 145 | + return; |
| 146 | + case 429: |
| 147 | + future.completeExceptionally(new TooManyRequestsError( |
| 148 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 149 | + response)); |
| 150 | + return; |
| 151 | + } |
| 152 | + } catch (JsonProcessingException ignored) { |
| 153 | + // unable to map error response, throwing generic error |
| 154 | + } |
| 155 | + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); |
| 156 | + future.completeExceptionally(new ManagementApiException( |
| 157 | + "Error with status code " + response.code(), response.code(), errorBody, response)); |
| 158 | + return; |
| 159 | + } catch (IOException e) { |
| 160 | + future.completeExceptionally(new ManagementException("Network error executing HTTP request", e)); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 166 | + future.completeExceptionally(new ManagementException("Network error executing HTTP request", e)); |
| 167 | + } |
| 168 | + }); |
| 169 | + return future; |
| 170 | + } |
| 171 | +} |
0 commit comments