~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 19:01:21 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316190121-3jlqy4oy51a67rj9
Added support for hide_message.

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,
46
47
        connection.request(method, url, body)
47
48
        return connection.getresponse()
48
49
 
 
50
    def message_url(self, archive_id, message_id, query):
 
51
        """Return the URL for a message
 
52
 
 
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.
 
55
        """
 
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, ''))
 
59
 
 
60
    def _method_message(self, method, archive_id, message_id,
 
61
                        query, body=None):
 
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()
 
67
 
49
68
    def put_archive(self, archive_id, mbox=None):
50
69
        """Create an archive.
51
70
 
147
166
                raise ValueError('Bad request')
148
167
        data = response.read()
149
168
        return simplejson.loads(data)
 
169
 
 
170
    def hide_message(self, archive_id, message_id, hidden):
 
171
        parameters = {
 
172
            'hidden': hidden,
 
173
            }
 
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
 
179
        data = response.read()
 
180
        return simplejson.loads(data)