From c6ab616f673839d311f8b772ea095a604dda5480 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Sat, 6 Feb 2016 14:07:50 -0800 Subject: [PATCH] Properly raise request errors, and distinguish auth failures --- tests/test_client.py | 14 ++++++++++++-- uvcclient/nvr.py | 12 +++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 8d4c0fa..90216a9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -65,8 +65,18 @@ def test_uvc_request_failed(self): conn = httplib.HTTPConnection.return_value resp = conn.getresponse.return_value resp.status = 404 - result = client._uvc_request('/bar', method='PUT', data='foobar') - self.assertEqual(None, result) + self.assertRaises(nvr.NvrError, + client._uvc_request, '/bar', method='PUT', + data='foobar') + + def test_uvc_request_failed_noauth(self): + client = nvr.UVCRemote('foo', 7080, 'key') + conn = httplib.HTTPConnection.return_value + resp = conn.getresponse.return_value + resp.status = 401 + self.assertRaises(nvr.NotAuthorized, + client._uvc_request, '/bar', method='PUT', + data='foobar') def test_uvc_request_deflated(self): client = nvr.UVCRemote('foo', 7080, 'key') diff --git a/uvcclient/nvr.py b/uvcclient/nvr.py index 04d64bd..9ae420d 100755 --- a/uvcclient/nvr.py +++ b/uvcclient/nvr.py @@ -39,6 +39,14 @@ class Invalid(Exception): pass +class NotAuthorized(Exception): + pass + + +class NvrError(Exception): + pass + + class UVCRemote(object): """Remote control client for Ubiquiti Unifi Video NVR.""" CHANNEL_NAMES = ['high', 'medium', 'low'] @@ -72,8 +80,10 @@ def _uvc_request(self, path, method='GET', data=None, headers = dict(resp.getheaders()) self._log.debug('%s %s Result: %s %s' % (method, url, resp.status, resp.reason)) + if resp.status in (401, 403): + raise NotAuthorized('NVR reported authorization failure') if resp.status / 100 != 2: - return + raise NvrError('Request failed: %s' % resp.status) data = resp.read() if (headers.get('content-encoding') == 'gzip' or