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
|
import httplib
import simplejson
from urlparse import urlunparse
from urllib import (
quote,
urlencode,
)
class UnsupportedOrder(Exception):
pass
class GrackleClient:
def __init__(self, host, port):
self.host = host
self.port = port
self.netloc = '%s:%d' % (host, port)
def archive_url(self, archive_id, query):
path = '/archive/%s' % quote(archive_id)
query_string = urlencode(query)
return urlunparse(('http', self.netloc, path, '', query_string, ''))
def _get_connection(self):
return httplib.HTTPConnection(self.host, self.port)
def _verb_archive(self, verb, archive_id, query, body=None):
url = self.archive_url(archive_id, query)
connection = self._get_connection()
connection.request(verb, url, body)
return connection.getresponse()
def put_message(self, archive_id, key, file_obj):
response = self._verb_archive(
'POST', archive_id, {'key': key}, file_obj.read())
data = response.read()
if response.status == httplib.BAD_REQUEST:
raise Exception('wtf')
elif response.status == httplib.CREATED:
return
else:
raise Exception('!!')
def get_messages(self, archive_id, message_ids=None, limit=None,
memo=None, order=None, headers=None,
max_body_length=None, include_hidden=False):
parameters = {}
if message_ids is not None:
parameters['message_ids'] = message_ids
if limit is not None:
parameters['limit'] = limit
if memo is not None:
parameters['memo'] = memo
if order is not None:
parameters['order'] = order
if headers is not None:
parameters['headers'] = headers
if max_body_length is not None:
parameters['max_body_length'] = max_body_length
parameters['include_hidden'] = include_hidden
query = {'parameters': simplejson.dumps(parameters)}
response = self._verb_archive('GET', archive_id, query)
if response.status == httplib.BAD_REQUEST:
raise UnsupportedOrder
data = response.read()
return simplejson.loads(data)
|