forked from bunq/sdk_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build UserContext non-static (bunq#93)
UserContext building without static references, backwards compatible
- Loading branch information
Showing
5 changed files
with
85 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,5 +101,4 @@ Date getExpiryTime() { | |
public Integer getUserId() { | ||
return userId; | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
package com.bunq.sdk.model.core; | ||
|
||
import com.bunq.sdk.context.ApiContext; | ||
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 { | ||
private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE"; | ||
private static final int INDEX_FIRST = 0; | ||
|
||
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("user"); | ||
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw); | ||
return response.getValue().get(INDEX_FIRST); | ||
} | ||
|
||
public MonetaryAccountBank getFirstActiveMonetaryAccountBank(Integer userId) { | ||
BunqResponseRaw responseRaw = getRawResponse(String.format("user/%s/monetary-account-bank", userId)); | ||
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList(MonetaryAccountBank.class, responseRaw, MonetaryAccountBank.class.getSimpleName()); | ||
for (MonetaryAccountBank monetaryAccountBank : response.getValue()) { | ||
if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) { | ||
return monetaryAccountBank; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@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,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()); | ||
} | ||
} |