10
class UnsupportedDisplayType(Exception):
11
"""Raised when an Unsupported display_type is requested."""
14
class UnsupportedOrder(Exception):
15
"""Raised when an Unsupported order is requested."""
18
SUPPORTED_DISPLAY_TYPES = (
9
from grackle.error import (
12
UnsupportedDisplayType,
55
47
connection.request(method, url, body)
56
48
return connection.getresponse()
50
def message_url(self, archive_id, message_id, query):
51
"""Return the URL for a message
53
:param archive_id: The id of the archive to generate the URL for.
54
:param query: The query to use in the URL, as a dict.
56
path = '/archive/%s/%s' % (quote(archive_id), quote(message_id))
57
query_string = urlencode(query)
58
return urlunparse(('http', self.netloc, path, '', query_string, ''))
60
def _method_message(self, method, archive_id, message_id,
62
"""Perform an HTTP method on a message's URL."""
63
url = self.message_url(archive_id, message_id, query)
64
connection = self._get_connection()
65
connection.request(method, url, body)
66
return connection.getresponse()
68
def put_archive(self, archive_id, mbox=None):
71
:param archive_id: The archive id.
72
:param mbox: An optional mbox with messages to add to the new archive.
74
response = self._method_archive(
75
'POST', '', {'archive_id': archive_id}, None)
77
if response.status == httplib.BAD_REQUEST:
78
raise Exception('wtf')
79
elif response.status == httplib.CREATED:
58
84
def put_message(self, archive_id, key, file_obj):
59
85
"""Put a message into an archive.
64
90
:param file_obj: The raw text of the message, as a file.
92
path = '%s/%s' % (archive_id, key)
66
93
response = self._method_archive(
67
'POST', archive_id, {'key': key}, file_obj.read())
94
'POST', path, {}, file_obj.read())
69
96
if response.status == httplib.BAD_REQUEST:
70
97
raise Exception('wtf')
74
101
raise Exception('!!')
76
def get_messages(self, archive_id, message_ids=None, limit=None,
77
memo=None, order=None, headers=None,
78
max_body_length=None, include_hidden=False,
103
def get_messages(self, archive_id, message_ids=None, date_range=None,
104
limit=None, memo=None, order=None, headers=None,
105
include_hidden=False, max_body_length=None,
79
106
display_type='all'):
80
107
"""Retrieve specified messages.
82
109
:param archive_id: The archive to retrieve messages from.
83
110
:param message_ids: (optional) Retrieve only messages with these ids.
111
:param date_range: Retrieve the messages from or between a range of
112
dates. Example: 2012-01-01..2012-01-31 retrieve all the messages
113
between the 01 and 31 of January, including message from 01
84
115
:param limit: The maximum number of messages to return. The server
85
116
may, at its discretion, return fewer.
86
117
:param memo: (optional) Opaque identifier describing the position in
109
140
if message_ids is not None:
110
141
parameters['message_ids'] = message_ids
142
if date_range is not None:
143
parameters['date_range'] = date_range
111
144
if limit is not None:
112
145
parameters['limit'] = limit
113
146
if memo is not None:
127
160
raise UnsupportedOrder
128
161
elif response.reason == UnsupportedDisplayType.__doc__:
129
162
raise UnsupportedDisplayType
163
elif response.reason == UnparsableDateRange.__doc__:
164
raise UnparsableDateRange
131
166
raise ValueError('Bad request')
132
167
data = response.read()
133
168
return simplejson.loads(data)
170
def hide_message(self, archive_id, message_id, hidden):
174
query = {'parameters': simplejson.dumps(parameters)}
175
response = self._method_message('POST', archive_id, message_id, query)
176
if response.status == httplib.BAD_REQUEST:
177
if response.reason == MessageIdNotFound.__doc__:
178
raise MessageIdNotFound
179
data = response.read()
180
return simplejson.loads(data)