diff --git a/redfish/connection.py b/redfish/connection.py index 6f40c10..2379ece 100644 --- a/redfish/connection.py +++ b/redfish/connection.py @@ -127,11 +127,12 @@ import sys import urllib2 from urlparse import urlparse +from redfish import exception LOG = logging.getLogger(__name__) - LOG.setLevel(logging.DEBUG) + class RedfishConnection(object): """Implements basic connection handling for Redfish APIs.""" @@ -196,7 +197,7 @@ class RedfishConnection(object): elif url.scheme == 'http': conn = httplib.HTTPConnection(host=url.netloc, strict=True) else: - raise RedfishException(message='Unknown connection schema') + raise exception.RedfishException(message='Unknown connection schema') # NOTE: Do not assume every HTTP operation will return a JSON body. # For example, ExtendedError structures are only required for HTTP 400 @@ -220,7 +221,9 @@ class RedfishConnection(object): response = dict() try: + LOG.debug("BODY: %s." % body.decode('utf-8')) response = json.loads(body.decode('utf-8')) + LOG.debug("Loaded json: %s" % response) except ValueError: # if it doesn't decode as json # NOTE: resources may return gzipped content # try to decode as gzip (we should check the headers for Content-Encoding=gzip) @@ -229,10 +232,7 @@ class RedfishConnection(object): uncompressed_string = gzipper.read().decode('UTF-8') response = json.loads(uncompressed_string) except: - pass - - # return empty - pass + raise exception.RedfishException(message='Failed to parse response as a JSON document, received "%s".' % body) return resp.status, headers, response diff --git a/redfish/exceptions.py b/redfish/exception.py similarity index 64% rename from redfish/exceptions.py rename to redfish/exception.py index cd320ff..99644c7 100644 --- a/redfish/exceptions.py +++ b/redfish/exception.py @@ -15,4 +15,13 @@ class RedfishException(Exception): """Base class for redfish exceptions""" - pass + def __init__(self, message=None, **kwargs): + self.kwargs = kwargs + + if not message: + try: + message = self.message % kwargs + except Excetion as e: + LOG.exception('Error in string format operation') + message = self.message + super(RedfishException, self).__init__(message) \ No newline at end of file diff --git a/redfish/tests/test_redfish.py b/redfish/tests/test_redfish.py index 9bb5ae0..dfaadc7 100644 --- a/redfish/tests/test_redfish.py +++ b/redfish/tests/test_redfish.py @@ -42,7 +42,7 @@ def get_response(): class _response(object): status = 200 def read(self): - return "{'foo': 'bar'}" + return '{"foo": "bar"}' def getheaders(self): return [('Fake-Header', 'fake value')] return _response() @@ -91,3 +91,12 @@ class TestRedfishConnection(base.TestCase): # ssl_mock.return_value = mock.Mock() # con = connection.RedfishConnection(*get_fake_params) # ssl_mock.assert_called_once_with(ssl.PROTOCOL_TLSv1) + + def test_get_ok(self): + con = connection.RedfishConnection(*get_fake_params()) + res = con.rest_get('/v1/test', '') + self.assertEqual(200, res[0]) + # Headers ae lower cased when returned + self.assertIn('fake-header', res[1].keys()) + print(res) + self.assertIn('foo', res[2].keys()) \ No newline at end of file