Skip to content

Commit

Permalink
Properly raise request errors, and distinguish auth failures
Browse files Browse the repository at this point in the history
  • Loading branch information
kk7ds committed Feb 6, 2016
1 parent 0561c5c commit c6ab616
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
12 changes: 11 additions & 1 deletion uvcclient/nvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c6ab616

Please sign in to comment.