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

Auto update bunq context bunq/sdk_java#60 #90

Merged
merged 4 commits into from
May 29, 2018
Merged
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
8 changes: 6 additions & 2 deletions src/main/java/com/bunq/sdk/context/ApiContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,14 @@ private void deleteSession() {
* Check if current time is too close to the saved session expiry time and reset session if
* needed.
*/
public void ensureSessionActive() {
if (!isSessionActive()){
public boolean ensureSessionActive() {
if (!isSessionActive()) {
resetSession();

return true;
}

return false;
}

public boolean isSessionActive() {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/bunq/sdk/context/BunqContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ public static void loadApiContext(ApiContext apiContext) {
}

public static ApiContext getApiContext() {
if (apiContext == null) {
if (BunqContext.apiContext == null) {
throw new BunqException(ERROR_API_CONTEXT_HAS_NOT_BEEN_SET);
}

return apiContext;
return BunqContext.apiContext;
}

public static UserContext getUserContext() {
if (userContext == null) {
if (BunqContext.userContext == null) {
throw new BunqException(ERROR_USER_CONTEXT_HAS_NOT_BEEN_SET);
}

return userContext;
return BunqContext.userContext;
}

public static void updateApiContext(ApiContext apiContext) {
if (BunqContext.apiContext == null) {
throw new BunqException(ERROR_API_CONTEXT_HAS_NOT_BEEN_SET);
}

BunqContext.apiContext = apiContext;
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/bunq/sdk/http/ApiClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bunq.sdk.http;

import com.bunq.sdk.context.ApiContext;
import com.bunq.sdk.context.BunqContext;
import com.bunq.sdk.context.InstallationContext;
import com.bunq.sdk.exception.ApiException;
import com.bunq.sdk.exception.BunqException;
Expand Down Expand Up @@ -242,8 +243,8 @@ private Response executeRequest(
Map<String, String> customHeaders,
String uri
) throws IOException {
if (!URIS_NOT_REQUIRING_ACTIVE_SESSION.contains(uri)) {
apiContext.ensureSessionActive();
if (!URIS_NOT_REQUIRING_ACTIVE_SESSION.contains(uri) && apiContext.ensureSessionActive()) {
BunqContext.updateApiContext(apiContext);
}

setHeaders(request, customHeaders);
Expand Down