-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFacturapiHttpClient.java
More file actions
410 lines (359 loc) · 13.2 KB
/
Copy pathFacturapiHttpClient.java
File metadata and controls
410 lines (359 loc) · 13.2 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package io.facturapi.http;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.facturapi.FacturapiException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public final class FacturapiHttpClient {
private static final MediaType JSON_MEDIA_TYPE = MediaType.get("application/json; charset=utf-8");
private final OkHttpClient httpClient;
private final ObjectMapper objectMapper;
private final String baseUrl;
private final String apiKey;
private final String userAgent;
public FacturapiHttpClient(FacturapiConfig config) {
this.httpClient = config.getHttpClient();
this.objectMapper = config.getObjectMapper();
this.baseUrl = config.getBaseUrl();
this.apiKey = config.getApiKey();
this.userAgent = config.getUserAgent();
}
public ObjectMapper getObjectMapper() {
return objectMapper;
}
public <T> T get(String path, Map<String, ?> queryParams, Class<T> responseType) {
return convert(requestJsonNode("GET", path, queryParams, null, null), responseType);
}
public <T> T get(String path, Map<String, ?> queryParams, TypeReference<T> typeReference) {
return convert(requestJsonNode("GET", path, queryParams, null, null), typeReference);
}
public <T> T post(String path, Object body, Map<String, ?> queryParams, Class<T> responseType) {
return convert(requestJsonNode("POST", path, queryParams, body, null), responseType);
}
public <T> T post(String path, Object body, Map<String, ?> queryParams, TypeReference<T> typeReference) {
return convert(requestJsonNode("POST", path, queryParams, body, null), typeReference);
}
public <T> T put(String path, Object body, Map<String, ?> queryParams, Class<T> responseType) {
return convert(requestJsonNode("PUT", path, queryParams, body, null), responseType);
}
public <T> T put(String path, Object body, Map<String, ?> queryParams, TypeReference<T> typeReference) {
return convert(requestJsonNode("PUT", path, queryParams, body, null), typeReference);
}
public <T> T delete(String path, Map<String, ?> queryParams, Class<T> responseType) {
return convert(requestJsonNode("DELETE", path, queryParams, null, null), responseType);
}
public <T> T delete(String path, Map<String, ?> queryParams, TypeReference<T> typeReference) {
return convert(requestJsonNode("DELETE", path, queryParams, null, null), typeReference);
}
public <T> T putMultipart(String path, MultipartBody multipartBody, Class<T> responseType) {
return convert(requestJsonNode("PUT", path, null, null, multipartBody), responseType);
}
public byte[] getBytes(String path) {
return requestBytes("GET", path, null);
}
public byte[] postBytes(String path, Object body) {
return requestBytes("POST", path, body);
}
public InputStream getStream(String path) {
return requestStream("GET", path, null);
}
public InputStream postStream(String path, Object body) {
return requestStream("POST", path, body);
}
private JsonNode requestJsonNode(
String method,
String path,
Map<String, ?> queryParams,
Object body,
MultipartBody multipartBody
) {
Request request = buildRequest(method, path, queryParams, body, multipartBody);
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw buildApiException(readBodyText(response), response);
}
ResponseBody responseBody = response.body();
if (responseBody == null) {
return objectMapper.nullNode();
}
byte[] responseBytes = responseBody.bytes();
if (responseBytes.length == 0) {
return objectMapper.nullNode();
}
String contentType = responseBody.contentType() == null ? "" : responseBody.contentType().toString();
if (!contentType.contains("application/json")) {
return objectMapper.valueToTree(new String(responseBytes, StandardCharsets.UTF_8));
}
return objectMapper.readTree(responseBytes);
} catch (IOException e) {
throw new FacturapiException("I/O error when calling Facturapi API", e);
}
}
private byte[] requestBytes(String method, String path, Object body) {
Request request = buildRequest(method, path, null, body, null);
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw buildApiException(readBodyText(response), response);
}
ResponseBody responseBody = response.body();
return responseBody == null ? new byte[0] : responseBody.bytes();
} catch (IOException e) {
throw new FacturapiException("I/O error when calling Facturapi API", e);
}
}
private InputStream requestStream(String method, String path, Object body) {
Request request = buildRequest(method, path, null, body, null);
try {
Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful()) {
try {
throw buildApiException(readBodyText(response), response);
} finally {
response.close();
}
}
ResponseBody responseBody = response.body();
if (responseBody == null) {
response.close();
return new ByteArrayInputStream(new byte[0]);
}
return new ResponseInputStream(response, responseBody);
} catch (IOException e) {
throw new FacturapiException("I/O error when calling Facturapi API", e);
}
}
private <T> T convert(JsonNode node, Class<T> type) {
if (type == Void.class) {
return null;
}
if (type == String.class) {
if (node == null || node.isNull()) {
return type.cast("");
}
if (node.isTextual()) {
return type.cast(node.asText());
}
if (node.isObject()) {
JsonNode value = firstDefined(node, "api_key", "key", "token", "value");
if (value != null && value.isTextual()) {
return type.cast(value.asText());
}
}
return type.cast(node.toString());
}
try {
return objectMapper.convertValue(node, type);
} catch (IllegalArgumentException e) {
throw new FacturapiException("Could not deserialize response to " + type.getSimpleName(), e);
}
}
private <T> T convert(JsonNode node, TypeReference<T> typeReference) {
try {
return objectMapper.convertValue(node, typeReference);
} catch (IllegalArgumentException e) {
throw new FacturapiException("Could not deserialize response", e);
}
}
private static JsonNode firstDefined(JsonNode node, String... keys) {
for (String key : keys) {
JsonNode value = node.get(key);
if (value != null && !value.isNull()) {
return value;
}
}
return null;
}
private FacturapiException buildApiException(String bodyText, Response response) {
int statusCode = response.code();
int resolvedStatus = statusCode;
String errorCode = null;
String errorPath = null;
String errorLocation = null;
JsonNode errors = null;
String message = "Request failed with status " + statusCode;
if (bodyText != null && !bodyText.isBlank()) {
try {
JsonNode root = objectMapper.readTree(bodyText);
if (root != null) {
JsonNode messageNode = firstDefined(root, "message", "error", "detail");
if (messageNode != null && messageNode.isTextual()) {
message = messageNode.asText();
} else if (root.isTextual()) {
message = root.asText();
} else {
message = bodyText;
}
JsonNode statusNode = firstDefined(root, "status");
if (statusNode != null) {
if (statusNode.isIntegralNumber()) {
resolvedStatus = statusNode.intValue();
} else if (statusNode.isNumber()) {
resolvedStatus = (int) Math.round(statusNode.asDouble());
} else if (statusNode.isTextual()) {
try {
resolvedStatus = Integer.parseInt(statusNode.asText());
} catch (NumberFormatException ignored) {
resolvedStatus = statusCode;
}
}
}
JsonNode codeNode = firstDefined(root, "code");
if (codeNode != null && (codeNode.isTextual() || codeNode.isNumber())) {
errorCode = codeNode.asText();
}
JsonNode pathNode = firstDefined(root, "path");
if (pathNode != null && pathNode.isTextual()) {
errorPath = pathNode.asText();
}
JsonNode locationNode = firstDefined(root, "location");
if (locationNode != null && locationNode.isTextual()) {
errorLocation = locationNode.asText();
}
JsonNode errorsNode = firstDefined(root, "errors");
if (errorsNode != null && errorsNode.isArray()) {
errors = errorsNode;
}
if (firstDefined(root, "message", "error", "detail") == null) {
message = bodyText;
}
}
} catch (Exception ignored) {
message = bodyText.isBlank() ? message : bodyText;
}
}
return new FacturapiException(
message,
resolvedStatus,
errorCode,
errorPath,
errorLocation,
errors,
response.header("x-facturapi-log-id"),
response.headers().toMultimap()
);
}
private static String readBodyText(Response response) throws IOException {
ResponseBody responseBody = response.body();
if (responseBody == null) {
return "";
}
return responseBody.string();
}
private Request buildRequest(
String method,
String path,
Map<String, ?> queryParams,
Object body,
MultipartBody multipartBody
) {
String url = baseUrl + normalizePath(path) + buildQuery(queryParams);
Request.Builder builder = new Request.Builder()
.url(url)
.header("Authorization", "Bearer " + apiKey)
.header("Accept", "application/json")
.header("User-Agent", userAgent);
RequestBody requestBody = null;
if (multipartBody != null) {
requestBody = multipartBody.getBody();
} else if (body != null) {
try {
byte[] json = objectMapper.writeValueAsBytes(body);
requestBody = RequestBody.create(json, JSON_MEDIA_TYPE);
} catch (IOException e) {
throw new UncheckedIOException("Could not serialize request body", e);
}
}
if (requestBody == null) {
if ("GET".equals(method) || "HEAD".equals(method)) {
builder.method(method, null);
} else {
builder.method(method, RequestBody.create(new byte[0], null));
}
} else {
builder.method(method, requestBody);
}
return builder.build();
}
private static String normalizePath(String path) {
if (path == null || path.isEmpty()) {
return "";
}
return path.startsWith("/") ? path : "/" + path;
}
private static String buildQuery(Map<String, ?> queryParams) {
if (queryParams == null || queryParams.isEmpty()) {
return "";
}
List<String> parts = new ArrayList<>();
for (Map.Entry<String, ?> entry : queryParams.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
continue;
}
if (value instanceof Iterable<?>) {
for (Object v : (Iterable<?>) value) {
appendQueryPart(parts, key, v);
}
} else if (value.getClass().isArray()) {
int length = java.lang.reflect.Array.getLength(value);
for (int i = 0; i < length; i++) {
appendQueryPart(parts, key, java.lang.reflect.Array.get(value, i));
}
} else {
appendQueryPart(parts, key, value);
}
}
return parts.isEmpty() ? "" : "?" + String.join("&", parts);
}
private static void appendQueryPart(List<String> parts, String key, Object value) {
if (value == null) {
return;
}
String encodedKey = URLEncoder.encode(Objects.toString(key), StandardCharsets.UTF_8);
String encodedValue = URLEncoder.encode(Objects.toString(value), StandardCharsets.UTF_8);
parts.add(encodedKey + "=" + encodedValue);
}
private static final class ResponseInputStream extends InputStream {
private final Response response;
private final ResponseBody responseBody;
private final InputStream delegate;
private ResponseInputStream(Response response, ResponseBody responseBody) {
this.response = response;
this.responseBody = responseBody;
this.delegate = responseBody.byteStream();
}
@Override
public int read() throws IOException {
return delegate.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return delegate.read(b, off, len);
}
@Override
public void close() throws IOException {
try {
delegate.close();
} finally {
responseBody.close();
response.close();
}
}
}
}