forked from emehrkay/Compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.py
299 lines (271 loc) · 11 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# encoding: utf-8
import base64
import datetime
import decimal
import httplib2
import re
import time
from urlparse import urlsplit
try:
import json
except ImportError:
import simplejson as json
__author__ = "Javier de la Rosa, and Diego Muñoz Escalante"
__credits__ = ["Javier de la Rosa", "Diego Muñoz Escalante"]
__license__ = "GPLv3"
__version__ = "0.1.0"
__email__ = "versae [at] gmail [dot] com"
__status__ = "Development"
CACHE = False
DEBUG = False
class StatusException(Exception):
"""
Create an Error Response.
"""
def __init__(self, value, result=None):
self.value = value
self.responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),
400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
418: ('I\'m a teapot', 'Is the server running?'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}
if result:
self.result = "\n%s" % result
def __str__(self):
return u"Error [%s]: %s. %s.%s" % (self.value,
self.responses[self.value][0],
self.responses[self.value][1],
self.result)
def __unicode__(self):
return self.__str__()
class NotFoundError(StatusException):
def __init__(self, value=None, result=None):
if not value:
value = 404
if not result:
result = "Node, relationship or property not found"
super(NotFoundError, self).__init__(value, result)
class Request(object):
"""
Create an HTTP request object for HTTP
verbs GET, POST, PUT and DELETE.
"""
def __init__(self, username=None, password=None, key_file=None,
cert_file=None):
self.username = username
self.password = password
self.key_file = key_file
self.cert_file = cert_file
self._illegal_s = re.compile(r"((^|[^%])(%%)*%s)")
def get(self, url, headers=None):
"""
Perform an HTTP GET request for a given URL.
Returns the response object.
"""
return self._request('GET', url, headers=headers)
def post(self, url, data, headers=None):
"""
Perform an HTTP POST request for a given url.
Returns the response object.
"""
return self._request('POST', url, data, headers=headers)
def put(self, url, data, headers=None):
"""
Perform an HTTP PUT request for a given url.
Returns the response object.
"""
return self._request('PUT', url, data, headers=headers)
def delete(self, url, headers=None):
"""
Perform an HTTP DELETE request for a given url.
Returns the response object.
"""
return self._request('DELETE', url, headers=headers)
# Proleptic Gregorian dates and strftime before 1900 « Python recipes
# ActiveState Code: http://bit.ly/9t0JKb via @addthis
def _findall(self, text, substr):
# Also finds overlaps
sites = []
i = 0
while 1:
j = text.find(substr, i)
if j == -1:
break
sites.append(j)
i = j + 1
return sites
# Every 28 years the calendar repeats, except through century leap
# years where it's 6 years. But only if you're using the Gregorian
# calendar. ;)
def _strftime(self, dt, fmt):
if self._illegal_s.search(fmt):
raise TypeError("This strftime implementation does not handle %s")
if dt.year > 1900:
return dt.strftime(fmt)
year = dt.year
# For every non-leap year century, advance by
# 6 years to get into the 28-year repeat cycle
delta = 2000 - year
off = 6 * (delta // 100 + delta // 400)
year = year + off
# Move to around the year 2000
year = year + ((2000 - year) // 28) * 28
timetuple = dt.timetuple()
s1 = time.strftime(fmt, (year,) + timetuple[1:])
sites1 = self._findall(s1, str(year))
s2 = time.strftime(fmt, (year + 28,) + timetuple[1:])
sites2 = self._findall(s2, str(year + 28))
sites = []
for site in sites1:
if site in sites2:
sites.append(site)
s = s1
syear = "%4d" % (dt.year, )
for site in sites:
s = s[:site] + syear + s[site + 4:]
return s
def _json_encode(self, data, ensure_ascii=False):
def _any(data):
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
ret = None
if isinstance(data, (list, tuple)):
ret = _list(data)
elif isinstance(data, dict):
ret = _dict(data)
elif isinstance(data, decimal.Decimal):
ret = str(data)
elif isinstance(data, datetime.datetime):
ret = self._strftime(data,
"%s %s" % (DATE_FORMAT, TIME_FORMAT))
elif isinstance(data, datetime.date):
ret = self._strftime(data, DATE_FORMAT)
elif isinstance(data, datetime.time):
ret = data.strftime(TIME_FORMAT)
else:
ret = data
return ret
def _list(data):
ret = []
for v in data:
ret.append(_any(v))
return ret
def _dict(data):
ret = {}
for k, v in data.items():
# Neo4j doesn't allow 'null' properties
if v:
ret[k] = _any(v)
return ret
ret = _any(data)
return json.dumps(ret, ensure_ascii=ensure_ascii)
def _request(self, method, url, data={}, headers={}):
global CACHE, DEBUG
splits = urlsplit(url)
scheme = splits.scheme
# Not used, it makes pyflakes happy
# hostname = splits.hostname
# port = splits.port
username = splits.username or self.username
password = splits.password or self.password
headers = headers or {}
if DEBUG:
httplib2.debuglevel = 1
else:
httplib2.debuglevel = 0
if CACHE:
headers['Cache-Control'] = 'no-cache'
http = httplib2.Http(".cache")
else:
http = httplib2.Http()
if scheme.lower() == 'https':
http.add_certificate(self.key_file, self.cert_file, self.url)
headers['Accept'] = 'application/json'
headers['Accept-Encoding'] = '*'
headers['Accept-Charset'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
# I'm not sure on the right policy about cache with Neo4j REST Server
# headers['Cache-Control'] = 'no-cache'
# TODO: Handle all requests with the same Http object
headers['Connection'] = 'close'
if username and password:
credentials = "%s:%s" % (username, password)
base64_credentials = base64.encodestring(credentials)
authorization = "Basic %s" % base64_credentials[:-1]
headers['Authorization'] = authorization
headers['Remote-User'] = username
if method in ("POST", "PUT"):
headers['Content-Type'] = 'application/json'
# Thanks to Yashh: http://bit.ly/cWsnZG
# Don't JSON encode body when it starts with "http://" to set inde
if isinstance(data, (str, unicode)) and data.startswith('http://'):
body = data
else:
## OrientDB reset Socket if body is '{}'
if data is not None and len(data) > 0 :
body = self._json_encode(data, ensure_ascii=True)
else:
body = '';
try:
response, content = http.request(url, method, headers=headers,
body=body)
return response, content
except AttributeError:
raise Exception("Unknown error. Is the server running?")