~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: William Grant
  • Date: 2012-01-25 06:19:56 UTC
  • Revision ID: william.grant@canonical.com-20120125061956-4tltjt6a4xf5yufj
Fix test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
    urlencode,
7
7
)
8
8
 
9
 
from grackle.error import (
10
 
    UnparsableDateRange,
11
 
    UnsupportedDisplayType,
12
 
    UnsupportedOrder,
13
 
    )
 
9
 
 
10
class UnsupportedOrder(Exception):
 
11
    """Raised when an Unsupported order is requested."""
14
12
 
15
13
 
16
14
class GrackleClient:
56
54
        """
57
55
        response = self._method_archive(
58
56
            'POST', archive_id, {'key': key}, file_obj.read())
59
 
        response.read()
 
57
        data = response.read()
60
58
        if response.status == httplib.BAD_REQUEST:
61
59
            raise Exception('wtf')
62
60
        elif response.status == httplib.CREATED:
64
62
        else:
65
63
            raise Exception('!!')
66
64
 
67
 
    def get_messages(self, archive_id, message_ids=None, date_range=None,
68
 
                     limit=None, memo=None, order=None, headers=None,
69
 
                     include_hidden=False, max_body_length=None,
70
 
                     display_type='all'):
 
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):
71
68
        """Retrieve specified messages.
72
69
 
73
70
        :param archive_id: The archive to retrieve messages from.
74
71
        :param message_ids: (optional) Retrieve only messages with these ids.
75
 
        :param date_range: Retrieve the messages from or between a range of
76
 
            dates. Example: 2012-01-01..2012-01-31 retrieve all the messages
77
 
            between the 01 and 31 of January, including message from 01
78
 
            and 31.
79
72
        :param limit: The maximum number of messages to return.  The server
80
73
            may, at its discretion, return fewer.
81
74
        :param memo: (optional) Opaque identifier describing the position in
94
87
            bodies.
95
88
        :param include_hidden: If true, include messages that have been
96
89
            flagged "hidden" in the results.
97
 
        :param display_type: Adjust the message content to meet the needs of
98
 
            the intended display. Valid values are:
99
 
            all: (the default) include all message content.
100
 
            text-only: include only plain/text parts; exclude all other parts.
101
 
            headers-only: include only the message headers.
102
90
        """
103
91
        parameters = {}
104
92
        if message_ids is not None:
105
93
            parameters['message_ids'] = message_ids
106
 
        if date_range is not None:
107
 
            parameters['date_range'] = date_range
108
94
        if limit is not None:
109
95
            parameters['limit'] = limit
110
96
        if memo is not None:
115
101
            parameters['headers'] = headers
116
102
        if max_body_length is not None:
117
103
            parameters['max_body_length'] = max_body_length
118
 
        parameters['display_type'] = display_type
119
104
        parameters['include_hidden'] = include_hidden
120
105
        query = {'parameters': simplejson.dumps(parameters)}
121
106
        response = self._method_archive('GET', archive_id, query)
122
107
        if response.status == httplib.BAD_REQUEST:
123
 
            if response.reason == UnsupportedOrder.__doc__:
124
 
                raise UnsupportedOrder
125
 
            elif response.reason == UnsupportedDisplayType.__doc__:
126
 
                raise UnsupportedDisplayType
127
 
            elif response.reason == UnparsableDateRange.__doc__:
128
 
                raise UnparsableDateRange
129
 
            else:
130
 
                raise ValueError('Bad request')
 
108
            raise UnsupportedOrder
131
109
        data = response.read()
132
110
        return simplejson.loads(data)
 
111