-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathRawEventsClient.java
More file actions
141 lines (134 loc) · 6.38 KB
/
Copy pathRawEventsClient.java
File metadata and controls
141 lines (134 loc) · 6.38 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
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.auth0.client.mgmt;
import com.auth0.client.mgmt.core.ClientOptions;
import com.auth0.client.mgmt.core.ManagementApiException;
import com.auth0.client.mgmt.core.ManagementApiHttpResponse;
import com.auth0.client.mgmt.core.ManagementException;
import com.auth0.client.mgmt.core.ObjectMappers;
import com.auth0.client.mgmt.core.QueryStringMapper;
import com.auth0.client.mgmt.core.RequestOptions;
import com.auth0.client.mgmt.core.ResponseBodyReader;
import com.auth0.client.mgmt.core.Stream;
import com.auth0.client.mgmt.errors.BadRequestError;
import com.auth0.client.mgmt.errors.ForbiddenError;
import com.auth0.client.mgmt.errors.GoneError;
import com.auth0.client.mgmt.errors.NotFoundError;
import com.auth0.client.mgmt.errors.TooManyRequestsError;
import com.auth0.client.mgmt.errors.UnauthorizedError;
import com.auth0.client.mgmt.types.EventStreamSubscribeEventsResponseContent;
import com.auth0.client.mgmt.types.SubscribeEventsRequestParameters;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class RawEventsClient {
protected final ClientOptions clientOptions;
public RawEventsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* Subscribe to events via Server-Sent Events (SSE)
*/
public ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>> subscribe() {
return subscribe(SubscribeEventsRequestParameters.builder().build());
}
/**
* Subscribe to events via Server-Sent Events (SSE)
*/
public ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>> subscribe(
RequestOptions requestOptions) {
return subscribe(SubscribeEventsRequestParameters.builder().build(), requestOptions);
}
/**
* Subscribe to events via Server-Sent Events (SSE)
*/
public ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>> subscribe(
SubscribeEventsRequestParameters request) {
return subscribe(request, null);
}
/**
* Subscribe to events via Server-Sent Events (SSE)
*/
public ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseContent>> subscribe(
SubscribeEventsRequestParameters request, RequestOptions requestOptions) {
HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("events");
if (!request.getFrom().isAbsent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "from", request.getFrom().orElse(null), false);
}
if (!request.getFromTimestamp().isAbsent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "from_timestamp", request.getFromTimestamp().orElse(null), false);
}
if (request.getEventType().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "event_type", request.getEventType().get(), true);
}
if (requestOptions != null) {
requestOptions.getQueryParameters().forEach((_key, _value) -> {
httpUrl.addQueryParameter(_key, _value);
});
}
Request.Builder _requestBuilder = new Request.Builder()
.url(httpUrl.build())
.method("GET", null)
.headers(Headers.of(clientOptions.headers(requestOptions)));
Request okhttpRequest = _requestBuilder.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
client = client.newBuilder().callTimeout(0, TimeUnit.SECONDS).build();
try {
Response response = client.newCall(okhttpRequest).execute();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return new ManagementApiHttpResponse<>(
Stream.fromSseWithEventDiscrimination(
EventStreamSubscribeEventsResponseContent.class,
new ResponseBodyReader(response),
"type"),
response);
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
try {
switch (response.code()) {
case 400:
throw new BadRequestError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
case 401:
throw new UnauthorizedError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
case 403:
throw new ForbiddenError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
case 404:
throw new NotFoundError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
case 410:
throw new GoneError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
case 429:
throw new TooManyRequestsError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
}
} catch (JsonProcessingException ignored) {
// unable to map error response, throwing generic error
}
Object errorBody = ObjectMappers.parseErrorBody(responseBodyString);
throw new ManagementApiException(
"Error with status code " + response.code(), response.code(), errorBody, response);
} catch (IOException e) {
throw new ManagementException("Network error executing HTTP request", e);
}
}
}