-
Notifications
You must be signed in to change notification settings - Fork 51
/
ClarifaiResponse.java
246 lines (194 loc) · 6.78 KB
/
ClarifaiResponse.java
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
package clarifai2.api;
import clarifai2.Func0;
import clarifai2.Func1;
import clarifai2.dto.ClarifaiStatus;
import clarifai2.exception.ClarifaiException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.NoSuchElementException;
public abstract class ClarifaiResponse<T> {
@NotNull final ClarifaiStatus status;
final int httpCode;
protected ClarifaiResponse(@NotNull ClarifaiStatus status, int httpCode) {
this.status = status;
this.httpCode = httpCode;
}
/**
* @return The value returned by this HTTP request, or throws a {@link NoSuchElementException}
* if there is no value. You likely want to guard any invocations create this method with a check against
* {@link #isSuccessful()} to ensure that an exception won't be thrown.
* @throws NoSuchElementException if {@link #isSuccessful()} is false.
*/
@NotNull
public final T get() {
final T value = getOrNull();
if (value == null) {
throw new NoSuchElementException("This API call was not successful. Details about this error: " + getStatus());
}
return value;
}
/**
* @param ifResponseWasNull The default value to return if we got no value from the HTTP request
* @return The HTTP response value if it was valid, otherwise {@code ifResponseWasNull}
*/
@NotNull
public final T getOr(@NotNull T ifResponseWasNull) {
final T value = getOrNull();
return value == null ? ifResponseWasNull : value;
}
/**
* @param supplier the function to invoke and return a value from if the value from the HTTP request was null
* @return The HTTP response value if it was valid, otherwise the return value of {@code supplier.call()}
*/
@NotNull public final T getOr(@NotNull Func0<T> supplier) {
final T value = getOrNull();
return value == null ? supplier.call() : value;
}
/**
* @param mapper the mapping function to apply to the HTTP response (if it was non-null)
* @param <R> the type of the value returned from the mapper
* @return a {@link ClarifaiResponse} that represents the object represented by {@code this} object, but with
* the mapping function applied.
*/
@NotNull public abstract <R> ClarifaiResponse<R> map(@NotNull Func1<T, R> mapper);
/**
* @return The HTTP response value if it was present, otherwise {@code null}
*/
@Nullable
public abstract T getOrNull();
/**
* @param throwable The throwable to throw if there was no valid HTTP response
* @return The HTTP response value
* @throws ClarifaiException wraps your provided throwable, if no valid value returned via HTTP
*/
@NotNull
public T getOrThrow(@NotNull Throwable throwable) throws ClarifaiException {
final T value = getOrNull();
if (value == null) {
throw new ClarifaiException("No value present", throwable);
}
return value;
}
/**
* @return information about the status of this API call, or {@code null} if the API could not return a value
* (usually due to network connectivity errors).
*/
@NotNull public final ClarifaiStatus getStatus() {
return status;
}
/**
* @return true if you can receive a valid value from {@link #get()} or related methods.
*/
public abstract boolean isSuccessful();
/**
* @return true if there are valid and invalid values from {@link #get()} or related methods.
*/
public abstract boolean isMixedSuccess();
/**
* @return The HTTP response code
*/
public final int responseCode() {
return httpCode;
}
@NotNull public abstract String rawBody() throws UnsupportedOperationException;
public static final class Successful<T> extends ClarifaiResponse<T> {
@NotNull private final String rawBody;
@NotNull private final T deserialized;
public Successful(
@NotNull ClarifaiStatus status,
int httpCode,
@NotNull String rawBody,
@NotNull T deserialized
) {
super(status, httpCode);
this.deserialized = deserialized;
this.rawBody = rawBody;
}
@NotNull @Override public String rawBody() {
return rawBody;
}
@NotNull @Override public <R> ClarifaiResponse<R> map(@NotNull Func1<T, R> mapper) {
return new Successful<>(status, httpCode, rawBody, mapper.call(deserialized));
}
@Nullable @Override public T getOrNull() {
return deserialized;
}
@Override public boolean isSuccessful() {
return true;
}
@Override public boolean isMixedSuccess() {
return false;
}
}
public static final class MixedSuccess<T> extends ClarifaiResponse<T> {
@NotNull private final String rawBody;
@NotNull private final T deserialized;
public MixedSuccess(
@NotNull ClarifaiStatus status,
int httpCode,
@NotNull String rawBody,
@NotNull T deserialized
) {
super(status, httpCode);
this.deserialized = deserialized;
this.rawBody = rawBody;
}
@NotNull @Override public String rawBody() {
return rawBody;
}
@NotNull @Override public <R> ClarifaiResponse<R> map(@NotNull Func1<T, R> mapper) {
return new MixedSuccess<>(status, httpCode, rawBody, mapper.call(deserialized));
}
@Nullable @Override public T getOrNull() {
return deserialized;
}
@Override public boolean isSuccessful() {
return false;
}
@Override public boolean isMixedSuccess() {
return true;
}
}
public static final class Failure<T> extends ClarifaiResponse<T> {
@NotNull private final String rawBody;
public Failure(@NotNull ClarifaiStatus status, int httpCode, @NotNull String rawBody) {
super(status, httpCode);
this.rawBody = rawBody;
}
@NotNull @Override public String rawBody() {
return rawBody;
}
@NotNull @Override public <R> ClarifaiResponse<R> map(@NotNull Func1<T, R> mapper) {
return new Failure<>(status, httpCode, rawBody);
}
@Nullable @Override public T getOrNull() {
return null;
}
@Override public boolean isSuccessful() {
return false;
}
@Override public boolean isMixedSuccess() {
return false;
}
}
public static final class NetworkError<T> extends ClarifaiResponse<T> {
public NetworkError(@NotNull ClarifaiStatus status) {
super(status, 0);
}
@NotNull @Override public <R> ClarifaiResponse<R> map(@NotNull Func1<T, R> mapper) {
return new NetworkError<>(status);
}
@NotNull @Override public String rawBody() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Cannot get rawBody() on a ClarifaiResponse that ended in network error");
}
@Nullable @Override public T getOrNull() {
return null;
}
@Override public boolean isSuccessful() {
return false;
}
@Override public boolean isMixedSuccess() {
return false;
}
}
}