Skip to content

Commit 6768af1

Browse files
authored
Merge pull request #5 from chaixdev/improve-java-sdk-consistency
Improve java sdk consistency
2 parents f5cb45c + 960d63d commit 6768af1

20 files changed

Lines changed: 303 additions & 287 deletions

.gitignore

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1+
2+
target/
3+
.mvn/
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
117
.idea
2-
/.vscode/
3-
target
18+
*.iws
419
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Twikey has integrations with a lot of accounting and CRM packages. It is the fir
1313
European level for Direct Debit and can work directly with all major Belgian and Dutch Banks. However you can use the
1414
payment options of your favorite PSP to allow other customers to pay as well.
1515

16+
1617
## Requirements ##
1718

1819
[![Maven Build](https://github.com/twikey/twikey-api-java/actions/workflows/maven-build.yml/badge.svg)](https://github.com/twikey/twikey-api-java/actions/workflows/maven-build.yml)
@@ -88,7 +89,6 @@ JSONObject invite = api.document().create(ct,customer,extraParams);
8889
_After creation, the link available in invite['url'] can be used to redirect the customer into the signing flow or even
8990
send him a link through any other mechanism. Ideally you store the mandatenumber for future usage (eg. sending transactions)._
9091

91-
9292
### Feed
9393

9494
Once signed, a webhook is sent (see below) after which you can fetch the detail through the document feed, which you can actually

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.twikey</groupId>
55
<artifactId>twikey-api-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.2.0</version>
7+
<version>0.2.1-SNAPSHOT</version>
88
<name>Twikey Api</name>
99
<url>http://maven.apache.org</url>
1010
<description>Official wrapper around the Twikey.com rest api</description>

src/main/java/com/twikey/DocumentGateway.java

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public DocumentResponse.MandateCreationResponse create(DocumentRequests.InviteRe
7171
} */
7272
return DocumentResponse.MandateCreationResponse.fromJson(response.body());
7373
} else {
74-
String apiError = response.headers()
75-
.firstValue("apierror")
76-
.orElse("Twikey status=" + response.statusCode());
77-
throw new TwikeyClient.UserException(apiError);
74+
throw new TwikeyClient.UserException(apiError(response));
7875
}
7976
}
8077

@@ -114,10 +111,7 @@ public DocumentResponse.MandateCreationResponse sign(DocumentRequests.SignReques
114111
} */
115112
return DocumentResponse.MandateCreationResponse.fromJson(response.body());
116113
} else {
117-
String apiError = response.headers()
118-
.firstValue("apierror")
119-
.orElse("Twikey status=" + response.statusCode());
120-
throw new TwikeyClient.UserException(apiError);
114+
throw new TwikeyClient.UserException(apiError(response));
121115
}
122116
}
123117

@@ -149,10 +143,7 @@ public void action(DocumentRequests.MandateActionRequest action) throws IOExcept
149143
.build();
150144
HttpResponse<String> response = twikeyClient.send(request, HttpResponse.BodyHandlers.ofString());
151145
if (response.statusCode() != 204) {
152-
String apiError = response.headers()
153-
.firstValue("apierror")
154-
.orElse("Twikey status=" + response.statusCode());
155-
throw new TwikeyClient.UserException(apiError);
146+
throw new TwikeyClient.UserException(apiError(response));
156147
}
157148
}
158149

@@ -195,9 +186,7 @@ public List<DocumentResponse.Document> query(DocumentRequests.MandateQuery actio
195186
if (response.statusCode() == 200) {
196187
return DocumentResponse.Document.fromQuery(json);
197188
} else {
198-
String apiError = response.headers()
199-
.firstValue("apierror")
200-
.orElse("Twikey status=" + response.statusCode());
189+
String apiError = apiError(response);
201190
if ("err_not_found".equals(apiError)) {
202191
return List.of();
203192
}
@@ -238,10 +227,7 @@ public void cancel(String mandateNumber, String reason, boolean notify) throws I
238227
.build();
239228
HttpResponse<String> response = twikeyClient.send(request, HttpResponse.BodyHandlers.ofString());
240229
if (response.statusCode() != 200) {
241-
String apiError = response.headers()
242-
.firstValue("apierror")
243-
.orElse("Twikey status=" + response.statusCode());
244-
throw new TwikeyClient.UserException(apiError);
230+
throw new TwikeyClient.UserException(apiError(response));
245231
}
246232
}
247233

@@ -275,10 +261,7 @@ public DocumentResponse.Document fetch(DocumentRequests.MandateDetailRequest fet
275261
return DocumentResponse.Document.fromJson(json, null);
276262
}
277263
} else {
278-
String apiError = response.headers()
279-
.firstValue("apierror")
280-
.orElse("Twikey status=" + response.statusCode());
281-
throw new TwikeyClient.UserException(apiError);
264+
throw new TwikeyClient.UserException(apiError(response));
282265
}
283266
}
284267

@@ -306,10 +289,7 @@ public void update(DocumentRequests.UpdateMandateRequest update) throws IOExcept
306289
.build();
307290
HttpResponse<String> response = twikeyClient.send(request, HttpResponse.BodyHandlers.ofString());
308291
if (response.statusCode() != 204) {
309-
String apiError = response.headers()
310-
.firstValue("apierror")
311-
.orElse("Twikey status=" + response.statusCode());
312-
throw new TwikeyClient.UserException(apiError);
292+
throw new TwikeyClient.UserException(apiError(response));
313293
}
314294
}
315295

@@ -336,10 +316,7 @@ public DocumentResponse.CustomerAccessResponse customerAccess(String mandateNumb
336316
JSONObject json = new JSONObject(new JSONTokener(response.body()));
337317
return DocumentResponse.CustomerAccessResponse.fromJson(json);
338318
} else {
339-
String apiError = response.headers()
340-
.firstValue("apierror")
341-
.orElse("Twikey status=" + response.statusCode());
342-
throw new TwikeyClient.UserException(apiError);
319+
throw new TwikeyClient.UserException(apiError(response));
343320
}
344321
}
345322

@@ -370,10 +347,7 @@ public DocumentResponse.PdfResponse retrievePdf(String mandateNumber) throws IOE
370347
}
371348
return new DocumentResponse.PdfResponse(response.body(), filename);
372349
} else {
373-
String apiError = response.headers()
374-
.firstValue("apierror")
375-
.orElse("Twikey status=" + response.statusCode());
376-
throw new TwikeyClient.UserException(apiError);
350+
throw new TwikeyClient.UserException(apiError(response));
377351
}
378352
}
379353

@@ -400,10 +374,7 @@ public void uploadPdf(DocumentRequests.UploadPdfRequest pdfRequest) throws IOExc
400374
.build();
401375
HttpResponse<String> response = twikeyClient.send(request, HttpResponse.BodyHandlers.ofString());
402376
if (response.statusCode() != 200) {
403-
String apiError = response.headers()
404-
.firstValue("apierror")
405-
.orElse("Twikey status=" + response.statusCode());
406-
throw new TwikeyClient.UserException(apiError);
377+
throw new TwikeyClient.UserException(apiError(response));
407378
}
408379
}
409380

@@ -445,12 +416,10 @@ public void feed(DocumentCallback mandateCallback) throws IOException, TwikeyCli
445416
}
446417
}
447418
}
448-
} else {
449-
String apiError = response.headers()
450-
.firstValue("apierror")
451-
.orElse("Twikey status=" + response.statusCode());
452-
throw new TwikeyClient.UserException(apiError);
453-
}
419+
} else {
420+
throw new TwikeyClient.UserException(apiError(response));
421+
}
422+
454423
} while (!isEmpty);
455424
}
456425
}

src/main/java/com/twikey/InvoiceGateway.java

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public InvoiceResponse.Invoice create(InvoiceRequests.CreateInvoiceRequest creat
5656
JSONObject json = new JSONObject(new JSONTokener(response.body()));
5757
return InvoiceResponse.Invoice.fromJson(json);
5858
} else {
59-
String apiError = response.headers()
60-
.firstValue("ApiError")
61-
.orElse("Twikey status=" + response.statusCode());
62-
throw new TwikeyClient.UserException(apiError);
59+
throw new TwikeyClient.UserException(apiError(response));
6360
}
6461
}
6562

@@ -93,10 +90,7 @@ public InvoiceResponse.Invoice update(InvoiceRequests.UpdateInvoiceRequest updat
9390
JSONObject json = new JSONObject(new JSONTokener(response.body()));
9491
return InvoiceResponse.Invoice.fromJson(json);
9592
} else {
96-
String apiError = response.headers()
97-
.firstValue("ApiError")
98-
.orElse("Twikey status=" + response.statusCode());
99-
throw new TwikeyClient.UserException(apiError);
93+
throw new TwikeyClient.UserException(apiError(response));
10094
}
10195
}
10296

@@ -123,10 +117,7 @@ public void delete(String delete) throws IOException, TwikeyClient.UserException
123117
HttpResponse<String> response = twikeyClient.send(request, HttpResponse.BodyHandlers.ofString());
124118

125119
if (response.statusCode() != 204) {
126-
String apiError = response.headers()
127-
.firstValue("ApiError")
128-
.orElse("Twikey status=" + response.statusCode());
129-
throw new TwikeyClient.UserException(apiError);
120+
throw new TwikeyClient.UserException(apiError(response));
130121
}
131122
}
132123

@@ -161,10 +152,7 @@ public InvoiceResponse.Invoice details(InvoiceRequests.InvoiceDetailRequest deta
161152
JSONObject json = new JSONObject(new JSONTokener(response.body()));
162153
return InvoiceResponse.Invoice.fromJson(json);
163154
} else {
164-
String apiError = response.headers()
165-
.firstValue("ApiError")
166-
.orElse("Twikey status=" + response.statusCode());
167-
throw new TwikeyClient.UserException(apiError);
155+
throw new TwikeyClient.UserException(apiError(response));
168156
}
169157
}
170158

@@ -193,10 +181,7 @@ public void action(InvoiceRequests.InvoiceActionRequest action) throws IOExcepti
193181

194182
HttpResponse<String> response = twikeyClient.send(request, HttpResponse.BodyHandlers.ofString());
195183
if (response.statusCode() != 204) {
196-
String apiError = response.headers()
197-
.firstValue("ApiError")
198-
.orElse("Twikey status=" + response.statusCode());
199-
throw new TwikeyClient.UserException(apiError);
184+
throw new TwikeyClient.UserException(apiError(response));
200185
}
201186
}
202187

@@ -231,10 +216,7 @@ public InvoiceResponse.Invoice uploadUbl(InvoiceRequests.UblUploadRequest Ubl) t
231216
JSONObject json = new JSONObject(new JSONTokener(response.body()));
232217
return InvoiceResponse.Invoice.fromJson(json);
233218
} else {
234-
String apiError = response.headers()
235-
.firstValue("ApiError")
236-
.orElse("Twikey status=" + response.statusCode());
237-
throw new TwikeyClient.UserException(apiError);
219+
throw new TwikeyClient.UserException(apiError(response));
238220
}
239221
}
240222

@@ -265,10 +247,7 @@ public String createBatch(InvoiceRequests.BulkInvoiceRequest batch) throws IOExc
265247
if (response.statusCode() == 200) {
266248
return new JSONObject(new JSONTokener(response.body())).getString("batchId");
267249
} else {
268-
String apiError = response.headers()
269-
.firstValue("ApiError")
270-
.orElse("Twikey status=" + response.statusCode());
271-
throw new TwikeyClient.UserException(apiError);
250+
throw new TwikeyClient.UserException(apiError(response));
272251
}
273252
}
274253

@@ -302,10 +281,7 @@ public InvoiceResponse.BulkInvoiceDetail batchDetails(String batchId) throws IOE
302281
} else if (response.statusCode() == 409) {
303282
return InvoiceResponse.BulkInvoiceDetail.PENDING;
304283
} else {
305-
String apiError = response.headers()
306-
.firstValue("ApiError")
307-
.orElse("Twikey status=" + response.statusCode());
308-
throw new TwikeyClient.UserException(apiError);
284+
throw new TwikeyClient.UserException(apiError(response));
309285
}
310286
}
311287

@@ -346,10 +322,7 @@ public void feed(InvoiceCallback invoiceCallback, String... sideloads) throws IO
346322
throw new RuntimeException(e);
347323
}
348324
} else {
349-
String apiError = response.headers()
350-
.firstValue("ApiError")
351-
.orElse("Twikey status=" + response.statusCode());
352-
throw new TwikeyClient.UserException(apiError);
325+
throw new TwikeyClient.UserException(apiError(response));
353326
}
354327
} while (!isEmpty);
355328
}

0 commit comments

Comments
 (0)