~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-02-24 21:31:01 UTC
  • Revision ID: curtis.hovey@canonical.com-20120224213101-jac3b2aj4nd5uwpj
Separate gracle MemoryStore to its own module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
)
8
8
 
9
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
 
10
18
class UnsupportedOrder(Exception):
11
19
    """Raised when an Unsupported order is requested."""
12
20
 
13
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',
 
30
    )
 
31
 
 
32
 
14
33
class GrackleClient:
15
34
    """Class for accessing Grackle web service."""
16
35
 
54
73
        """
55
74
        response = self._method_archive(
56
75
            'POST', archive_id, {'key': key}, file_obj.read())
57
 
        data = response.read()
 
76
        response.read()
58
77
        if response.status == httplib.BAD_REQUEST:
59
78
            raise Exception('wtf')
60
79
        elif response.status == httplib.CREATED:
62
81
        else:
63
82
            raise Exception('!!')
64
83
 
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):
 
84
    def get_messages(self, archive_id, message_ids=None, date_range=None,
 
85
                     limit=None, memo=None, order=None, headers=None,
 
86
                     include_hidden=False, max_body_length=None,
 
87
                     display_type='all'):
68
88
        """Retrieve specified messages.
69
89
 
70
90
        :param archive_id: The archive to retrieve messages from.
71
91
        :param message_ids: (optional) Retrieve only messages with these ids.
 
92
        :param date_range: Retrieve the messages from or between a range of
 
93
            dates. Example: 2012-01-01..2012-01-31 retrieve all the messages
 
94
            between the 01 and 31 of January, including message from 01
 
95
            and 31.
72
96
        :param limit: The maximum number of messages to return.  The server
73
97
            may, at its discretion, return fewer.
74
98
        :param memo: (optional) Opaque identifier describing the position in
87
111
            bodies.
88
112
        :param include_hidden: If true, include messages that have been
89
113
            flagged "hidden" in the results.
 
114
        :param display_type: Adjust the message content to meet the needs of
 
115
            the intended display. Valid values are:
 
116
            all: (the default) include all message content.
 
117
            text-only: include only plain/text parts; exclude all other parts.
 
118
            headers-only: include only the message headers.
90
119
        """
91
120
        parameters = {}
92
121
        if message_ids is not None:
93
122
            parameters['message_ids'] = message_ids
 
123
        if date_range is not None:
 
124
            parameters['date_range'] = date_range
94
125
        if limit is not None:
95
126
            parameters['limit'] = limit
96
127
        if memo is not None:
101
132
            parameters['headers'] = headers
102
133
        if max_body_length is not None:
103
134
            parameters['max_body_length'] = max_body_length
 
135
        parameters['display_type'] = display_type
104
136
        parameters['include_hidden'] = include_hidden
105
137
        query = {'parameters': simplejson.dumps(parameters)}
106
138
        response = self._method_archive('GET', archive_id, query)
107
139
        if response.status == httplib.BAD_REQUEST:
108
 
            raise UnsupportedOrder
 
140
            if response.reason == UnsupportedOrder.__doc__:
 
141
                raise UnsupportedOrder
 
142
            elif response.reason == UnsupportedDisplayType.__doc__:
 
143
                raise UnsupportedDisplayType
 
144
            elif response.reason == UnparsableDateRange.__doc__:
 
145
                raise UnparsableDateRange
 
146
            else:
 
147
                raise ValueError('Bad request')
109
148
        data = response.read()
110
149
        return simplejson.loads(data)
111
150
 
 
151
    def hide_message(self, archive_id, message_id, hidden):
 
152
        parameters = {
 
153
            'message_id': message_id,
 
154
            'hidden': hidden,
 
155
            }
 
156
        query = {'parameters': simplejson.dumps(parameters)}
 
157
        response = self._method_archive('POST', archive_id, query)
 
158
        if response.status == httplib.BAD_REQUEST:
 
159
            if response.reason == MessageIdNotFound.__doc__:
 
160
                raise MessageIdNotFound
 
161
        data = response.read()
 
162
        return simplejson.loads(data)