Skip to content

Commit d91d7da

Browse files
committed
Merge branch 'release-0.10.0'
2 parents ed6142a + a41b11f commit d91d7da

File tree

99 files changed

+1454
-912
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1454
-912
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ gradle-app.setting
6868

6969
# bunq-specific
7070
bunq.conf
71-
tmp/attachment_out.jpg
71+
context-save-restore-test.conf
7272
/tmp
7373
config.properties

.idea/misc.xml

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
group 'com.bunq.sdk'
2-
version '0.9.1'
2+
version '0.10.0'
33

44
apply plugin: 'java'
5+
apply plugin: 'maven'
56
sourceCompatibility = 1.8
67

78
repositories {

src/main/java/com/bunq/sdk/context/ApiContext.java

+62-12
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
import com.bunq.sdk.exception.BunqException;
44
import com.bunq.sdk.json.BunqGsonBuilder;
5-
import com.bunq.sdk.model.DeviceServer;
65
import com.bunq.sdk.model.Installation;
76
import com.bunq.sdk.model.SessionServer;
7+
import com.bunq.sdk.model.generated.DeviceServer;
88
import com.bunq.sdk.model.generated.Session;
99
import com.bunq.sdk.security.SecurityUtils;
1010
import com.google.gson.Gson;
11-
import com.google.gson.JsonObject;
1211
import com.google.gson.annotations.Expose;
1312
import com.google.gson.annotations.SerializedName;
14-
import com.sun.istack.internal.Nullable;
1513
import java.io.File;
1614
import java.io.IOException;
1715
import java.net.URI;
1816
import java.security.KeyPair;
1917
import java.util.ArrayList;
2018
import java.util.Date;
19+
import java.util.HashMap;
2120
import java.util.List;
21+
import java.util.Map;
22+
import javax.annotation.Nullable;
2223
import org.apache.commons.io.FileUtils;
2324

2425
/**
@@ -81,6 +82,10 @@ public class ApiContext implements java.io.Serializable {
8182
@SerializedName("session_context")
8283
private SessionContext sessionContext;
8384

85+
@Expose
86+
@SerializedName("proxy")
87+
private String proxy;
88+
8489
/**
8590
* Create an empty API context.
8691
*/
@@ -90,19 +95,36 @@ private ApiContext(ApiEnvironmentType environmentType, String apiKey) {
9095
}
9196

9297
/**
93-
* Create and initialize an API Context with current IP as permitted.
98+
* Create and initialize an API Context with current IP as permitted and no proxy.
9499
*/
95100
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
96101
String deviceDescription) {
97102
return create(environmentType, apiKey, deviceDescription, new ArrayList<>());
98103
}
99104

100105
/**
101-
* Create and initialize an API Context.
106+
* Create and initialize an API Context with given permitted ips and no proxy.
102107
*/
103108
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
104109
String deviceDescription, List<String> permittedIps) {
110+
return create(environmentType, apiKey, deviceDescription, permittedIps, null);
111+
}
112+
113+
/**
114+
* Create and initialize an API Context with current IP as permitted and a proxy.
115+
*/
116+
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
117+
String deviceDescription, String proxy) {
118+
return create(environmentType, apiKey, deviceDescription, new ArrayList<>(), proxy);
119+
}
120+
121+
/**
122+
* Create and initialize an API Context.
123+
*/
124+
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
125+
String deviceDescription, List<String> permittedIps, String proxy) {
105126
ApiContext apiContext = new ApiContext(environmentType, apiKey);
127+
apiContext.proxy = proxy;
106128
apiContext.initialize(deviceDescription, permittedIps);
107129

108130
return apiContext;
@@ -122,14 +144,20 @@ public static ApiContext restore(String fileName) {
122144
try {
123145
File file = new File(fileName);
124146
String json = FileUtils.readFileToString(file, ENCODING_BUNQ_CONF);
125-
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
126147

127-
return gson.fromJson(jsonObject, ApiContext.class);
148+
return fromJson(json);
128149
} catch (IOException exception) {
129150
throw new BunqException(ERROR_COULD_NOT_RESTORE_API_CONTEXT, exception);
130151
}
131152
}
132153

154+
/**
155+
* Restores a context from a given JSON string.
156+
*/
157+
public static ApiContext fromJson(String json) {
158+
return gson.fromJson(json, ApiContext.class);
159+
}
160+
133161
private void initialize(String deviceDescription, List<String> permittedIps) {
134162
/* The calls below are order-sensitive: to initialize a Device Registration, we need an
135163
* Installation, and to initialize a Session we need a Device Registration. */
@@ -146,19 +174,31 @@ private void initializeInstallation() {
146174
Installation installation = Installation.create(
147175
this,
148176
SecurityUtils.getPublicKeyFormattedString(keyPairClient.getPublic())
149-
);
177+
).getValue();
150178
installationContext = new InstallationContext(installation, keyPairClient);
151179
}
152180

153181
private void initializeDeviceRegistration(String deviceDescription, List<String> permittedIps) {
154-
DeviceServer.create(this, deviceDescription, permittedIps);
182+
Map<String, Object> deviceServerRequestBody = generateDeviceServerRequestBodyBytes(
183+
deviceDescription, permittedIps);
184+
DeviceServer.create(this, deviceServerRequestBody);
185+
}
186+
187+
private Map<String, Object> generateDeviceServerRequestBodyBytes(String description,
188+
List<String> permittedIps) {
189+
HashMap<String, Object> deviceServerRequestBody = new HashMap<>();
190+
deviceServerRequestBody.put(DeviceServer.FIELD_DESCRIPTION, description);
191+
deviceServerRequestBody.put(DeviceServer.FIELD_SECRET, apiKey);
192+
deviceServerRequestBody.put(DeviceServer.FIELD_PERMITTED_IPS, permittedIps);
193+
194+
return deviceServerRequestBody;
155195
}
156196

157197
/**
158198
* Create a new session and its data in a SessionContext.
159199
*/
160200
private void initializeSession() {
161-
sessionContext = new SessionContext(SessionServer.create(this));
201+
sessionContext = new SessionContext(SessionServer.create(this).getValue());
162202
}
163203

164204
/**
@@ -219,13 +259,19 @@ public void save() {
219259
public void save(String fileName) {
220260
try {
221261
File file = new File(fileName);
222-
String json = gson.toJson(this);
223-
FileUtils.writeStringToFile(file, json, ENCODING_BUNQ_CONF);
262+
FileUtils.writeStringToFile(file, toJson(), ENCODING_BUNQ_CONF);
224263
} catch (IOException exception) {
225264
throw new BunqException(ERROR_COULD_NOT_SAVE_API_CONTEXT, exception);
226265
}
227266
}
228267

268+
/**
269+
* Serializes the context to JSON.
270+
*/
271+
public String toJson() {
272+
return gson.toJson(this);
273+
}
274+
229275
/**
230276
* @return The base URI of the current environment.
231277
*/
@@ -264,4 +310,8 @@ public SessionContext getSessionContext() {
264310
return sessionContext;
265311
}
266312

313+
public String getProxy() {
314+
return proxy;
315+
}
316+
267317
}

src/main/java/com/bunq/sdk/examples/AttachmentPublicExample.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.bunq.sdk.context.ApiContext;
44
import com.bunq.sdk.http.ApiClient;
5+
import com.bunq.sdk.http.BunqResponse;
56
import com.bunq.sdk.model.generated.AttachmentPublic;
67
import com.bunq.sdk.model.generated.AttachmentPublicContent;
78
import java.io.File;
@@ -32,9 +33,9 @@ public static void main(String[] args) throws IOException {
3233

3334
try {
3435
byte[] requestBytes = FileUtils.readFileToByteArray(new File(PATH_ATTACHMENT_IN));
35-
String uuid = AttachmentPublic.create(apiContext, requestBytes, customHeaders);
36-
byte[] responseBytes = AttachmentPublicContent.list(apiContext, uuid);
37-
FileUtils.writeByteArrayToFile(new File(PATH_ATTACHMENT_OUT), responseBytes);
36+
String uuid = AttachmentPublic.create(apiContext, requestBytes, customHeaders).getValue();
37+
BunqResponse<byte[]> response = AttachmentPublicContent.list(apiContext, uuid);
38+
FileUtils.writeByteArrayToFile(new File(PATH_ATTACHMENT_OUT), response.getValue());
3839
apiContext.save();
3940
} catch (IOException exception) {
4041
System.out.print(exception.getMessage());

src/main/java/com/bunq/sdk/examples/CardDebitExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void main(String[] args) throws IOException {
3737
pointer.setName(POINTER_NAME_TEST);
3838
requestMap.put(CardDebit.FIELD_ALIAS, pointer);
3939

40-
System.out.println(CardDebit.create(apiContext, requestMap, USER_ITEM_ID).getId());
40+
System.out.println(CardDebit.create(apiContext, requestMap, USER_ITEM_ID).getValue().getId());
4141
}
4242

4343
private static String generateRandomSecondLine() {

src/main/java/com/bunq/sdk/examples/CustomerStatementExportExample.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public static void main(String[] args) {
4343
Date dateEnd = new Date();
4444

4545
customerStatementMap.put(CustomerStatementExport.FIELD_DATE_END, formatDate.format(dateEnd));
46-
int userId = User.list(apiContext).get(INDEX_FIRST).getUserCompany().getId();
47-
int monetaryAccountId = MonetaryAccountBank.list(apiContext, userId).get(INDEX_FIRST).getId();
46+
int userId = User.list(apiContext).getValue().get(INDEX_FIRST).getUserCompany().getId();
47+
int monetaryAccountId = MonetaryAccountBank.list(apiContext, userId).getValue().get(INDEX_FIRST)
48+
.getId();
4849
int customerStatementId = CustomerStatementExport.create(apiContext, customerStatementMap,
49-
userId, monetaryAccountId);
50+
userId, monetaryAccountId).getValue();
5051

5152
CustomerStatementExport.delete(apiContext, userId, monetaryAccountId, customerStatementId);
5253

src/main/java/com/bunq/sdk/examples/MonetaryAccountExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void main(String[] args) {
2121
apiContext,
2222
USER_ITEM_ID,
2323
MONETARY_ACCOUNT_ITEM_ID
24-
);
24+
).getValue();
2525

2626
System.out.println(monetaryAccount.getMonetaryAccountBank());
2727
}

src/main/java/com/bunq/sdk/examples/PaymentBatchExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void main(String[] args) {
5252
paymentBatchMap,
5353
USER_ITEM_ID,
5454
MONETARY_ACCOUNT_ITEM_ID
55-
);
55+
).getValue();
5656

5757
System.out.println(
5858
PaymentBatch.get(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID, paymentBatchId)

src/main/java/com/bunq/sdk/examples/PaymentExample.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public static void main(String[] args) {
3232
generateExamplePaymentMap(),
3333
USER_ITEM_ID,
3434
MONETARY_ACCOUNT_ITEM_ID
35-
);
36-
Payment payment = Payment.get(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID, paymentId);
35+
).getValue();
36+
Payment payment = Payment.get(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID, paymentId)
37+
.getValue();
3738
System.out.println(payment);
3839
}
3940

src/main/java/com/bunq/sdk/examples/PaymentListExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class PaymentListExample {
1818
*/
1919
public static void main(String[] args) {
2020
ApiContext apiContext = ApiContext.restore(API_CONTEXT_FILE_PATH);
21-
List<Payment> payments = Payment.list(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID);
21+
List<Payment> payments = Payment.list(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID)
22+
.getValue();
2223
printPayments(payments);
2324
}
2425

src/main/java/com/bunq/sdk/examples/RequestExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void main(String[] args) {
4545
requestMap,
4646
USER_ITEM_ID,
4747
MONETARY_ACCOUNT_ITEM_ID
48-
);
48+
).getValue();
4949

5050
System.out.println(
5151
RequestInquiry.get(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID, requestId)

src/main/java/com/bunq/sdk/examples/UserListExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class UserListExample {
1616
*/
1717
public static void main(String[] args) {
1818
ApiContext apiContext = ApiContext.restore(API_CONTEXT_FILE_PATH);
19-
List<User> users = User.list(apiContext);
19+
List<User> users = User.list(apiContext).getValue();
2020
apiContext.save();
2121

2222
for (User oneUser : users) {

0 commit comments

Comments
 (0)