6
by Aaron Bentley
Use constants. |
1 |
import httplib |
11
by Aaron Bentley
Start working on GET. |
2 |
import simplejson |
3 |
from urlparse import urlunparse |
|
4 |
from urllib import ( |
|
5 |
quote, |
|
6 |
urlencode, |
|
7 |
)
|
|
8 |
||
7
by Aaron Bentley
Fix URLs etc. |
9 |
|
21
by Aaron Bentley
Test unsupported orders. |
10 |
class UnsupportedOrder(Exception): |
11 |
pass
|
|
12 |
||
7
by Aaron Bentley
Fix URLs etc. |
13 |
|
14 |
class GrackleClient: |
|
15 |
||
16 |
def __init__(self, host, port): |
|
17 |
self.host = host |
|
18 |
self.port = port |
|
11
by Aaron Bentley
Start working on GET. |
19 |
self.netloc = '%s:%d' % (host, port) |
20 |
||
18
by Aaron Bentley
archive_name -> archive_id |
21 |
def archive_url(self, archive_id, query): |
26
by Aaron Bentley
Implement an archive namespace. |
22 |
path = '/archive/%s' % quote(archive_id) |
15
by Aaron Bentley
Test filtering by message-id. |
23 |
query_string = urlencode(query) |
24 |
return urlunparse(('http', self.netloc, path, '', query_string, '')) |
|
11
by Aaron Bentley
Start working on GET. |
25 |
|
26 |
def _get_connection(self): |
|
27 |
return httplib.HTTPConnection(self.host, self.port) |
|
28 |
||
18
by Aaron Bentley
archive_name -> archive_id |
29 |
def _verb_archive(self, verb, archive_id, query, body=None): |
30 |
url = self.archive_url(archive_id, query) |
|
11
by Aaron Bentley
Start working on GET. |
31 |
connection = self._get_connection() |
32 |
connection.request(verb, url, body) |
|
33 |
return connection.getresponse() |
|
34 |
||
18
by Aaron Bentley
archive_name -> archive_id |
35 |
def put_message(self, archive_id, key, file_obj): |
11
by Aaron Bentley
Start working on GET. |
36 |
response = self._verb_archive( |
18
by Aaron Bentley
archive_name -> archive_id |
37 |
'POST', archive_id, {'key': key}, file_obj.read()) |
7
by Aaron Bentley
Fix URLs etc. |
38 |
data = response.read() |
39 |
if response.status == httplib.BAD_REQUEST: |
|
40 |
raise Exception('wtf') |
|
41 |
elif response.status == httplib.CREATED: |
|
42 |
return
|
|
43 |
else: |
|
44 |
raise Exception('!!') |
|
11
by Aaron Bentley
Start working on GET. |
45 |
|
19
by Aaron Bentley
Implement memo/limit support. |
46 |
def get_messages(self, archive_id, message_ids=None, limit=None, |
28
by Aaron Bentley
Extract GrackleStore. |
47 |
memo=None, order=None, headers=None, |
29
by Aaron Bentley
implement include_hidden. |
48 |
max_body_length=None, include_hidden=False): |
11
by Aaron Bentley
Start working on GET. |
49 |
parameters = {} |
50 |
if message_ids is not None: |
|
13
by Aaron Bentley
Retrieve messages. |
51 |
parameters['message_ids'] = message_ids |
19
by Aaron Bentley
Implement memo/limit support. |
52 |
if limit is not None: |
53 |
parameters['limit'] = limit |
|
54 |
if memo is not None: |
|
55 |
parameters['memo'] = memo |
|
20
by Aaron Bentley
Support order by date |
56 |
if order is not None: |
57 |
parameters['order'] = order |
|
27
by Aaron Bentley
get_messages supports header parameter. |
58 |
if headers is not None: |
59 |
parameters['headers'] = headers |
|
28
by Aaron Bentley
Extract GrackleStore. |
60 |
if max_body_length is not None: |
61 |
parameters['max_body_length'] = max_body_length |
|
29
by Aaron Bentley
implement include_hidden. |
62 |
parameters['include_hidden'] = include_hidden |
11
by Aaron Bentley
Start working on GET. |
63 |
query = {'parameters': simplejson.dumps(parameters)} |
18
by Aaron Bentley
archive_name -> archive_id |
64 |
response = self._verb_archive('GET', archive_id, query) |
21
by Aaron Bentley
Test unsupported orders. |
65 |
if response.status == httplib.BAD_REQUEST: |
66 |
raise UnsupportedOrder |
|
13
by Aaron Bentley
Retrieve messages. |
67 |
data = response.read() |
68 |
return simplejson.loads(data) |
|
27
by Aaron Bentley
get_messages supports header parameter. |
69 |