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

Added isSessionExpired() method #31. #32

Merged
merged 3 commits into from
Oct 24, 2017
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
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put these constants after the map (just like we do in PHP).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you're right! My apologies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python is not that magical ✨ 😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OGKevin declare time mixed with run time? Tell me it is not magical :P

_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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline after the if body! :)


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