9
from grackle.error import (
11
UnsupportedDisplayType,
10
class UnsupportedOrder(Exception):
16
14
class GrackleClient:
17
"""Class for accessing Grackle web service."""
19
16
def __init__(self, host, port):
22
:param host: The name of the server.
23
:param port: The port providing Grackle service.
27
19
self.netloc = '%s:%d' % (host, port)
29
21
def archive_url(self, archive_id, query):
30
"""Return the URL for an archive
32
:param archive_id: The id of the archive to generate the URL for.
33
:param query: The query to use in the URL, as a dict.
35
22
path = '/archive/%s' % quote(archive_id)
36
23
query_string = urlencode(query)
37
24
return urlunparse(('http', self.netloc, path, '', query_string, ''))
39
26
def _get_connection(self):
40
27
return httplib.HTTPConnection(self.host, self.port)
42
def _method_archive(self, method, archive_id, query, body=None):
43
"""Perform an HTTP method on an archive's URL."""
29
def _verb_archive(self, verb, archive_id, query, body=None):
44
30
url = self.archive_url(archive_id, query)
45
31
connection = self._get_connection()
46
connection.request(method, url, body)
32
connection.request(verb, url, body)
47
33
return connection.getresponse()
49
def put_archive(self, archive_id, mbox=None):
52
:param archive_id: The archive id.
53
:param mbox: An optional mbox with messages to add to the new archive.
55
response = self._method_archive(
56
'POST', '', {'archive_id': archive_id}, None)
58
if response.status == httplib.BAD_REQUEST:
59
raise Exception('wtf')
60
elif response.status == httplib.CREATED:
65
35
def put_message(self, archive_id, key, file_obj):
66
"""Put a message into an archive.
68
:param archive_id: The archive to put the message into.
69
:param key: An arbitrary identifier that can later be used to retrieve
71
:param file_obj: The raw text of the message, as a file.
73
path = '%s/%s' % (archive_id, key)
74
response = self._method_archive(
75
'POST', path, {}, file_obj.read())
36
response = self._verb_archive(
37
'POST', archive_id, {'key': key}, file_obj.read())
38
data = response.read()
77
39
if response.status == httplib.BAD_REQUEST:
78
40
raise Exception('wtf')
79
41
elif response.status == httplib.CREATED:
82
44
raise Exception('!!')
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,
88
"""Retrieve specified messages.
90
:param archive_id: The archive to retrieve messages from.
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
96
:param limit: The maximum number of messages to return. The server
97
may, at its discretion, return fewer.
98
:param memo: (optional) Opaque identifier describing the position in
99
the list of messages to return. The combination of a memo and a
100
limit describes a batch of results. If not specified, the start
102
:param order: The order to return results in. Supported orders are
103
determined by the server. See test_client.SUPPORTED_ORDERS for an
105
:param headers: The headers to include in the message. Only headers
106
actually present in the message will be provided. If unspecified,
107
most headers will be included.
108
:param max_body_length: The maximum length for a message's body. When
109
multiple messages are nested (as with a thread), this applies to
110
each message's body, not the aggregate length of all messages'
112
:param include_hidden: If true, include messages that have been
113
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.
46
def get_messages(self, archive_id, message_ids=None, limit=None,
47
memo=None, order=None, headers=None,
48
max_body_length=None, include_hidden=False):
121
50
if message_ids is not None:
122
51
parameters['message_ids'] = message_ids
123
if date_range is not None:
124
parameters['date_range'] = date_range
125
52
if limit is not None:
126
53
parameters['limit'] = limit
127
54
if memo is not None:
132
59
parameters['headers'] = headers
133
60
if max_body_length is not None:
134
61
parameters['max_body_length'] = max_body_length
135
parameters['display_type'] = display_type
136
62
parameters['include_hidden'] = include_hidden
137
63
query = {'parameters': simplejson.dumps(parameters)}
138
response = self._method_archive('GET', archive_id, query)
64
response = self._verb_archive('GET', archive_id, query)
139
65
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
147
raise ValueError('Bad request')
66
raise UnsupportedOrder
148
67
data = response.read()
149
68
return simplejson.loads(data)