Revision 21190887
b/astakosclient/astakosclient/__init__.py | ||
---|---|---|
145 | 145 |
# Get the connection object |
146 | 146 |
with self.conn_class(self.netloc) as conn: |
147 | 147 |
# Send request |
148 |
(data, status) = \ |
|
148 |
(message, data, status) = \
|
|
149 | 149 |
_do_request(conn, method, request_path, **kwargs) |
150 | 150 |
except Exception as err: |
151 | 151 |
self.logger.error("Failed to send request: %s" % repr(err)) |
... | ... | |
154 | 154 |
# Return |
155 | 155 |
self.logger.debug("Request returned with status %s" % status) |
156 | 156 |
if status == 400: |
157 |
raise BadRequest(data) |
|
157 |
raise BadRequest(message, data)
|
|
158 | 158 |
elif status == 401: |
159 |
raise Unauthorized(data) |
|
159 |
raise Unauthorized(message, data)
|
|
160 | 160 |
elif status == 403: |
161 |
raise Forbidden(data) |
|
161 |
raise Forbidden(message, data)
|
|
162 | 162 |
elif status == 404: |
163 |
raise NotFound(data) |
|
163 |
raise NotFound(message, data)
|
|
164 | 164 |
elif status < 200 or status >= 300: |
165 |
raise AstakosClientException(data, status) |
|
165 |
raise AstakosClientException(message, data, status)
|
|
166 | 166 |
return simplejson.loads(unicode(data)) |
167 | 167 |
|
168 | 168 |
# ------------------------ |
... | ... | |
314 | 314 |
length = response.getheader('content-length', None) |
315 | 315 |
data = response.read(length) |
316 | 316 |
status = int(response.status) |
317 |
return (data, status) |
|
317 |
message = response.reason |
|
318 |
return (message, data, status) |
b/astakosclient/astakosclient/errors.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
|
35 | 35 |
class AstakosClientException(Exception): |
36 |
def __init__(self, message, status=0):
|
|
36 |
def __init__(self, message='', details='', status=None):
|
|
37 | 37 |
self.message = message |
38 |
self.status = status |
|
39 |
|
|
40 |
def __str__(self): |
|
41 |
return repr(self.message) |
|
38 |
self.details = details |
|
39 |
if not hasattr(self, 'status'): |
|
40 |
self.status = status |
|
41 |
super(AstakosClientException, |
|
42 |
self).__init__(self.message, self.details, self.status) |
|
42 | 43 |
|
43 | 44 |
|
44 | 45 |
class BadRequest(AstakosClientException): |
45 |
def __init__(self, message): |
|
46 |
"""400 Bad Request""" |
|
47 |
super(BadRequest, self).__init__(message, 400) |
|
46 |
status = 400 |
|
48 | 47 |
|
49 | 48 |
|
50 | 49 |
class Unauthorized(AstakosClientException): |
51 |
def __init__(self, message): |
|
52 |
"""401 Invalid X-Auth-Token""" |
|
53 |
super(Unauthorized, self).__init__(message, 401) |
|
50 |
status = 401 |
|
54 | 51 |
|
55 | 52 |
|
56 | 53 |
class Forbidden(AstakosClientException): |
57 |
def __init__(self, message): |
|
58 |
"""403 Forbidden""" |
|
59 |
super(Forbidden, self).__init__(message, 403) |
|
54 |
status = 403 |
|
60 | 55 |
|
61 | 56 |
|
62 | 57 |
class NotFound(AstakosClientException): |
63 |
def __init__(self, message): |
|
64 |
"""404 Not Found""" |
|
65 |
super(NotFound, self).__init__(message, 404) |
|
58 |
status = 404 |
|
66 | 59 |
|
67 | 60 |
|
68 | 61 |
class NoUserName(AstakosClientException): |
b/astakosclient/astakosclient/tests.py | ||
---|---|---|
71 | 71 |
|
72 | 72 |
def _request_status_302(conn, method, url, **kwargs): |
73 | 73 |
"""This request returns 302""" |
74 |
message = "FOUND" |
|
74 | 75 |
status = 302 |
75 | 76 |
data = '<html>\r\n<head><title>302 Found</title></head>\r\n' \ |
76 | 77 |
'<body bgcolor="white">\r\n<center><h1>302 Found</h1></center>\r\n' \ |
77 | 78 |
'<hr><center>nginx/0.7.67</center>\r\n</body>\r\n</html>\r\n' |
78 |
return (data, status) |
|
79 |
return (message, data, status)
|
|
79 | 80 |
|
80 | 81 |
|
81 | 82 |
def _request_status_404(conn, method, url, **kwargs): |
82 | 83 |
"""This request returns 404""" |
84 |
message = "Not Found" |
|
83 | 85 |
status = 404 |
84 | 86 |
data = '<html><head><title>404 Not Found</title></head>' \ |
85 | 87 |
'<body><h1>Not Found</h1><p>The requested URL /foo was ' \ |
86 | 88 |
'not found on this server.</p><hr><address>Apache Server ' \ |
87 | 89 |
'at example.com Port 80</address></body></html>' |
88 |
return (data, status) |
|
90 |
return (message, data, status)
|
|
89 | 91 |
|
90 | 92 |
|
91 | 93 |
def _request_status_401(conn, method, url, **kwargs): |
92 | 94 |
"""This request returns 401""" |
95 |
message = "UNAUTHORIZED" |
|
93 | 96 |
status = 401 |
94 | 97 |
data = "Invalid X-Auth-Token\n" |
95 |
return (data, status) |
|
98 |
return (message, data, status)
|
|
96 | 99 |
|
97 | 100 |
|
98 | 101 |
def _request_status_400(conn, method, url, **kwargs): |
99 | 102 |
"""This request returns 400""" |
103 |
message = "BAD REQUEST" |
|
100 | 104 |
status = 400 |
101 | 105 |
data = "Method not allowed.\n" |
102 |
return (data, status) |
|
106 |
return (message, data, status)
|
|
103 | 107 |
|
104 | 108 |
|
105 | 109 |
def _request_ok(conn, method, url, **kwargs): |
... | ... | |
136 | 140 |
if "usage=1" not in url: |
137 | 141 |
# Strip `usage' key from `user' |
138 | 142 |
del user['usage'] |
139 |
return (simplejson.dumps(user), 200) |
|
143 |
return ("", simplejson.dumps(user), 200)
|
|
140 | 144 |
|
141 | 145 |
|
142 | 146 |
def _req_catalogs(conn, method, url, **kwargs): |
... | ... | |
176 | 180 |
return_catalog = {"displayname_catalog": catalogs, "uuid_catalog": {}} |
177 | 181 |
else: |
178 | 182 |
return_catalog = {"displayname_catalog": {}, "uuid_catalog": {}} |
179 |
return (simplejson.dumps(return_catalog), 200) |
|
183 |
return ("", simplejson.dumps(return_catalog), 200)
|
|
180 | 184 |
|
181 | 185 |
|
182 | 186 |
# ---------------------------- |
Also available in: Unified diff