44
47
connection.request(method, url, body)
45
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:
47
84
def put_message(self, archive_id, key, file_obj):
48
85
"""Put a message into an archive.
53
90
:param file_obj: The raw text of the message, as a file.
92
path = '%s/%s' % (archive_id, key)
55
93
response = self._method_archive(
56
'POST', archive_id, {'key': key}, file_obj.read())
94
'POST', path, {}, file_obj.read())
58
96
if response.status == httplib.BAD_REQUEST:
59
97
raise Exception('wtf')
63
101
raise Exception('!!')
65
def get_messages(self, archive_id, message_ids=None, limit=None,
66
memo=None, order=None, headers=None,
67
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,
68
107
"""Retrieve specified messages.
70
109
:param archive_id: The archive to retrieve messages from.
71
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
72
115
:param limit: The maximum number of messages to return. The server
73
116
may, at its discretion, return fewer.
74
117
:param memo: (optional) Opaque identifier describing the position in
88
131
:param include_hidden: If true, include messages that have been
89
132
flagged "hidden" in the results.
133
:param display_type: Adjust the message content to meet the needs of
134
the intended display. Valid values are:
135
all: (the default) include all message content.
136
text-only: include only plain/text parts; exclude all other parts.
137
headers-only: include only the message headers.
92
140
if message_ids is not None:
93
141
parameters['message_ids'] = message_ids
142
if date_range is not None:
143
parameters['date_range'] = date_range
94
144
if limit is not None:
95
145
parameters['limit'] = limit
96
146
if memo is not None:
101
151
parameters['headers'] = headers
102
152
if max_body_length is not None:
103
153
parameters['max_body_length'] = max_body_length
154
parameters['display_type'] = display_type
104
155
parameters['include_hidden'] = include_hidden
105
156
query = {'parameters': simplejson.dumps(parameters)}
106
157
response = self._method_archive('GET', archive_id, query)
107
158
if response.status == httplib.BAD_REQUEST:
108
raise UnsupportedOrder
159
if response.reason == UnsupportedOrder.__doc__:
160
raise UnsupportedOrder
161
elif response.reason == UnsupportedDisplayType.__doc__:
162
raise UnsupportedDisplayType
163
elif response.reason == UnparsableDateRange.__doc__:
164
raise UnparsableDateRange
166
raise ValueError('Bad request')
167
data = response.read()
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
109
179
data = response.read()
110
180
return simplejson.loads(data)