~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 15:12:12 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316151212-w2u06qd6oxi2cvli
Make an rfc822 message with a mime payload.

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
 
    MessageIdNotFound,
12
10
    UnparsableDateRange,
13
11
    UnsupportedDisplayType,
14
12
    UnsupportedOrder,
28
26
        self.port = port
29
27
        self.netloc = '%s:%d' % (host, port)
30
28
 
31
 
    def archive_url(self, path, query):
 
29
    def archive_url(self, archive_id, query):
32
30
        """Return the URL for an archive
33
31
 
34
 
        :param path: The path to generate the URL for.
35
 
            Maybe be '', 'archive_id', or 'archive_id/message_id'
 
32
        :param archive_id: The id of the archive to generate the URL for.
36
33
        :param query: The query to use in the URL, as a dict.
37
34
        """
38
 
        path = '/archive/%s' % quote(path)
 
35
        path = '/archive/%s' % quote(archive_id)
39
36
        query_string = urlencode(query)
40
37
        return urlunparse(('http', self.netloc, path, '', query_string, ''))
41
38
 
42
39
    def _get_connection(self):
43
40
        return httplib.HTTPConnection(self.host, self.port)
44
41
 
45
 
    def _method_archive(self, method, path, query, body=None):
 
42
    def _method_archive(self, method, archive_id, query, body=None):
46
43
        """Perform an HTTP method on an archive's URL."""
47
 
        url = self.archive_url(path, query)
 
44
        url = self.archive_url(archive_id, query)
48
45
        connection = self._get_connection()
49
46
        connection.request(method, url, body)
50
47
        return connection.getresponse()
56
53
        :param mbox: An optional mbox with messages to add to the new archive.
57
54
        """
58
55
        response = self._method_archive(
59
 
            'PUT', archive_id, {}, None)
 
56
            'POST', '', {'archive_id': archive_id}, None)
60
57
        response.read()
61
58
        if response.status == httplib.BAD_REQUEST:
62
 
            if response.reason == ArchiveIdExists.__doc__:
63
 
                raise ArchiveIdExists
64
59
            raise Exception('wtf')
65
60
        elif response.status == httplib.CREATED:
66
61
            return
77
72
        """
78
73
        path = '%s/%s' % (archive_id, key)
79
74
        response = self._method_archive(
80
 
            'PUT', path, {}, file_obj.read())
 
75
            'POST', path, {}, file_obj.read())
81
76
        response.read()
82
77
        if response.status == httplib.BAD_REQUEST:
83
 
            if response.reason == ArchiveIdExists.__doc__:
84
 
                raise ArchiveIdExists
85
78
            raise Exception('wtf')
86
79
        elif response.status == httplib.CREATED:
87
80
            return
154
147
                raise ValueError('Bad request')
155
148
        data = response.read()
156
149
        return simplejson.loads(data)
157
 
 
158
 
    def hide_message(self, archive_id, message_id, hidden):
159
 
        parameters = {
160
 
            'hidden': hidden,
161
 
            }
162
 
        query = {'parameters': simplejson.dumps(parameters)}
163
 
        path = '%s/%s' % (archive_id, message_id)
164
 
        response = self._method_archive('POST', path, query)
165
 
        if response.status == httplib.BAD_REQUEST:
166
 
            if response.reason == MessageIdNotFound.__doc__:
167
 
                raise MessageIdNotFound
168
 
        data = response.read()
169
 
        return simplejson.loads(data)