Commit 564eb947 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis

astakosclient: AstakosClientException now has details attr

parent a0eb1fca
......@@ -145,7 +145,7 @@ class AstakosClient():
# Get the connection object
with self.conn_class(self.netloc) as conn:
# Send request
(data, status) = \
(message, data, status) = \
_do_request(conn, method, request_path, **kwargs)
except Exception as err:
self.logger.error("Failed to send request: %s" % repr(err))
......@@ -154,15 +154,15 @@ class AstakosClient():
# Return
self.logger.debug("Request returned with status %s" % status)
if status == 400:
raise BadRequest(data)
raise BadRequest(message, data)
elif status == 401:
raise Unauthorized(data)
raise Unauthorized(message, data)
elif status == 403:
raise Forbidden(data)
raise Forbidden(message, data)
elif status == 404:
raise NotFound(data)
raise NotFound(message, data)
elif status < 200 or status >= 300:
raise AstakosClientException(data, status)
raise AstakosClientException(message, data, status)
return simplejson.loads(unicode(data))
# ------------------------
......@@ -314,4 +314,5 @@ def _do_request(conn, method, url, **kwargs):
length = response.getheader('content-length', None)
data = response.read(length)
status = int(response.status)
return (data, status)
message = response.reason
return (message, data, status)
......@@ -33,36 +33,29 @@
class AstakosClientException(Exception):
def __init__(self, message, status=0):
def __init__(self, message='', details='', status=None):
self.message = message
self.status = status
def __str__(self):
return repr(self.message)
self.details = details
if not hasattr(self, 'status'):
self.status = status
super(AstakosClientException,
self).__init__(self.message, self.details, self.status)
class BadRequest(AstakosClientException):
def __init__(self, message):
"""400 Bad Request"""
super(BadRequest, self).__init__(message, 400)
status = 400
class Unauthorized(AstakosClientException):
def __init__(self, message):
"""401 Invalid X-Auth-Token"""
super(Unauthorized, self).__init__(message, 401)
status = 401
class Forbidden(AstakosClientException):
def __init__(self, message):
"""403 Forbidden"""
super(Forbidden, self).__init__(message, 403)
status = 403
class NotFound(AstakosClientException):
def __init__(self, message):
"""404 Not Found"""
super(NotFound, self).__init__(message, 404)
status = 404
class NoUserName(AstakosClientException):
......
......@@ -71,35 +71,39 @@ def _request_offline(conn, method, url, **kwargs):
def _request_status_302(conn, method, url, **kwargs):
"""This request returns 302"""
message = "FOUND"
status = 302
data = '<html>\r\n<head><title>302 Found</title></head>\r\n' \
'<body bgcolor="white">\r\n<center><h1>302 Found</h1></center>\r\n' \
'<hr><center>nginx/0.7.67</center>\r\n</body>\r\n</html>\r\n'
return (data, status)
return (message, data, status)
def _request_status_404(conn, method, url, **kwargs):
"""This request returns 404"""
message = "Not Found"
status = 404
data = '<html><head><title>404 Not Found</title></head>' \
'<body><h1>Not Found</h1><p>The requested URL /foo was ' \
'not found on this server.</p><hr><address>Apache Server ' \
'at example.com Port 80</address></body></html>'
return (data, status)
return (message, data, status)
def _request_status_401(conn, method, url, **kwargs):
"""This request returns 401"""
message = "UNAUTHORIZED"
status = 401
data = "Invalid X-Auth-Token\n"
return (data, status)
return (message, data, status)
def _request_status_400(conn, method, url, **kwargs):
"""This request returns 400"""
message = "BAD REQUEST"
status = 400
data = "Method not allowed.\n"
return (data, status)
return (message, data, status)
def _request_ok(conn, method, url, **kwargs):
......@@ -136,7 +140,7 @@ def _req_authenticate(conn, method, url, **kwargs):
if "usage=1" not in url:
# Strip `usage' key from `user'
del user['usage']
return (simplejson.dumps(user), 200)
return ("", simplejson.dumps(user), 200)
def _req_catalogs(conn, method, url, **kwargs):
......@@ -176,7 +180,7 @@ def _req_catalogs(conn, method, url, **kwargs):
return_catalog = {"displayname_catalog": catalogs, "uuid_catalog": {}}
else:
return_catalog = {"displayname_catalog": {}, "uuid_catalog": {}}
return (simplejson.dumps(return_catalog), 200)
return ("", simplejson.dumps(return_catalog), 200)
# ----------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment