Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor change in API to enable non-static usage #98

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/java/com/bunq/sdk/context/BunqContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public final class BunqContext {

public static void loadApiContext(ApiContext apiContext) {
BunqContext.apiContext = apiContext;
BunqContext.userContext = new UserContext(apiContext.getSessionContext().getUserId());
BunqContext.userContext.initMainMonetaryAccount();
BunqContext.userContext = new UserContext(apiContext);
}

public static ApiContext getApiContext() {
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/com/bunq/sdk/context/SessionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Context of your current bunq Public API session.
*/
class SessionContext implements java.io.Serializable {
public class SessionContext implements java.io.Serializable {

/**
* Error constants.
Expand Down Expand Up @@ -53,14 +53,10 @@ class SessionContext implements java.io.Serializable {
SessionContext(SessionServer sessionServer) {
this.token = sessionServer.getSessionToken().getToken();
this.expiryTime = calculateExpiryTime(sessionServer);
this.userId = getUserId(sessionServer.getReferencedObject());
}

private Integer getUserId(BunqModel user) {
if (user instanceof UserPerson) {
return ((UserPerson) user).getId();
} else if (user instanceof UserCompany) {
return ((UserCompany) user).getId();
if (sessionServer.getUserPerson()!=null) {
this.userId = sessionServer.getUserPerson().getId();
} else if (sessionServer.getUserCompany()!=null) {
this.userId = sessionServer.getUserCompany().getId();
} else {
throw new BunqException(ERROR_UNEXPECTED_USER_TYPE);
}
Expand All @@ -70,23 +66,16 @@ private static Date calculateExpiryTime(SessionServer sessionServer) {
Date expiryTime = new Date();
long sessionTimeoutMilliseconds = getSessionTimeout(sessionServer) * MILLISECONDS_IN_SECOND;
expiryTime.setTime(expiryTime.getTime() + sessionTimeoutMilliseconds);

return expiryTime;
}

private static int getSessionTimeout(SessionServer sessionServer) {
UserCompany userCompany = sessionServer.getUserCompany();

if (userCompany != null) {
return userCompany.getSessionTimeout();
if (sessionServer.getUserCompany() != null) {
return sessionServer.getUserCompany().getSessionTimeout();
}

UserPerson userPerson = sessionServer.getUserPerson();

if (userPerson != null) {
return userPerson.getSessionTimeout();
if (sessionServer.getUserPerson() != null) {
return sessionServer.getUserPerson().getSessionTimeout();
}

return SESSION_TIMEOUT_DEFAULT;
}

Expand All @@ -101,5 +90,4 @@ Date getExpiryTime() {
public Integer getUserId() {
return userId;
}

}
67 changes: 25 additions & 42 deletions src/main/java/com/bunq/sdk/context/UserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,39 @@ public class UserContext {
*/
private static final String ERROR_UNEXPECTED_USER_INSTANCE = "\"%s\" is unexpected user instance.";
private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = "No active monetary account found.";
private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set.";

private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE";

private UserCompany userCompany;
private UserPerson userPerson;
private MonetaryAccountBank primaryMonetaryAccountBank;
private Integer userId;

public UserContext(Integer userId) {
BunqModel userObject = User.list().getValue().get(0).getReferencedObject();

this.setUser(userObject);
this.userId = userId;
}

private void setUser(BunqModel user) {
if (user instanceof UserPerson) {
this.userPerson = (UserPerson) user;
} else if (user instanceof UserCompany) {
this.userCompany = (UserCompany) user;
private final ApiContext apiContext;
private final UserCompany userCompany;
private final UserPerson userPerson;
private final MonetaryAccountBank primaryMonetaryAccountBank;

public UserContext(ApiContext apiContext) {
this.apiContext = apiContext;
User user = User.getFirst(this.apiContext);
if (user.getUserPerson()!=null) {
this.userPerson = user.getUserPerson();
this.userCompany = null;
} else if (user.getUserCompany()!=null) {
this.userCompany = user.getUserCompany();
this.userPerson = null;
} else {
throw new BunqException(ERROR_UNEXPECTED_USER_INSTANCE);
}
}

public void initMainMonetaryAccount() {
List<MonetaryAccountBank> allMonetaryAccount = MonetaryAccountBank.list().getValue();

for (MonetaryAccountBank monetaryAccountBank : allMonetaryAccount) {
if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) {
this.primaryMonetaryAccountBank = monetaryAccountBank;

return;
}
MonetaryAccountBank monetaryAccountBank = MonetaryAccountBank.getFirstActive(this.apiContext);
if(monetaryAccountBank!=null) {
this.primaryMonetaryAccountBank = monetaryAccountBank;
}
else {
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
}
}

throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
public ApiContext getApiContext() {
return apiContext;
}

public Integer getUserId() {
return this.userId;
return this.apiContext.getSessionContext().getUserId();
}

public boolean isOnlyUserPersonSet() {
Expand All @@ -68,16 +59,8 @@ public boolean isOnlyUserCompanySet() {
return this.userPerson == null && this.userCompany != null;
}

public boolean isBothUserTypeSet() {
return this.userPerson != null && this.userCompany != null;
}

public Integer getMainMonetaryAccountId() {
if (this.primaryMonetaryAccountBank == null) {
throw new BunqException(ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET);
} else {
return this.primaryMonetaryAccountBank.getId();
}
return this.primaryMonetaryAccountBank.getId();
}

public UserPerson getUserPerson() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bunq.sdk.model.generated.endpoint;

import com.bunq.sdk.context.ApiContext;
import com.bunq.sdk.exception.BunqException;
import com.bunq.sdk.http.ApiClient;
import com.bunq.sdk.http.BunqResponse;
import com.bunq.sdk.http.BunqResponseRaw;
Expand Down Expand Up @@ -48,6 +50,7 @@ public class MonetaryAccountBank extends BunqModel {
public static final String FIELD_REASON_DESCRIPTION = "reason_description";
public static final String FIELD_NOTIFICATION_FILTERS = "notification_filters";
public static final String FIELD_SETTING = "setting";
private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE";

/**
* Object type.
Expand Down Expand Up @@ -404,16 +407,36 @@ public static BunqResponse<Integer> update(Integer monetaryAccountBankId, String
return update(monetaryAccountBankId, description, dailyLimit, avatarUuid, status, subStatus, reason, reasonDescription, notificationFilters, setting, null);
}

/**
* Gets the first active MonetaryAccountBank or null
*/
public static MonetaryAccountBank getFirstActive(ApiContext apiContext) {
List<MonetaryAccountBank> allMonetaryAccount = list(apiContext, null,null).getValue();
for (MonetaryAccountBank monetaryAccountBank : allMonetaryAccount) {
if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) {
return monetaryAccountBank;
}
}
return null;
}

/**
* Gets a listing of all MonetaryAccountBanks of a given user.
*/
public static BunqResponse<List<MonetaryAccountBank>> list(Map<String, String> params, Map<String, String> customHeaders) {
ApiClient apiClient = new ApiClient(getApiContext());
private static BunqResponse<List<MonetaryAccountBank>> list(ApiContext apiContext, Map<String, String> params, Map<String, String> customHeaders) {
ApiClient apiClient = new ApiClient(apiContext);
BunqResponseRaw responseRaw = apiClient.get(String.format(ENDPOINT_URL_LISTING, determineUserId()), params, customHeaders);

return fromJsonList(MonetaryAccountBank.class, responseRaw, OBJECT_TYPE_GET);
}

/**
* Gets a listing of all MonetaryAccountBanks of a given user.
*/
public static BunqResponse<List<MonetaryAccountBank>> list(Map<String, String> params, Map<String, String> customHeaders) {
return list(getApiContext(),params,customHeaders);
}

public static BunqResponse<List<MonetaryAccountBank>> list() {
return list(null, null);
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/bunq/sdk/model/generated/endpoint/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bunq.sdk.model.generated.endpoint;

import com.bunq.sdk.context.ApiContext;
import com.bunq.sdk.exception.BunqException;
import com.bunq.sdk.http.ApiClient;
import com.bunq.sdk.http.BunqResponse;
Expand Down Expand Up @@ -71,11 +72,22 @@ public static BunqResponse<User> get(Map<String, String> params) {
return get(params, null);
}

/**
* Get first of all available users.
*/
public static User getFirst(ApiContext apiContext) {
return list(apiContext,null,null).getValue().get(0);
}

/**
* Get a collection of all available users.
*/
public static BunqResponse<List<User>> list(Map<String, String> params, Map<String, String> customHeaders) {
ApiClient apiClient = new ApiClient(getApiContext());
return list(getApiContext(),params,customHeaders);
}

private static BunqResponse<List<User>> list(ApiContext apiContext,Map<String, String> params, Map<String, String> customHeaders) {
ApiClient apiClient = new ApiClient(apiContext);
BunqResponseRaw responseRaw = apiClient.get(ENDPOINT_URL_LISTING, params, customHeaders);

return fromJsonList(User.class, responseRaw);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/bunq/sdk/context/UserContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bunq.sdk.context;

import com.bunq.sdk.BunqSdkTestBase;
import org.junit.Test;

import static org.junit.Assert.*;

public class UserContextTest extends BunqSdkTestBase {

@Test
public void testConstruct() {
ApiContext context = getApiContext();

UserContext sut = new UserContext(context);

assertNotNull(sut.getUserId());
assertNotNull(sut.getMainMonetaryAccountId());
}
}