3
def put_message(archive_name, file_obj):
4
connection = httplib.HTTPConnection('localhost', 8435)
5
connection.request('POST', '/archive/%s' % archive_name, file_obj.read())
6
response = connection.getresponse()
8
if response.status == httplib.BAD_REQUEST:
10
elif response.status == httplib.CREATED:
3
from urlparse import urlunparse
10
class UnsupportedOrder(Exception):
16
def __init__(self, host, port):
19
self.netloc = '%s:%d' % (host, port)
21
def archive_url(self, archive_id, query):
22
path = '/archive/%s' % quote(archive_id)
23
query_string = urlencode(query)
24
return urlunparse(('http', self.netloc, path, '', query_string, ''))
26
def _get_connection(self):
27
return httplib.HTTPConnection(self.host, self.port)
29
def _verb_archive(self, verb, archive_id, query, body=None):
30
url = self.archive_url(archive_id, query)
31
connection = self._get_connection()
32
connection.request(verb, url, body)
33
return connection.getresponse()
35
def put_message(self, archive_id, key, file_obj):
36
response = self._verb_archive(
37
'POST', archive_id, {'key': key}, file_obj.read())
38
data = response.read()
39
if response.status == httplib.BAD_REQUEST:
40
raise Exception('wtf')
41
elif response.status == httplib.CREATED:
46
def get_messages(self, archive_id, message_ids=None, limit=None,
47
memo=None, order=None, headers=None,
48
max_body_length=None, include_hidden=False):
50
if message_ids is not None:
51
parameters['message_ids'] = message_ids
53
parameters['limit'] = limit
55
parameters['memo'] = memo
57
parameters['order'] = order
58
if headers is not None:
59
parameters['headers'] = headers
60
if max_body_length is not None:
61
parameters['max_body_length'] = max_body_length
62
parameters['include_hidden'] = include_hidden
63
query = {'parameters': simplejson.dumps(parameters)}
64
response = self._verb_archive('GET', archive_id, query)
65
if response.status == httplib.BAD_REQUEST:
66
raise UnsupportedOrder
67
data = response.read()
68
return simplejson.loads(data)