Repository navigation
Expand file tree
/
Copy pathstatus_code.py
More file actions
55 lines (42 loc) · 1.58 KB
/
Copy pathstatus_code.py
File metadata and controls
55 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
class HttpResponseError(Exception):
"""
Represents an HTTP response error.
"""
def __init__(self, code, reason):
#: Number representing the error (example: 404)
self.status_code = code
#: Reason of the fail (example: "Not found")
self.reason = reason.title()
#: A message for the error (example: "404 - Not found")
self.msg = "%s - %s" % (self.status_code, self.reason)
def __str__(self):
return self.msg
class StatusCode(object):
http_errors = (400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411,
412, 413, 414, 415, 416, 417, 500, 501, 502, 503, 504, 505)
def __init__(self, status_code, reason):
#: A message for the response (example: Success)
self.reason = reason
#: Code of the response (example: 200)
self.code = status_code
def __cmp__(self, other):
if self.code == other:
return 0
return 1
def is_valid_response(self):
"""
Returns ``True`` if the response is valid (:attr:`code` < 400).
Otherwise, raises an :class:`HttpResponseError <splinter.request_handler.status_code.HttpResponseError>`
exception.
"""
if self.code in self.http_errors:
raise HttpResponseError(self.code, self.reason)
return True
def is_success(self):
"""
Returns ``True`` if the response was succed, otherwise, returns ``False``.
"""
if self.code not in self.http_errors:
return True
return False