10
class UnparsableDateRange(Exception):
11
"""The date_range was not in the format of 2012-01-01..2012-01-31."""
14
class UnsupportedDisplayType(Exception):
15
"""Raised when an Unsupported display_type is requested."""
18
class UnsupportedOrder(Exception):
19
"""Raised when an Unsupported order is requested."""
22
class MessageIdNotFound(Exception):
23
"""No message matching the message_id was found in the archive."""
26
SUPPORTED_DISPLAY_TYPES = (
9
from grackle.error import (
12
UnsupportedDisplayType,
44
28
self.netloc = '%s:%d' % (host, port)
46
def archive_url(self, archive_id, query):
30
def archive_url(self, path, query):
47
31
"""Return the URL for an archive
49
:param archive_id: The id of the archive to generate the URL for.
33
:param oath: The path to generate the URL for.
50
34
:param query: The query to use in the URL, as a dict.
52
path = '/archive/%s' % quote(archive_id)
36
path = '/archive/%s' % quote(path)
53
37
query_string = urlencode(query)
54
38
return urlunparse(('http', self.netloc, path, '', query_string, ''))
56
40
def _get_connection(self):
57
41
return httplib.HTTPConnection(self.host, self.port)
59
def _method_archive(self, method, archive_id, query, body=None):
43
def _method_archive(self, method, path, query, body=None):
60
44
"""Perform an HTTP method on an archive's URL."""
61
url = self.archive_url(archive_id, query)
45
url = self.archive_url(path, query)
62
46
connection = self._get_connection()
63
47
connection.request(method, url, body)
64
48
return connection.getresponse()
50
def put_archive(self, archive_id, mbox=None):
53
:param archive_id: The archive id.
54
:param mbox: An optional mbox with messages to add to the new archive.
56
response = self._method_archive(
57
'POST', '', {'archive_id': archive_id}, None)
59
if response.status == httplib.BAD_REQUEST:
60
raise Exception('wtf')
61
elif response.status == httplib.CREATED:
66
66
def put_message(self, archive_id, key, file_obj):
67
67
"""Put a message into an archive.
72
72
:param file_obj: The raw text of the message, as a file.
74
path = '%s/%s' % (archive_id, key)
74
75
response = self._method_archive(
75
'POST', archive_id, {'key': key}, file_obj.read())
76
'POST', path, {}, file_obj.read())
77
78
if response.status == httplib.BAD_REQUEST:
78
79
raise Exception('wtf')
147
148
raise ValueError('Bad request')
148
149
data = response.read()
149
150
return simplejson.loads(data)
152
def hide_message(self, archive_id, message_id, hidden):
156
query = {'parameters': simplejson.dumps(parameters)}
157
path = '%s/%s' % (archive_id, message_id)
158
response = self._method_archive('POST', path, query)
159
if response.status == httplib.BAD_REQUEST:
160
if response.reason == MessageIdNotFound.__doc__:
161
raise MessageIdNotFound
162
data = response.read()
163
return simplejson.loads(data)