Skip to content

Commit

Permalink
Merge pull request #32 from bunq/feature/isExpired#31
Browse files Browse the repository at this point in the history
Added isSessionExpired() method #31.
  • Loading branch information
dnl-blkv committed Oct 24, 2017
2 parents 226bf71 + ffea2c4 commit 233f052
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 13 additions & 1 deletion bunq/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class ApiClient(object):
:type _api_context: bunq.sdk.context.ApiContext
"""

# Endpoints not requiring active session for the request to succeed.
_URL_DEVICE_SERVER = 'device-server'
_URI_INSTALLATION = 'installation'
_URI_SESSION_SERVER = 'session-server'
_URIS_NOT_REQUIRING_ACTIVE_SESSION = [
_URI_INSTALLATION,
_URI_SESSION_SERVER,
_URL_DEVICE_SERVER,
]

# HTTPS type of proxy, the only used at bunq
_FIELD_PROXY_HTTPS = 'https'

Expand Down Expand Up @@ -93,7 +103,9 @@ def _request(self, method, uri_relative, request_bytes, params,

uri_relative_with_params = self._append_params_to_uri(uri_relative,
params)
self._api_context.ensure_session_active()
if uri_relative not in self._URIS_NOT_REQUIRING_ACTIVE_SESSION:
self._api_context.ensure_session_active()

all_headers = self._get_all_headers(
method,
uri_relative_with_params,
Expand Down
17 changes: 14 additions & 3 deletions bunq/sdk/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,28 @@ def _get_session_timeout_seconds(cls, session_server):
return session_server.user_person.session_timeout

def ensure_session_active(self):
"""
Resets the session if it has expired.
"""

if not self.is_session_active():
self.reset_session()

def is_session_active(self):
"""
:rtype: bool
"""

if self.session_context is None:
return
return False

time_now = datetime.datetime.now()
time_to_expiry = self.session_context.expiry_time - time_now
time_to_expiry_minimum = datetime.timedelta(
seconds=self._TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS
)

if time_to_expiry < time_to_expiry_minimum:
self.reset_session()
return time_to_expiry > time_to_expiry_minimum

def reset_session(self):
"""
Expand Down

0 comments on commit 233f052

Please sign in to comment.