~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 19:38:35 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316193835-egu5tc1n0xlr5udj
Rename args to be honest about what is expected.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
    urlencode,
7
7
)
8
8
 
9
 
 
10
 
class UnparsableDateRange(Exception):
11
 
    """The date_range was not in the format of 2012-01-01..2012-01-31."""
12
 
 
13
 
 
14
 
class UnsupportedDisplayType(Exception):
15
 
    """Raised when an Unsupported display_type is requested."""
16
 
 
17
 
 
18
 
class UnsupportedOrder(Exception):
19
 
    """Raised when an Unsupported order is requested."""
20
 
 
21
 
 
22
 
class MessageIdNotFound(Exception):
23
 
    """No message matching the message_id was found in the archive."""
24
 
 
25
 
 
26
 
SUPPORTED_DISPLAY_TYPES = (
27
 
    'all',
28
 
    'text-only',
29
 
    'headers-only',
 
9
from grackle.error import (
 
10
    MessageIdNotFound,
 
11
    UnparsableDateRange,
 
12
    UnsupportedDisplayType,
 
13
    UnsupportedOrder,
30
14
    )
31
15
 
32
16
 
43
27
        self.port = port
44
28
        self.netloc = '%s:%d' % (host, port)
45
29
 
46
 
    def archive_url(self, archive_id, query):
 
30
    def archive_url(self, path, query):
47
31
        """Return the URL for an archive
48
32
 
49
 
        :param archive_id: The id of the archive to generate the URL for.
 
33
        :param oath: The path to generate the URL for.
50
34
        :param query: The query to use in the URL, as a dict.
51
35
        """
52
 
        path = '/archive/%s' % quote(archive_id)
 
36
        path = '/archive/%s' % quote(path)
53
37
        query_string = urlencode(query)
54
38
        return urlunparse(('http', self.netloc, path, '', query_string, ''))
55
39
 
56
40
    def _get_connection(self):
57
41
        return httplib.HTTPConnection(self.host, self.port)
58
42
 
59
 
    def _method_archive(self, method, archive_id, query, body=None):
 
43
    def _method_archive(self, method, path, query, body=None):
60
44
        """Perform an HTTP method on an archive's URL."""
61
 
        url = self.archive_url(archive_id, query)
 
45
        url = self.archive_url(path, query)
62
46
        connection = self._get_connection()
63
47
        connection.request(method, url, body)
64
48
        return connection.getresponse()
65
49
 
 
50
    def put_archive(self, archive_id, mbox=None):
 
51
        """Create an archive.
 
52
 
 
53
        :param archive_id: The archive id.
 
54
        :param mbox: An optional mbox with messages to add to the new archive.
 
55
        """
 
56
        response = self._method_archive(
 
57
            'POST', '', {'archive_id': archive_id}, None)
 
58
        response.read()
 
59
        if response.status == httplib.BAD_REQUEST:
 
60
            raise Exception('wtf')
 
61
        elif response.status == httplib.CREATED:
 
62
            return
 
63
        else:
 
64
            raise Exception('!!')
 
65
 
66
66
    def put_message(self, archive_id, key, file_obj):
67
67
        """Put a message into an archive.
68
68
 
71
71
            the message.
72
72
        :param file_obj: The raw text of the message, as a file.
73
73
        """
 
74
        path = '%s/%s' % (archive_id, key)
74
75
        response = self._method_archive(
75
 
            'POST', archive_id, {'key': key}, file_obj.read())
 
76
            'POST', path, {}, file_obj.read())
76
77
        response.read()
77
78
        if response.status == httplib.BAD_REQUEST:
78
79
            raise Exception('wtf')
147
148
                raise ValueError('Bad request')
148
149
        data = response.read()
149
150
        return simplejson.loads(data)
 
151
 
 
152
    def hide_message(self, archive_id, message_id, hidden):
 
153
        parameters = {
 
154
            'hidden': hidden,
 
155
            }
 
156
        query = {'parameters': simplejson.dumps(parameters)}
 
157
        path = '%s/%s' % (archive_id, message_id)
 
158
        response = self._method_archive('POST', path, query)
 
159
        if response.status == httplib.BAD_REQUEST:
 
160
            if response.reason == MessageIdNotFound.__doc__:
 
161
                raise MessageIdNotFound
 
162
        data = response.read()
 
163
        return simplejson.loads(data)