~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 20:16:12 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316201612-lr7b32umqgduaja6
Added a rudimentary put_archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
)
8
8
 
9
9
from grackle.error import (
 
10
    MessageIdNotFound,
10
11
    UnparsableDateRange,
11
12
    UnsupportedDisplayType,
12
13
    UnsupportedOrder,
26
27
        self.port = port
27
28
        self.netloc = '%s:%d' % (host, port)
28
29
 
29
 
    def archive_url(self, archive_id, query):
 
30
    def archive_url(self, path, query):
30
31
        """Return the URL for an archive
31
32
 
32
 
        :param archive_id: The id of the archive to generate the URL for.
 
33
        :param path: The path to generate the URL for.
 
34
            Maybe be '', 'archive_id', or 'archive_id/message_id'
33
35
        :param query: The query to use in the URL, as a dict.
34
36
        """
35
 
        path = '/archive/%s' % quote(archive_id)
 
37
        path = '/archive/%s' % quote(path)
36
38
        query_string = urlencode(query)
37
39
        return urlunparse(('http', self.netloc, path, '', query_string, ''))
38
40
 
39
41
    def _get_connection(self):
40
42
        return httplib.HTTPConnection(self.host, self.port)
41
43
 
42
 
    def _method_archive(self, method, archive_id, query, body=None):
 
44
    def _method_archive(self, method, path, query, body=None):
43
45
        """Perform an HTTP method on an archive's URL."""
44
 
        url = self.archive_url(archive_id, query)
 
46
        url = self.archive_url(path, query)
45
47
        connection = self._get_connection()
46
48
        connection.request(method, url, body)
47
49
        return connection.getresponse()
48
50
 
 
51
    def put_archive(self, archive_id, mbox=None):
 
52
        """Create an archive.
 
53
 
 
54
        :param archive_id: The archive id.
 
55
        :param mbox: An optional mbox with messages to add to the new archive.
 
56
        """
 
57
        response = self._method_archive(
 
58
            'PUT', archive_id, {}, None)
 
59
        response.read()
 
60
        if response.status == httplib.BAD_REQUEST:
 
61
            raise Exception('wtf')
 
62
        elif response.status == httplib.CREATED:
 
63
            return
 
64
        else:
 
65
            raise Exception('!!')
 
66
 
49
67
    def put_message(self, archive_id, key, file_obj):
50
68
        """Put a message into an archive.
51
69
 
54
72
            the message.
55
73
        :param file_obj: The raw text of the message, as a file.
56
74
        """
 
75
        path = '%s/%s' % (archive_id, key)
57
76
        response = self._method_archive(
58
 
            'POST', archive_id, {'key': key}, file_obj.read())
 
77
            'PUT', path, {}, file_obj.read())
59
78
        response.read()
60
79
        if response.status == httplib.BAD_REQUEST:
61
80
            raise Exception('wtf')
130
149
                raise ValueError('Bad request')
131
150
        data = response.read()
132
151
        return simplejson.loads(data)
 
152
 
 
153
    def hide_message(self, archive_id, message_id, hidden):
 
154
        parameters = {
 
155
            'hidden': hidden,
 
156
            }
 
157
        query = {'parameters': simplejson.dumps(parameters)}
 
158
        path = '%s/%s' % (archive_id, message_id)
 
159
        response = self._method_archive('POST', path, query)
 
160
        if response.status == httplib.BAD_REQUEST:
 
161
            if response.reason == MessageIdNotFound.__doc__:
 
162
                raise MessageIdNotFound
 
163
        data = response.read()
 
164
        return simplejson.loads(data)