~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 19:37:09 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316193709-oa33ido3h6hpo0bu
Factor-out message methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
)
8
8
 
9
9
from grackle.error import (
10
 
    ArchiveIdExists,
11
10
    MessageIdNotFound,
12
11
    UnparsableDateRange,
13
12
    UnsupportedDisplayType,
28
27
        self.port = port
29
28
        self.netloc = '%s:%d' % (host, port)
30
29
 
31
 
    def archive_url(self, path, query):
 
30
    def archive_url(self, archive_id, query):
32
31
        """Return the URL for an archive
33
32
 
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.
37
35
        """
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, ''))
41
39
 
42
40
    def _get_connection(self):
43
41
        return httplib.HTTPConnection(self.host, self.port)
44
42
 
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.
57
55
        """
58
56
        response = self._method_archive(
59
 
            'PUT', archive_id, {}, None)
 
57
            'POST', '', {'archive_id': archive_id}, None)
60
58
        response.read()
61
59
        if response.status == httplib.BAD_REQUEST:
62
 
            if response.reason == ArchiveIdExists.__doc__:
63
 
                raise ArchiveIdExists
64
60
            raise Exception('wtf')
65
61
        elif response.status == httplib.CREATED:
66
62
            return
77
73
        """
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())
81
77
        response.read()
82
78
        if response.status == httplib.BAD_REQUEST:
83
 
            if response.reason == ArchiveIdExists.__doc__:
84
 
                raise ArchiveIdExists
85
79
            raise Exception('wtf')
86
80
        elif response.status == httplib.CREATED:
87
81
            return