forked from dapi-co/dapi-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.java
More file actions
300 lines (232 loc) · 14 KB
/
Copy pathData.java
File metadata and controls
300 lines (232 loc) · 14 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
package co.dapi;
import co.dapi.response.GetAccountsResponse;
import co.dapi.response.GetBalanceResponse;
import co.dapi.response.GetIdentityResponse;
import co.dapi.types.UserInput;
import co.dapi.response.GetTransactionsResponse;
import co.dapi.response.GetCategorizedTransactionsResponse;
import co.dapi.response.GetEnrichedTransactionsResponse;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
public class Data {
private final Config config;
public Data(Config config) {
this.config = config;
}
GetIdentityResponse getIdentity(String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetIdentityRequest body = new GetIdentityRequest(this.config.getAppSecret(), userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, GetIdentityRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
GetIdentityResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetIdentityResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetIdentityResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
GetAccountsResponse getAccounts(String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetAccountsRequest body = new GetAccountsRequest(this.config.getAppSecret(), userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, GetAccountsRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
GetAccountsResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetAccountsResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetAccountsResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
GetBalanceResponse getBalance(String accountID, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetBalanceRequest body = new GetBalanceRequest(accountID, this.config.getAppSecret(), userSecret,
operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, GetBalanceRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
GetBalanceResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetBalanceResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetBalanceResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
GetTransactionsResponse getTransactions(String accountID, LocalDate fromDate, LocalDate toDate, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetTransactionsRequest body = new GetTransactionsRequest(accountID, fromDate, toDate, this.config.getAppSecret(),
userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, GetTransactionsRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
GetTransactionsResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetTransactionsResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetTransactionsResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
GetCategorizedTransactionsResponse getCategorizedTransactions(String accountID, LocalDate fromDate, LocalDate toDate, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetCategorizedTransactionsRequest body = new GetCategorizedTransactionsRequest(accountID, fromDate, toDate, this.config.getAppSecret(),
userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, GetCategorizedTransactionsRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
GetCategorizedTransactionsResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetCategorizedTransactionsResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetCategorizedTransactionsResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
GetEnrichedTransactionsResponse getEnrichedTransactions(String accountID, LocalDate fromDate, LocalDate toDate, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetEnrichedTransactionsRequest body = new GetEnrichedTransactionsRequest(accountID, fromDate, toDate, this.config.getAppSecret(),
userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, GetEnrichedTransactionsRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
GetEnrichedTransactionsResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetEnrichedTransactionsResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetEnrichedTransactionsResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
private static class GetIdentityRequest extends DapiRequest.BaseRequest {
final private String action = "/data/identity/get";
public GetIdentityRequest(String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
}
}
private static class GetAccountsRequest extends DapiRequest.BaseRequest {
final private String action = "/data/accounts/get";
public GetAccountsRequest(String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
}
}
private static class GetBalanceRequest extends DapiRequest.BaseRequest {
private final String action = "/data/balance/get";
private final String accountID;
public GetBalanceRequest(String accountID, String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.accountID = accountID;
}
}
private static class GetTransactionsRequest extends DapiRequest.BaseRequest {
private final String action = "/data/transactions/get";
private final String accountID;
private final String fromDate;
private final String toDate;
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public GetTransactionsRequest(String accountID, LocalDate fromDate, LocalDate toDate, String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.accountID = accountID;
this.fromDate = dateFormatter.format(fromDate);
this.toDate = dateFormatter.format(toDate);
}
}
private static class GetCategorizedTransactionsRequest extends DapiRequest.BaseRequest {
private final String action = "/data/categorizedTransactions/get";
private final String accountID;
private final String fromDate;
private final String toDate;
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public GetCategorizedTransactionsRequest(String accountID, LocalDate fromDate, LocalDate toDate, String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.accountID = accountID;
this.fromDate = dateFormatter.format(fromDate);
this.toDate = dateFormatter.format(toDate);
}
}
private static class GetEnrichedTransactionsRequest extends DapiRequest.BaseRequest {
private final String action = "/data/enrichedTransactions/get";
private final String accountID;
private final String fromDate;
private final String toDate;
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public GetEnrichedTransactionsRequest(String accountID, LocalDate fromDate, LocalDate toDate, String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.accountID = accountID;
this.fromDate = dateFormatter.format(fromDate);
this.toDate = dateFormatter.format(toDate);
}
}
}