Fixed recurssion error by adding is_retry property to get_auth_token
This commit is contained in:
+11
-6
@@ -9,7 +9,7 @@ from blinkpy.helpers.constants import DEFAULT_URL
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def request_login(blink, url, username, password):
|
||||
def request_login(blink, url, username, password, is_retry=False):
|
||||
"""
|
||||
Login request.
|
||||
|
||||
@@ -17,6 +17,7 @@ def request_login(blink, url, username, password):
|
||||
:param url: Login url.
|
||||
:param username: Blink username.
|
||||
:param password: Blink password.
|
||||
:param is_retry: Is this part of a re-authorization attempt?
|
||||
"""
|
||||
headers = {
|
||||
'Host': DEFAULT_URL,
|
||||
@@ -28,7 +29,7 @@ def request_login(blink, url, username, password):
|
||||
'client_specifier': 'iPhone 9.2 | 2.2 | 222'
|
||||
})
|
||||
return http_req(blink, url=url, headers=headers, data=data,
|
||||
json_resp=False, reqtype='post')
|
||||
json_resp=False, reqtype='post', is_retry=is_retry)
|
||||
|
||||
|
||||
def request_networks(blink):
|
||||
@@ -226,28 +227,32 @@ def request_motion_detection_disable(blink, network, camera_id):
|
||||
return http_post(blink, url)
|
||||
|
||||
|
||||
def http_get(blink, url, stream=False, json=True):
|
||||
def http_get(blink, url, stream=False, json=True, is_retry=False):
|
||||
"""
|
||||
Perform an http get request.
|
||||
|
||||
:param url: URL to perform get request.
|
||||
:param stream: Stream response? True/FALSE
|
||||
:param json: Return json response? TRUE/False
|
||||
:param is_retry: Is this part of a re-auth attempt?
|
||||
"""
|
||||
if blink.auth_header is None:
|
||||
raise BlinkException(ERROR.AUTH_TOKEN)
|
||||
_LOGGER.debug("Making GET request to %s", url)
|
||||
return http_req(blink, url=url, headers=blink.auth_header,
|
||||
reqtype='get', stream=stream, json_resp=json)
|
||||
reqtype='get', stream=stream, json_resp=json,
|
||||
is_retry=is_retry)
|
||||
|
||||
|
||||
def http_post(blink, url):
|
||||
def http_post(blink, url, is_retry=False):
|
||||
"""
|
||||
Perform an http post request.
|
||||
|
||||
:param url: URL to perfom post request.
|
||||
:param is_retry: Is this part of a re-auth attempt?
|
||||
"""
|
||||
if blink.auth_header is None:
|
||||
raise BlinkException(ERROR.AUTH_TOKEN)
|
||||
_LOGGER.debug("Making POST request to %s", url)
|
||||
return http_req(blink, url=url, headers=blink.auth_header, reqtype='post')
|
||||
return http_req(blink, url=url, headers=blink.auth_header,
|
||||
reqtype='post', is_retry=is_retry)
|
||||
|
||||
+10
-16
@@ -1,7 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
blinkpy is an unofficial api for the Blink security camera system.
|
||||
https://github.com/fronzbot/blinkpy
|
||||
|
||||
repo url: https://github.com/fronzbot/blinkpy
|
||||
|
||||
Original protocol hacking by MattTW :
|
||||
https://github.com/MattTW/BlinkMonitorProtocol
|
||||
@@ -35,7 +36,7 @@ class Blink():
|
||||
"""Class to initialize communication."""
|
||||
|
||||
def __init__(self, username=None, password=None,
|
||||
refresh_rate=REFRESH_RATE, loglevel=logging.INFO):
|
||||
refresh_rate=REFRESH_RATE):
|
||||
"""
|
||||
Initialize Blink system.
|
||||
|
||||
@@ -62,16 +63,6 @@ class Blink():
|
||||
self.cameras = CaseInsensitiveDict({})
|
||||
self._login_url = LOGIN_URL
|
||||
self.version = __version__
|
||||
self._loglevel = loglevel
|
||||
|
||||
@property
|
||||
def loglevel(self):
|
||||
return self._loglevel
|
||||
|
||||
@loglevel.setter
|
||||
def loglevel(self, value):
|
||||
"""Sets the logging level."""
|
||||
_LOGGER.setLevel(value)
|
||||
|
||||
@property
|
||||
def auth_header(self):
|
||||
@@ -86,7 +77,8 @@ class Blink():
|
||||
Essentially this is just a wrapper function for ease of use.
|
||||
"""
|
||||
if self._username is None or self._password is None:
|
||||
self.login()
|
||||
if not self.login():
|
||||
return
|
||||
elif not self.get_auth_token():
|
||||
return
|
||||
|
||||
@@ -107,7 +99,7 @@ class Blink():
|
||||
_LOGGER.warning("Unable to login with %s.", self._username)
|
||||
return False
|
||||
|
||||
def get_auth_token(self):
|
||||
def get_auth_token(self, is_retry=False):
|
||||
"""Retrieve the authentication token from Blink."""
|
||||
if not isinstance(self._username, str):
|
||||
raise BlinkAuthenticationException(ERROR.USERNAME)
|
||||
@@ -118,7 +110,8 @@ class Blink():
|
||||
response = api.request_login(self,
|
||||
login_url,
|
||||
self._username,
|
||||
self._password)
|
||||
self._password,
|
||||
is_retry=is_retry)
|
||||
try:
|
||||
if response.status_code != 200:
|
||||
_LOGGER.debug("Received response code %s during login.",
|
||||
@@ -127,7 +120,8 @@ class Blink():
|
||||
response = api.request_login(self,
|
||||
login_url,
|
||||
self._username,
|
||||
self._password)
|
||||
self._password,
|
||||
is_retry=is_retry)
|
||||
response = response.json()
|
||||
(self.region_id, self.region), = response['region'].items()
|
||||
except AttributeError:
|
||||
|
||||
@@ -35,7 +35,7 @@ def create_session():
|
||||
def attempt_reauthorization(blink):
|
||||
"""Attempt to refresh auth token and links."""
|
||||
_LOGGER.info("Auth token expired, attempting reauthorization.")
|
||||
headers = blink.get_auth_token()
|
||||
headers = blink.get_auth_token(is_retry=True)
|
||||
return headers
|
||||
|
||||
|
||||
@@ -64,7 +64,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=0.1)
|
||||
response = blink.session.send(prepped, stream=stream, timeout=10)
|
||||
if json_resp and 'code' in response.json():
|
||||
if is_retry:
|
||||
_LOGGER.error("Cannot obtain new token for server auth.")
|
||||
@@ -72,7 +72,7 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
|
||||
else:
|
||||
headers = attempt_reauthorization(blink)
|
||||
if not headers:
|
||||
raise exception.ConnectionError
|
||||
raise exceptions.ConnectionError
|
||||
return http_req(blink, url=url, data=data, headers=headers,
|
||||
reqtype=reqtype, stream=stream,
|
||||
json_resp=json_resp, is_retry=True)
|
||||
|
||||
Reference in New Issue
Block a user