From 676959502092cc5610004229528b36a90c77631f Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Fri, 20 Oct 2017 10:05:22 +0200 Subject: [PATCH] Added isSessionActive method and fixed client #33. --- BunqSdk/Context/ApiContext.cs | 18 +++++++++++++----- BunqSdk/Http/ApiClient.cs | 24 ++++++++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/BunqSdk/Context/ApiContext.cs b/BunqSdk/Context/ApiContext.cs index cc1f679..2edda96 100644 --- a/BunqSdk/Context/ApiContext.cs +++ b/BunqSdk/Context/ApiContext.cs @@ -176,8 +176,19 @@ private void DeleteSession() /// public void EnsureSessionActive() { - if (SessionContext == null) return; + if (!IsSessionActive()) + { + ResetSession(); + } + } + public bool IsSessionActive() + { + if (SessionContext == null) + { + return false; + } + var timeToExpiry = SessionContext.ExpiryTime.Subtract(DateTime.Now); var timeToExpiryMinimum = new TimeSpan( TIME_UNIT_COUNT_NONE, @@ -185,10 +196,7 @@ public void EnsureSessionActive() TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS ); - if (timeToExpiry < timeToExpiryMinimum) - { - ResetSession(); - } + return timeToExpiry > timeToExpiryMinimum; } /// diff --git a/BunqSdk/Http/ApiClient.cs b/BunqSdk/Http/ApiClient.cs index fcd185a..63502e4 100644 --- a/BunqSdk/Http/ApiClient.cs +++ b/BunqSdk/Http/ApiClient.cs @@ -16,6 +16,18 @@ namespace Bunq.Sdk.Http { public class ApiClient { + + private const string DEVICE_SERVER_URL = "device-server"; + private const string INSTALLATION_URL = "installation"; + private const string SESSION_SERVER_URL = "session-server"; + + private static readonly string[] URIS_NOT_REQUIRING_ACTIVE_SESSION = new string[] + { + DEVICE_SERVER_URL, + INSTALLATION_URL, + SESSION_SERVER_URL + }; + /// /// Header constants. /// @@ -115,7 +127,7 @@ private BunqResponseRaw SendRequest(HttpMethod method, string uriRelative, byte[ { var requestMessage = CreateHttpRequestMessage(method, uriRelative, uriParams, requestBodyBytes); - return SendRequest(requestMessage, customHeaders); + return SendRequest(requestMessage, customHeaders, uriRelative); } private BunqResponseRaw SendRequest(HttpMethod method, string uriRelative, @@ -123,13 +135,17 @@ private BunqResponseRaw SendRequest(HttpMethod method, string uriRelative, { var requestMessage = CreateHttpRequestMessage(method, uriRelative, uriParams); - return SendRequest(requestMessage, customHeaders); + return SendRequest(requestMessage, customHeaders, uriRelative); } private BunqResponseRaw SendRequest(HttpRequestMessage requestMessage, - IDictionary customHeaders) + IDictionary customHeaders, string uriRelative) { - apiContext.EnsureSessionActive(); + if (!URIS_NOT_REQUIRING_ACTIVE_SESSION.Contains(uriRelative)) + { + apiContext.EnsureSessionActive(); + } + SetDefaultHeaders(requestMessage); SetHeaders(requestMessage, customHeaders); SetSessionHeaders(requestMessage);