forked from twilio/twilio-python
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequest.py
77 lines (63 loc) · 2.21 KB
/
request.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from urllib.parse import urlencode
class Request(object):
"""
An HTTP request.
"""
ANY = '*'
def __init__(self,
method=ANY,
url=ANY,
auth=ANY,
params=ANY,
data=ANY,
headers=ANY,
**kwargs):
self.method = method.upper()
self.url = url
self.auth = auth
self.params = params
self.data = data
self.headers = headers
@classmethod
def attribute_equal(cls, lhs, rhs):
if lhs == cls.ANY or rhs == cls.ANY:
# ANY matches everything
return True
lhs = lhs or None
rhs = rhs or None
return lhs == rhs
def __eq__(self, other):
if not isinstance(other, Request):
return False
return self.attribute_equal(self.method, other.method) and \
self.attribute_equal(self.url, other.url) and \
self.attribute_equal(self.auth, other.auth) and \
self.attribute_equal(self.params, other.params) and \
self.attribute_equal(self.data, other.data) and \
self.attribute_equal(self.headers, other.headers)
def __str__(self):
auth = ''
if self.auth and self.auth != self.ANY:
auth = '{} '.format(self.auth)
params = ''
if self.params and self.params != self.ANY:
params = '?{}'.format(urlencode(self.params, doseq=True))
data = ''
if self.data and self.data != self.ANY:
if self.method == 'GET':
data = '\n -G'
data += '\n{}'.format('\n'.join(' -d "{}={}"'.format(k, v) for k, v in self.data.items()))
headers = ''
if self.headers and self.headers != self.ANY:
headers = '\n{}'.format('\n'.join(' -H "{}: {}"'.format(k, v)
for k, v in self.headers.items()))
return '{auth}{method} {url}{params}{data}{headers}'.format(
auth=auth,
method=self.method,
url=self.url,
params=params,
data=data,
headers=headers,
)
def __repr__(self):
return str(self)