Skip to content

Commit

Permalink
Merge pull request #189 from fronzbot/timeout
Browse files Browse the repository at this point in the history
Improved timeout, prevent blocking on startup when unable to login
  • Loading branch information
fronzbot committed Jun 17, 2019
2 parents 57882bb + 741cf48 commit 414b647
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
14 changes: 7 additions & 7 deletions blinkpy/blinkpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(self, username=None, password=None,
self.cameras = CaseInsensitiveDict({})
self.video_list = CaseInsensitiveDict({})
self._login_url = LOGIN_URL
self.login_urls = []
self.motion_interval = motion_interval
self.version = __version__
self.legacy = legacy_subdomain
Expand Down Expand Up @@ -131,9 +132,9 @@ def get_auth_token(self, is_retry=False):
if not isinstance(self._password, str):
raise BlinkAuthenticationException(ERROR.PASSWORD)

login_urls = [LOGIN_URL, OLD_LOGIN_URL, LOGIN_BACKUP_URL]
self.login_urls = [LOGIN_URL, OLD_LOGIN_URL, LOGIN_BACKUP_URL]

response = self.login_request(login_urls, is_retry=is_retry)
response = self.login_request(is_retry=is_retry)

if not response:
return False
Expand All @@ -148,10 +149,10 @@ def get_auth_token(self, is_retry=False):

return self._auth_header

def login_request(self, login_urls, is_retry=False):
def login_request(self, is_retry=False):
"""Make a login request."""
try:
login_url = login_urls.pop(0)
login_url = self.login_urls.pop(0)
except IndexError:
_LOGGER.error("Could not login to blink servers.")
return False
Expand All @@ -165,14 +166,13 @@ def login_request(self, login_urls, is_retry=False):
is_retry=is_retry)
try:
if response.status_code != 200:
response = self.login_request(login_urls)
response = self.login_request(is_retry=True)
response = response.json()
(self.region_id, self.region), = response['region'].items()

except AttributeError:
_LOGGER.error("Login API endpoint failed with response %s",
response,
exc_info=True)
response)
return False

except KeyError:
Expand Down
12 changes: 9 additions & 3 deletions blinkpy/helpers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
import time
from functools import wraps
from functools import partial, wraps
from requests import Request, Session, exceptions
from blinkpy.helpers.constants import BLINK_URL, TIMESTAMP_FORMAT
import blinkpy.helpers.errors as ERROR
Expand All @@ -28,8 +28,14 @@ def merge_dicts(dict_a, dict_b):


def create_session():
"""Create a session for blink communication."""
"""
Create a session for blink communication.
From @ericfrederich via
https://github.com/kennethreitz/requests/issues/2011
"""
sess = Session()
sess.get = partial(sess.get, timeout=5)
return sess


Expand Down Expand Up @@ -65,7 +71,7 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
prepped = req.prepare()

try:
response = blink.session.send(prepped, stream=stream, timeout=10)
response = blink.session.send(prepped, stream=stream)
if json_resp and 'code' in response.json():
if is_retry:
_LOGGER.error("Cannot obtain new token for server auth.")
Expand Down
6 changes: 4 additions & 2 deletions tests/test_blink_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def test_backup_url(self, req, mock_sess):
bad_req,
new_req
]
self.blink.login_request(['test1', 'test2', 'test3'])
self.blink.login_urls = ['test1', 'test2', 'test3']
self.blink.login_request()
# pylint: disable=protected-access
self.assertEqual(self.blink._login_url, 'test3')

Expand All @@ -78,7 +79,8 @@ def test_backup_url(self, req, mock_sess):
new_req,
bad_req
]
self.blink.login_request(['test1', 'test2', 'test3'])
self.blink.login_urls = ['test1', 'test2', 'test3']
self.blink.login_request()
# pylint: disable=protected-access
self.assertEqual(self.blink._login_url, 'test2')

Expand Down

0 comments on commit 414b647

Please sign in to comment.