Skip to content

Commit

Permalink
Added isSessionActive method and fixed client #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Oct 20, 2017
1 parent 4814e7e commit 6769595
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
18 changes: 13 additions & 5 deletions BunqSdk/Context/ApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,27 @@ private void DeleteSession()
/// </summary>
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,
TIME_UNIT_COUNT_NONE,
TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS
);

if (timeToExpiry < timeToExpiryMinimum)
{
ResetSession();
}
return timeToExpiry > timeToExpiryMinimum;
}

/// <summary>
Expand Down
24 changes: 20 additions & 4 deletions BunqSdk/Http/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/// <summary>
/// Header constants.
/// </summary>
Expand Down Expand Up @@ -115,21 +127,25 @@ 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,
IDictionary<string, string> uriParams, IDictionary<string, string> customHeaders)
{
var requestMessage = CreateHttpRequestMessage(method, uriRelative, uriParams);

return SendRequest(requestMessage, customHeaders);
return SendRequest(requestMessage, customHeaders, uriRelative);
}

private BunqResponseRaw SendRequest(HttpRequestMessage requestMessage,
IDictionary<string, string> customHeaders)
IDictionary<string, string> customHeaders, string uriRelative)
{
apiContext.EnsureSessionActive();
if (!URIS_NOT_REQUIRING_ACTIVE_SESSION.Contains(uriRelative))
{
apiContext.EnsureSessionActive();
}

SetDefaultHeaders(requestMessage);
SetHeaders(requestMessage, customHeaders);
SetSessionHeaders(requestMessage);
Expand Down

0 comments on commit 6769595

Please sign in to comment.