-
Notifications
You must be signed in to change notification settings - Fork 23
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
UserContext building without static references. (bunq/sdk_java#93) #99
Changes from 13 commits
714d486
20c7811
aa9aae0
20304a2
3f93d18
d77e23d
0bc1987
6768192
8491b9e
cdc15e7
0d7cae4
61dee09
be155d7
c416a86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing new line before return statement. |
||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a new line after the |
||
); | ||
|
||
for (MonetaryAccountBank monetaryAccountBank : response.getValue()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing new line before loop. |
||
if (STATUS_ACTIVE.equals(monetaryAccountBank.getStatus())) { | ||
return monetaryAccountBank; | ||
} | ||
} | ||
|
||
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK); | ||
} | ||
|
||
@Override | ||
public boolean isAllFieldNull() { | ||
return true; | ||
} | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing new line after |
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()); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing new line