-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.py
More file actions
163 lines (125 loc) · 4.82 KB
/
request.py
File metadata and controls
163 lines (125 loc) · 4.82 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import sys
import json
from pybutton.error import ButtonClientError
# `pybutton.request` will expose 4 attributes, all specific to the major
# version of the interpreter:
#
# * `Request`: The request class used to make network calls
# * `urlopen`: The function used to make the network call
# * `HTTPError`: The Exception class raised by `urlopen`
# * `request`: A function for making a network request
#
if sys.version_info[0] == 3:
from urllib.request import Request
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.parse import urlunsplit
from urllib.parse import urlparse
from urllib.parse import parse_qs
def request(url, method, headers, data=None, timeout=None):
''' Make an HTTP request in Python 3.x
This method will abstract the underlying organization and invocation of
the Python 3 HTTP standard lib implementation.
Args:
url (str): The url of the resource to request
method (str): The HTTP method
headers (dict): HTTP Headers as key value pairs to include
data (dict) optional: Data to be encoded as JSON and included in
the request body
Raises:
urllib.error.HTTPError
pybutton.ButtonClientError
Returns:
(dict): The response from the server interpreted as JSON.
'''
encoded_data = json.dumps(data).encode('utf8') if data else None
request = Request(url, data=encoded_data, headers=headers)
request.get_method = lambda: method
if data:
request.add_header('Content-Type', 'application/json')
response = urlopen(request, timeout=timeout).read().decode('utf8')
try:
return json.loads(response)
except ValueError:
raise ButtonClientError('Invalid response: {0}'.format(response))
else:
from urllib2 import Request
from urllib2 import urlopen
from urllib2 import HTTPError
from urllib import urlencode
from urlparse import urlunsplit
from urlparse import urlparse
from urlparse import parse_qs
def request(url, method, headers, data=None, timeout=None):
''' Make an HTTP request in Python 2.x
This method will abstract the underlying organization and invocation of
the Python 2 HTTP standard lib implementation.
Args:
url (str): The url of the resource to request
method (str): The HTTP method
headers (dict): HTTP Headers as key value pairs to include
data (dict) optional: Data to be encoded as JSON and included in
the request body
Raises:
urllib2.HTTPError
pybutton.ButtonClientError
Returns:
(dict): The response from the server interpreted as JSON.
'''
request = Request(url)
request.get_method = lambda: method
for k, v in headers.iteritems():
request.add_header(k, v)
if data:
request.add_header('Content-Type', 'application/json')
request.add_data(json.dumps(data))
response = urlopen(request, timeout=timeout).read()
try:
return json.loads(response)
except ValueError:
raise ButtonClientError('Invalid response: {0}'.format(response))
def request_url(secure, hostname, port, path, query=None):
'''
Combines url components into a url passable into the request function.
Args:
secure (boolean): Whether or not to use HTTPS.
hostname (str): The host name for the url.
port (int): The port number, as an integer.
path (str): The hierarchical path.
query (dict): A dict of query parameters.
Returns:
(str) A complete url made up of the arguments.
'''
encoded_query = urlencode(query) if query else ''
scheme = 'https' if secure else 'http'
netloc = '{0}:{1}'.format(hostname, port)
return urlunsplit((scheme, netloc, path, encoded_query, ''))
def query_dict(url):
'''
Given a url, returns a dictionary of its query parameters.
Args:
url (string): The url to extract query parameters from.
Returns:
(dict) A dictionary of query parameters, formatted as follows:
{
query_name: [ list of values ],
...
}
'''
url_components = urlparse(url)
if (url_components):
query_string = url_components.query
return parse_qs(query_string)
__all__ = [
Request,
urlopen,
HTTPError,
request,
request_url,
query_dict,
]