~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-02-14 22:52:55 UTC
  • Revision ID: curtis.hovey@canonical.com-20120214225255-7cwc33qlpmaztebo
Dates must be a value.

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
 
    MessageIdNotFound,
11
 
    UnparsableDateRange,
12
 
    UnsupportedDisplayType,
13
 
    UnsupportedOrder,
 
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
 
 
18
class UnsupportedOrder(Exception):
 
19
    """Raised when an Unsupported order is requested."""
 
20
 
 
21
 
 
22
SUPPORTED_DISPLAY_TYPES = (
 
23
    'all',
 
24
    'text-only',
 
25
    'headers-only',
14
26
    )
15
27
 
16
28
 
47
59
        connection.request(method, url, body)
48
60
        return connection.getresponse()
49
61
 
50
 
    def message_url(self, archive_id, message_id, query):
51
 
        """Return the URL for a message
52
 
 
53
 
        :param archive_id: The id of the archive to generate the URL for.
54
 
        :param query: The query to use in the URL, as a dict.
55
 
        """
56
 
        path = '/archive/%s/%s' % (quote(archive_id), quote(message_id))
57
 
        query_string = urlencode(query)
58
 
        return urlunparse(('http', self.netloc, path, '', query_string, ''))
59
 
 
60
 
    def _method_message(self, method, archive_id, message_id,
61
 
                        query, body=None):
62
 
        """Perform an HTTP method on a message's URL."""
63
 
        url = self.message_url(archive_id, message_id, query)
64
 
        connection = self._get_connection()
65
 
        connection.request(method, url, body)
66
 
        return connection.getresponse()
67
 
 
68
 
    def put_archive(self, archive_id, mbox=None):
69
 
        """Create an archive.
70
 
 
71
 
        :param archive_id: The archive id.
72
 
        :param mbox: An optional mbox with messages to add to the new archive.
73
 
        """
74
 
        response = self._method_archive(
75
 
            'POST', '', {'archive_id': archive_id}, None)
76
 
        response.read()
77
 
        if response.status == httplib.BAD_REQUEST:
78
 
            raise Exception('wtf')
79
 
        elif response.status == httplib.CREATED:
80
 
            return
81
 
        else:
82
 
            raise Exception('!!')
83
 
 
84
62
    def put_message(self, archive_id, key, file_obj):
85
63
        """Put a message into an archive.
86
64
 
89
67
            the message.
90
68
        :param file_obj: The raw text of the message, as a file.
91
69
        """
92
 
        path = '%s/%s' % (archive_id, key)
93
70
        response = self._method_archive(
94
 
            'POST', path, {}, file_obj.read())
 
71
            'POST', archive_id, {'key': key}, file_obj.read())
95
72
        response.read()
96
73
        if response.status == httplib.BAD_REQUEST:
97
74
            raise Exception('wtf')
166
143
                raise ValueError('Bad request')
167
144
        data = response.read()
168
145
        return simplejson.loads(data)
169
 
 
170
 
    def hide_message(self, archive_id, message_id, hidden):
171
 
        parameters = {
172
 
            'hidden': hidden,
173
 
            }
174
 
        query = {'parameters': simplejson.dumps(parameters)}
175
 
        response = self._method_message('POST', archive_id, message_id, query)
176
 
        if response.status == httplib.BAD_REQUEST:
177
 
            if response.reason == MessageIdNotFound.__doc__:
178
 
                raise MessageIdNotFound
179
 
        data = response.read()
180
 
        return simplejson.loads(data)