Added additional re-auth attempt in try/except

This commit is contained in:
Kevin Fronczak
2018-10-18 12:49:18 -04:00
parent f61f68a5c2
commit 1a12eb367f
2 changed files with 21 additions and 6 deletions
+7 -1
View File
@@ -59,7 +59,13 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
reqtype=reqtype, stream=stream,
json_resp=json_resp, is_retry=True)
except (exceptions.ConnectionError, exceptions.Timeout):
_LOGGER.error("Cannot connect to server. Possible outage.")
_LOGGER.error("Cannot connect to server with url %s.", url)
if not is_retry:
headers = attempt_reauthorization(blink)
return http_req(blink, url=url, data=data, headers=headers,
reqtype=reqtype, stream=stream,
json_resp=json_resp, is_retry=True)
_LOGGER.error("Possible issue with Blink servers.")
return None
if json_resp:
+14 -5
View File
@@ -1,6 +1,7 @@
"""Test various api functions."""
import unittest
from unittest import mock
from blinkpy import api
from blinkpy.blinkpy import Blink
from blinkpy.helpers.util import create_session
@@ -20,13 +21,21 @@ class TestBlinkAPI(unittest.TestCase):
"""Tear down blink module."""
self.blink = None
def test_http_req_connect_error(self):
@mock.patch('blinkpy.blinkpy.Blink.get_auth_token')
def test_http_req_connect_error(self, mock_auth):
"""Test http_get error condition."""
expected = ("ERROR:blinkpy.helpers.util:"
"Cannot connect to server. Possible outage.")
mock_auth.return_value = {'foo': 'bar'}
firstlog = ("ERROR:blinkpy.helpers.util:"
"Cannot connect to server with url {}").format(
'http://notreal.fake.')
nextlog = ("INFO:blinkpy.helpers.util:"
"Auth token expired, attempting reauthorization.")
lastlog = ("ERROR:blinkpy.helpers.util:"
"Possible issue with Blink servers.")
expected = [firstlog, nextlog, firstlog, lastlog]
with self.assertLogs() as getlog:
api.http_get(self.blink, 'http://notreal.fake')
with self.assertLogs() as postlog:
api.http_post(self.blink, 'http://notreal.fake')
self.assertEqual(getlog.output, [expected])
self.assertEqual(postlog.output, [expected])
self.assertEqual(getlog.output, expected)
self.assertEqual(postlog.output, expected)