~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-01-30 18:30:54 UTC
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: curtis.hovey@canonical.com-20120130183054-lyjgaxcekr8efuou
Hush lint.

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:
46
44
        connection.request(method, url, body)
47
45
        return connection.getresponse()
48
46
 
49
 
    def put_archive(self, archive_id, mbox=None):
50
 
        """Create an archive.
51
 
 
52
 
        :param archive_id: The archive id.
53
 
        :param mbox: An optional mbox with messages to add to the new archive.
54
 
        """
55
 
        response = self._method_archive(
56
 
            'POST', '', {'archive_id': archive_id}, None)
57
 
        response.read()
58
 
        if response.status == httplib.BAD_REQUEST:
59
 
            raise Exception('wtf')
60
 
        elif response.status == httplib.CREATED:
61
 
            return
62
 
        else:
63
 
            raise Exception('!!')
64
 
 
65
47
    def put_message(self, archive_id, key, file_obj):
66
48
        """Put a message into an archive.
67
49
 
70
52
            the message.
71
53
        :param file_obj: The raw text of the message, as a file.
72
54
        """
73
 
        path = '%s/%s' % (archive_id, key)
74
55
        response = self._method_archive(
75
 
            'POST', path, {}, file_obj.read())
 
56
            'POST', archive_id, {'key': key}, file_obj.read())
76
57
        response.read()
77
58
        if response.status == httplib.BAD_REQUEST:
78
59
            raise Exception('wtf')
81
62
        else:
82
63
            raise Exception('!!')
83
64
 
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'):
 
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):
88
68
        """Retrieve specified messages.
89
69
 
90
70
        :param archive_id: The archive to retrieve messages from.
91
71
        :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.
96
72
        :param limit: The maximum number of messages to return.  The server
97
73
            may, at its discretion, return fewer.
98
74
        :param memo: (optional) Opaque identifier describing the position in
111
87
            bodies.
112
88
        :param include_hidden: If true, include messages that have been
113
89
            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.
119
90
        """
120
91
        parameters = {}
121
92
        if message_ids is not None:
122
93
            parameters['message_ids'] = message_ids
123
 
        if date_range is not None:
124
 
            parameters['date_range'] = date_range
125
94
        if limit is not None:
126
95
            parameters['limit'] = limit
127
96
        if memo is not None:
132
101
            parameters['headers'] = headers
133
102
        if max_body_length is not None:
134
103
            parameters['max_body_length'] = max_body_length
135
 
        parameters['display_type'] = display_type
136
104
        parameters['include_hidden'] = include_hidden
137
105
        query = {'parameters': simplejson.dumps(parameters)}
138
106
        response = self._method_archive('GET', archive_id, query)
139
107
        if response.status == httplib.BAD_REQUEST:
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')
 
108
            raise UnsupportedOrder
148
109
        data = response.read()
149
110
        return simplejson.loads(data)