forked from square/square-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_response.py
More file actions
57 lines (42 loc) · 1.73 KB
/
api_response.py
File metadata and controls
57 lines (42 loc) · 1.73 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
56
57
import json
class ApiResponse:
"""Http response received.
Attributes:
status_code (int): The status code response from the server that
corresponds to this response.
reason_phrase (string): The reason phrase returned by the server.
headers (dict): A dictionary of headers (key : value) that were
returned with the response
text (string): The Raw body of the HTTP Response as a string
request (HttpRequest): The request that resulted in this response.
body (Object): The data field specified for the response
errors (Array<String>): Any errors returned by the server
"""
def __init__(self, http_response,
body=None,
errors=None):
"""The Constructor
Args:
http_response (HttpResponse): The original, raw response from the api
data (Object): The data field specified for the response
errors (Array<String>): Any errors returned by the server
"""
self.status_code = http_response.status_code
self.reason_phrase = http_response.reason_phrase
self.headers = http_response.headers
self.text = http_response.text
self.request = http_response.request
self.body = body
self.errors = errors
if type(body) is dict:
self.cursor = body.get('cursor')
def is_success(self):
""" Returns true if status code is between 200-300
"""
return 200 <= self.status_code < 300
def is_error(self):
""" Returns true if status code is between 400-600
"""
return 400 <= self.status_code < 600
def __repr__(self):
return '<ApiResponse [%s]>' % (self.text)