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 reload context bunq/sdk_csharp#72 #111

Merged
merged 6 commits into from
Jul 20, 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
30 changes: 29 additions & 1 deletion BunqSdk.Tests/Context/ApiContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Bunq.Sdk.Context;
using System;
using Bunq.Sdk.Context;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Bunq.Sdk.Tests.Context
Expand All @@ -14,6 +16,12 @@ public class ApiContextTest : BunqSdkTestBase, IClassFixture<ApiContextTest>
/// </summary>
private const string ContextFilenameTest = "context-save-restore-test.conf";

/// <summary>
/// Field constatns.
/// </summary>
private const string FieldSessionContext = "session_context";
private const string FieldExpiryTime = "expiry_time";

private static ApiContext apiContext;

public ApiContextTest()
Expand Down Expand Up @@ -45,5 +53,25 @@ public void TestApiContextSaveRestore()

Assert.Equal(apiContextJson, apiContextRestored.ToJson());
}

[Fact]
public void TestAutoApiContextReLoad()
{
var contextJson = JObject.Parse(apiContext.ToJson());
var expiredTime = DateTime.Now.Subtract(TimeSpan.FromDays(20));
contextJson.SelectToken(FieldSessionContext)[FieldExpiryTime] = expiredTime.ToString();

var expiredApiContext = ApiContext.FromJson(contextJson.ToString());

Assert.NotEqual(apiContext, expiredApiContext);

BunqContext.UpdateApiContext(expiredApiContext);

Assert.Equal(expiredApiContext, BunqContext.ApiContext);

BunqContext.UserContext.RefreshUserContext();

Assert.True(BunqContext.ApiContext.IsSessionActive());
}
}
}
11 changes: 8 additions & 3 deletions BunqSdk/Context/ApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,17 @@ private void DeleteSession()
/// <summary>
/// Check if current time is too close to the saved session expiry time and reset session if needed.
/// </summary>
public void EnsureSessionActive()
public bool EnsureSessionActive()
{
if (!IsSessionActive())
if (IsSessionActive())
{
ResetSession();
return false;
}

ResetSession();

return true;

}

public bool IsSessionActive()
Expand Down
26 changes: 18 additions & 8 deletions BunqSdk/Context/BunqContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ public static class BunqContext
public static ApiContext ApiContext {
get
{
if (apiContext == null)
{
throw new BunqException(ErrorApicontextHasNotBeenLoaded);
}
else
{
return apiContext;
}
AssertApiContextIsLoaded();

return apiContext;
}
private set => apiContext = value ?? throw new ArgumentNullException(nameof(value));
}

private static void AssertApiContextIsLoaded()
{
if (apiContext == null)
{
throw new BunqException(ErrorApicontextHasNotBeenLoaded);
}
}

public static UserContext UserContext
{
get
Expand All @@ -47,5 +50,12 @@ public static void LoadApiContext(ApiContext apiContextToLoad)
UserContext = new UserContext(apiContextToLoad.SessionContext.UserId);
UserContext.InitPrimaryMonetaryAccount();
}

public static void UpdateApiContext(ApiContext apiContext)
{
AssertApiContextIsLoaded();

BunqContext.apiContext = apiContext;
}
}
}
4 changes: 2 additions & 2 deletions BunqSdk/Http/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ private BunqResponseRaw SendRequest(HttpMethod method, string uriRelative,
private BunqResponseRaw SendRequest(HttpRequestMessage requestMessage,
IDictionary<string, string> customHeaders, string uriRelative)
{
if (!URIS_NOT_REQUIRING_ACTIVE_SESSION.Contains(uriRelative))
if (!URIS_NOT_REQUIRING_ACTIVE_SESSION.Contains(uriRelative) && apiContext.EnsureSessionActive())
{
apiContext.EnsureSessionActive();
BunqContext.UpdateApiContext(apiContext);
}

SetDefaultHeaders(requestMessage);
Expand Down