~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 16:12:13 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316161213-htrw58db1ojtl8d9
Always use an rfc822 message in tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
    urlencode,
7
7
)
8
8
 
9
 
 
10
 
class UnsupportedDisplayType(Exception):
11
 
    """Raised when an Unsupported display_type is requested."""
12
 
 
13
 
 
14
 
class UnsupportedOrder(Exception):
15
 
    """Raised when an Unsupported order is requested."""
16
 
 
17
 
 
18
 
SUPPORTED_DISPLAY_TYPES = (
19
 
    'all',
20
 
    'text-only',
21
 
    'headers-only',
 
9
from grackle.error import (
 
10
    UnparsableDateRange,
 
11
    UnsupportedDisplayType,
 
12
    UnsupportedOrder,
22
13
    )
23
14
 
24
15
 
55
46
        connection.request(method, url, body)
56
47
        return connection.getresponse()
57
48
 
 
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
 
58
65
    def put_message(self, archive_id, key, file_obj):
59
66
        """Put a message into an archive.
60
67
 
63
70
            the message.
64
71
        :param file_obj: The raw text of the message, as a file.
65
72
        """
 
73
        path = '%s/%s' % (archive_id, key)
66
74
        response = self._method_archive(
67
 
            'POST', archive_id, {'key': key}, file_obj.read())
 
75
            'POST', path, {}, file_obj.read())
68
76
        response.read()
69
77
        if response.status == httplib.BAD_REQUEST:
70
78
            raise Exception('wtf')
73
81
        else:
74
82
            raise Exception('!!')
75
83
 
76
 
    def get_messages(self, archive_id, message_ids=None, limit=None,
77
 
                     memo=None, order=None, headers=None,
78
 
                     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,
79
87
                     display_type='all'):
80
88
        """Retrieve specified messages.
81
89
 
82
90
        :param archive_id: The archive to retrieve messages from.
83
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.
84
96
        :param limit: The maximum number of messages to return.  The server
85
97
            may, at its discretion, return fewer.
86
98
        :param memo: (optional) Opaque identifier describing the position in
108
120
        parameters = {}
109
121
        if message_ids is not None:
110
122
            parameters['message_ids'] = message_ids
 
123
        if date_range is not None:
 
124
            parameters['date_range'] = date_range
111
125
        if limit is not None:
112
126
            parameters['limit'] = limit
113
127
        if memo is not None:
127
141
                raise UnsupportedOrder
128
142
            elif response.reason == UnsupportedDisplayType.__doc__:
129
143
                raise UnsupportedDisplayType
 
144
            elif response.reason == UnparsableDateRange.__doc__:
 
145
                raise UnparsableDateRange
130
146
            else:
131
147
                raise ValueError('Bad request')
132
148
        data = response.read()