~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:
6
6
    urlencode,
7
7
)
8
8
 
9
 
 
10
 
class UnsupportedOrder(Exception):
11
 
    """Raised when an Unsupported order is requested."""
 
9
from grackle.error import (
 
10
    MessageIdNotFound,
 
11
    UnparsableDateRange,
 
12
    UnsupportedDisplayType,
 
13
    UnsupportedOrder,
 
14
    )
12
15
 
13
16
 
14
17
class GrackleClient:
44
47
        connection.request(method, url, body)
45
48
        return connection.getresponse()
46
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
 
 
68
    def put_archive(self, archive_id, mbox=None):
 
69
        """Create an archive.
 
70
 
 
71
        :param archive_id: The archive id.
 
72
        :param mbox: An optional mbox with messages to add to the new archive.
 
73
        """
 
74
        response = self._method_archive(
 
75
            'POST', '', {'archive_id': archive_id}, None)
 
76
        response.read()
 
77
        if response.status == httplib.BAD_REQUEST:
 
78
            raise Exception('wtf')
 
79
        elif response.status == httplib.CREATED:
 
80
            return
 
81
        else:
 
82
            raise Exception('!!')
 
83
 
47
84
    def put_message(self, archive_id, key, file_obj):
48
85
        """Put a message into an archive.
49
86
 
52
89
            the message.
53
90
        :param file_obj: The raw text of the message, as a file.
54
91
        """
 
92
        path = '%s/%s' % (archive_id, key)
55
93
        response = self._method_archive(
56
 
            'POST', archive_id, {'key': key}, file_obj.read())
 
94
            'POST', path, {}, file_obj.read())
57
95
        response.read()
58
96
        if response.status == httplib.BAD_REQUEST:
59
97
            raise Exception('wtf')
62
100
        else:
63
101
            raise Exception('!!')
64
102
 
65
 
    def get_messages(self, archive_id, message_ids=None, limit=None,
66
 
                     memo=None, order=None, headers=None,
67
 
                     max_body_length=None, include_hidden=False):
 
103
    def get_messages(self, archive_id, message_ids=None, date_range=None,
 
104
                     limit=None, memo=None, order=None, headers=None,
 
105
                     include_hidden=False, max_body_length=None,
 
106
                     display_type='all'):
68
107
        """Retrieve specified messages.
69
108
 
70
109
        :param archive_id: The archive to retrieve messages from.
71
110
        :param message_ids: (optional) Retrieve only messages with these ids.
 
111
        :param date_range: Retrieve the messages from or between a range of
 
112
            dates. Example: 2012-01-01..2012-01-31 retrieve all the messages
 
113
            between the 01 and 31 of January, including message from 01
 
114
            and 31.
72
115
        :param limit: The maximum number of messages to return.  The server
73
116
            may, at its discretion, return fewer.
74
117
        :param memo: (optional) Opaque identifier describing the position in
87
130
            bodies.
88
131
        :param include_hidden: If true, include messages that have been
89
132
            flagged "hidden" in the results.
 
133
        :param display_type: Adjust the message content to meet the needs of
 
134
            the intended display. Valid values are:
 
135
            all: (the default) include all message content.
 
136
            text-only: include only plain/text parts; exclude all other parts.
 
137
            headers-only: include only the message headers.
90
138
        """
91
139
        parameters = {}
92
140
        if message_ids is not None:
93
141
            parameters['message_ids'] = message_ids
 
142
        if date_range is not None:
 
143
            parameters['date_range'] = date_range
94
144
        if limit is not None:
95
145
            parameters['limit'] = limit
96
146
        if memo is not None:
101
151
            parameters['headers'] = headers
102
152
        if max_body_length is not None:
103
153
            parameters['max_body_length'] = max_body_length
 
154
        parameters['display_type'] = display_type
104
155
        parameters['include_hidden'] = include_hidden
105
156
        query = {'parameters': simplejson.dumps(parameters)}
106
157
        response = self._method_archive('GET', archive_id, query)
107
158
        if response.status == httplib.BAD_REQUEST:
108
 
            raise UnsupportedOrder
 
159
            if response.reason == UnsupportedOrder.__doc__:
 
160
                raise UnsupportedOrder
 
161
            elif response.reason == UnsupportedDisplayType.__doc__:
 
162
                raise UnsupportedDisplayType
 
163
            elif response.reason == UnparsableDateRange.__doc__:
 
164
                raise UnparsableDateRange
 
165
            else:
 
166
                raise ValueError('Bad request')
 
167
        data = response.read()
 
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
109
179
        data = response.read()
110
180
        return simplejson.loads(data)