29
28
self.netloc = '%s:%d' % (host, port)
31
def archive_url(self, path, query):
30
def archive_url(self, archive_id, query):
32
31
"""Return the URL for an archive
34
:param path: The path to generate the URL for.
35
Maybe be '', 'archive_id', or 'archive_id/message_id'
33
:param archive_id: The id of the archive to generate the URL for.
36
34
:param query: The query to use in the URL, as a dict.
38
path = '/archive/%s' % quote(path)
36
path = '/archive/%s' % quote(archive_id)
39
37
query_string = urlencode(query)
40
38
return urlunparse(('http', self.netloc, path, '', query_string, ''))
42
40
def _get_connection(self):
43
41
return httplib.HTTPConnection(self.host, self.port)
45
def _method_archive(self, method, path, query, body=None):
43
def _method_archive(self, method, archive_id, query, body=None):
46
44
"""Perform an HTTP method on an archive's URL."""
47
url = self.archive_url(path, query)
45
url = self.archive_url(archive_id, query)
48
46
connection = self._get_connection()
49
47
connection.request(method, url, body)
50
48
return connection.getresponse()
56
54
:param mbox: An optional mbox with messages to add to the new archive.
58
56
response = self._method_archive(
59
'PUT', archive_id, {}, None)
57
'POST', '', {'archive_id': archive_id}, None)
61
59
if response.status == httplib.BAD_REQUEST:
62
if response.reason == ArchiveIdExists.__doc__:
64
60
raise Exception('wtf')
65
61
elif response.status == httplib.CREATED:
78
74
path = '%s/%s' % (archive_id, key)
79
75
response = self._method_archive(
80
'PUT', path, {}, file_obj.read())
76
'POST', path, {}, file_obj.read())
82
78
if response.status == httplib.BAD_REQUEST:
83
if response.reason == ArchiveIdExists.__doc__:
85
79
raise Exception('wtf')
86
80
elif response.status == httplib.CREATED: