~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-16 20:31:23 UTC
  • Revision ID: aaron@canonical.com-20120116203123-bxg29ktwtq19al75
Cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
)
8
8
 
9
9
 
10
 
class UnsupportedDisplayType(Exception):
11
 
    """Raised when an Unsupported display_type is requested."""
12
 
 
13
 
 
14
10
class UnsupportedOrder(Exception):
15
11
    """Raised when an Unsupported order is requested."""
16
12
 
17
13
 
18
 
SUPPORTED_DISPLAY_TYPES = (
19
 
    'all',
20
 
    'text-only',
21
 
    'headers-only',
22
 
    )
23
 
 
24
 
 
25
14
class GrackleClient:
26
15
    """Class for accessing Grackle web service."""
27
16
 
65
54
        """
66
55
        response = self._method_archive(
67
56
            'POST', archive_id, {'key': key}, file_obj.read())
68
 
        response.read()
 
57
        data = response.read()
69
58
        if response.status == httplib.BAD_REQUEST:
70
59
            raise Exception('wtf')
71
60
        elif response.status == httplib.CREATED:
75
64
 
76
65
    def get_messages(self, archive_id, message_ids=None, limit=None,
77
66
                     memo=None, order=None, headers=None,
78
 
                     max_body_length=None, include_hidden=False,
79
 
                     display_type='all'):
 
67
                     max_body_length=None, include_hidden=False):
80
68
        """Retrieve specified messages.
81
69
 
82
70
        :param archive_id: The archive to retrieve messages from.
99
87
            bodies.
100
88
        :param include_hidden: If true, include messages that have been
101
89
            flagged "hidden" in the results.
102
 
        :param display_type: Adjust the message content to meet the needs of
103
 
            the intended display. Valid values are:
104
 
            all: (the default) include all message content.
105
 
            text-only: include only plain/text parts; exclude all other parts.
106
 
            headers-only: include only the message headers.
107
90
        """
108
91
        parameters = {}
109
92
        if message_ids is not None:
118
101
            parameters['headers'] = headers
119
102
        if max_body_length is not None:
120
103
            parameters['max_body_length'] = max_body_length
121
 
        parameters['display_type'] = display_type
122
104
        parameters['include_hidden'] = include_hidden
123
105
        query = {'parameters': simplejson.dumps(parameters)}
124
106
        response = self._method_archive('GET', archive_id, query)
125
107
        if response.status == httplib.BAD_REQUEST:
126
 
            if response.reason == UnsupportedOrder.__doc__:
127
 
                raise UnsupportedOrder
128
 
            elif response.reason == UnsupportedDisplayType.__doc__:
129
 
                raise UnsupportedDisplayType
130
 
            else:
131
 
                raise ValueError('Bad request')
 
108
            raise UnsupportedOrder
132
109
        data = response.read()
133
110
        return simplejson.loads(data)
 
111