~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-01 01:08:15 UTC
  • Revision ID: curtis.hovey@canonical.com-20120301010815-hz0al69e3j0c9e6n
Raise ArchiveIdNotFound if the client puts a message into an unknown 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,
11
10
    UnparsableDateRange,
12
11
    UnsupportedDisplayType,
13
12
    UnsupportedOrder,
27
26
        self.port = port
28
27
        self.netloc = '%s:%d' % (host, port)
29
28
 
30
 
    def archive_url(self, path, query):
 
29
    def archive_url(self, archive_id, query):
31
30
        """Return the URL for an archive
32
31
 
33
 
        :param path: The path to generate the URL for.
34
 
            Maybe be '', 'archive_id', or 'archive_id/message_id'
 
32
        :param archive_id: The id of the archive to generate the URL for.
35
33
        :param query: The query to use in the URL, as a dict.
36
34
        """
37
 
        path = '/archive/%s' % quote(path)
 
35
        path = '/archive/%s' % quote(archive_id)
38
36
        query_string = urlencode(query)
39
37
        return urlunparse(('http', self.netloc, path, '', query_string, ''))
40
38
 
41
39
    def _get_connection(self):
42
40
        return httplib.HTTPConnection(self.host, self.port)
43
41
 
44
 
    def _method_archive(self, method, path, query, body=None):
 
42
    def _method_archive(self, method, archive_id, query, body=None):
45
43
        """Perform an HTTP method on an archive's URL."""
46
 
        url = self.archive_url(path, query)
 
44
        url = self.archive_url(archive_id, query)
47
45
        connection = self._get_connection()
48
46
        connection.request(method, url, body)
49
47
        return connection.getresponse()
74
72
        """
75
73
        path = '%s/%s' % (archive_id, key)
76
74
        response = self._method_archive(
77
 
            'PUT', path, {}, file_obj.read())
 
75
            'POST', path, {}, file_obj.read())
78
76
        response.read()
79
77
        if response.status == httplib.BAD_REQUEST:
80
78
            raise Exception('wtf')
149
147
                raise ValueError('Bad request')
150
148
        data = response.read()
151
149
        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)