-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from tubbynl/develop-non-breaking-refactor
UserContext building without static references. (#93)
- 1.26.11.52
- 1.26.11.46
- 1.26.11.42
- 1.26.9.70
- 1.26.9.63
- 1.26.8.44
- 1.26.8.14
- 1.26.5.30
- 1.26.0.38
- 1.25.17.65
- 1.25.17.47
- 1.25.17.21
- 1.25.16.53
- 1.25.15.29
- 1.25.15.19
- 1.25.15.9
- 1.25.14.41
- 1.25.13.24
- 1.25.11.28
- 1.25.10.11
- 1.25.8.46
- 1.25.6.13
- 1.25.5.47
- 1.25.4.31
- 1.25.0.3
- 1.24.23.8
- 1.24.20.53
- 1.24.20.34
- 1.24.20.11
- 1.24.19.19
- 1.24.18.46
- 1.24.16.39
- 1.24.11.32
- 1.24.10.26
- 1.24.9.37
- 1.24.9.23
- 1.24.8.28
- 1.24.3.72
- 1.24.3.42
- 1.24.3.14
- 1.24.2.37
- 1.24.1.10
- 1.24.0.33
- 1.23.21.89
- 1.23.21.70
- 1.23.21.51
- 1.23.20.25
- 1.23.20.10
- 1.23.19.58
- 1.23.18.37
- 1.23.16.37
- 1.23.13.29
- 1.23.10.62
- 1.23.10.43
- 1.23.8.74
- 1.23.5.42
- 1.23.4.29
- 1.23.3.22
- 1.23.2.15
- 1.23.2.5
- 1.23.0.56
- 1.23.0.20
- 1.22.17.81
- 1.22.17.56
- 1.22.17.50
- 1.22.17.22
- 1.22.17.6
- 1.22.15.24
- 1.22.13.30
- 1.22.10.14
- 1.22.8.20
- 1.22.7.54
- 1.22.7.28
- 1.22.5.46
- 1.22.3.32
- 1.22.2.34
- 1.22.1.83
- 1.21.21.47
- 1.21.21.41
- 1.21.21.2
- 1.21.19.20
- 1.21.18.24
- 1.21.17.56
- 1.21.16.24
- 1.21.16.1
- 1.21.12.6
- 1.21.11.19
- 1.21.10.1
- 1.21.9.33
- 1.21.7.10
- 1.21.6.4
- 1.21.5.20
- 1.21.4.17
- 1.21.3.35
- 1.21.1.32
- 1.21.0.54
- 1.20.22.74
- 1.20.22.64
- 1.20.21.28
- 1.20.21.10
- 1.20.20.12
- 1.20.13.19
- 1.20.10.25
- 1.20.6.17
- 1.19.11.48
- 1.19.9.10
- 1.19.1.3
- 1.18.19.21
- 1.18.16.30
- 1.18.16.16
- 1.18.15.48
- 1.18.13.65
- 1.18.13.60
- 1.18.12.52
- 1.18.12.49
- 1.14.18.1
- 1.14.18
- 1.14.1
- 1.14.0
- 1.13.1
- 1.13.0
- 1.12.1
- 1.12.0
- 1.10.16
- 1.10.1
- 1.10.0
- 1.1.0
- 1.0.1
- 1.0.0
Showing
4 changed files
with
118 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/bunq/sdk/model/core/UserContextHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.bunq.sdk.model.core; | ||
|
||
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; | ||
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank; | ||
import com.bunq.sdk.model.generated.endpoint.User; | ||
|
||
import java.util.List; | ||
|
||
public class UserContextHelper extends BunqModel { | ||
|
||
/** | ||
* Error constants. | ||
*/ | ||
private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK = | ||
"No active monetary account found."; | ||
|
||
/** | ||
* Endpoint constants. | ||
*/ | ||
private static final String ENDPOINT_USER = "user"; | ||
private static final String ENDPOINT_MONETARY_ACCOUNT_BANK = "user/%s/monetary-account-bank"; | ||
|
||
/** | ||
* The index of the first item in an array. | ||
*/ | ||
private static final Integer INDEX_FIRST = 0; | ||
|
||
/** | ||
* Status constants. | ||
*/ | ||
private static final String STATUS_ACTIVE = "ACTIVE"; | ||
|
||
private final ApiClient apiClient; | ||
|
||
public UserContextHelper(ApiContext apiContext) { | ||
this.apiClient = new ApiClient(apiContext); | ||
} | ||
|
||
private BunqResponseRaw getRawResponse(String url) { | ||
return this.apiClient.get(url, null, null); | ||
} | ||
|
||
public User getFirstUser() { | ||
BunqResponseRaw responseRaw = getRawResponse(ENDPOINT_USER); | ||
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw); | ||
|
||
return response.getValue().get(INDEX_FIRST); | ||
} | ||
|
||
public MonetaryAccountBank getFirstActiveMonetaryAccountBankByUserId(Integer userId) { | ||
BunqResponseRaw responseRaw = getRawResponse( | ||
String.format(ENDPOINT_MONETARY_ACCOUNT_BANK, userId) | ||
); | ||
String wrapper = MonetaryAccountBank.class.getSimpleName(); | ||
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList( | ||
MonetaryAccountBank.class, | ||
responseRaw, | ||
wrapper | ||
); | ||
|
||
for (MonetaryAccountBank monetaryAccountBank : response.getValue()) { | ||
if (STATUS_ACTIVE.equals(monetaryAccountBank.getStatus())) { | ||
return monetaryAccountBank; | ||
} | ||
} | ||
|
||
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK); | ||
} | ||
|
||
@Override | ||
public boolean isAllFieldNull() { | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.bunq.sdk.context; | ||
|
||
import com.bunq.sdk.BunqSdkTestBase; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class UserContextTest extends BunqSdkTestBase { | ||
|
||
@Test | ||
public void buildUserContext() { | ||
ApiContext context = getApiContext(); | ||
|
||
UserContext userContext = new UserContext(context); | ||
|
||
assertNotNull(userContext.getUserId()); | ||
assertNotNull(userContext.getMainMonetaryAccountId()); | ||
} | ||
|
||
} |