10
class UnsupportedDisplayType(Exception):
11
"""Raised when an Unsupported display_type is requested."""
14
10
class UnsupportedOrder(Exception):
15
"""Raised when an Unsupported order is requested."""
18
SUPPORTED_DISPLAY_TYPES = (
25
14
class GrackleClient:
26
"""Class for accessing Grackle web service."""
28
16
def __init__(self, host, port):
31
:param host: The name of the server.
32
:param port: The port providing Grackle service.
36
19
self.netloc = '%s:%d' % (host, port)
38
21
def archive_url(self, archive_id, query):
39
"""Return the URL for an archive
41
:param archive_id: The id of the archive to generate the URL for.
42
:param query: The query to use in the URL, as a dict.
44
22
path = '/archive/%s' % quote(archive_id)
45
23
query_string = urlencode(query)
46
24
return urlunparse(('http', self.netloc, path, '', query_string, ''))
48
26
def _get_connection(self):
49
27
return httplib.HTTPConnection(self.host, self.port)
51
def _method_archive(self, method, archive_id, query, body=None):
52
"""Perform an HTTP method on an archive's URL."""
29
def _verb_archive(self, verb, archive_id, query, body=None):
53
30
url = self.archive_url(archive_id, query)
54
31
connection = self._get_connection()
55
connection.request(method, url, body)
32
connection.request(verb, url, body)
56
33
return connection.getresponse()
58
35
def put_message(self, archive_id, key, file_obj):
59
"""Put a message into an archive.
61
:param archive_id: The archive to put the message into.
62
:param key: An arbitrary identifier that can later be used to retrieve
64
:param file_obj: The raw text of the message, as a file.
66
response = self._method_archive(
36
response = self._verb_archive(
67
37
'POST', archive_id, {'key': key}, file_obj.read())
38
data = response.read()
69
39
if response.status == httplib.BAD_REQUEST:
70
40
raise Exception('wtf')
71
41
elif response.status == httplib.CREATED:
76
46
def get_messages(self, archive_id, message_ids=None, limit=None,
77
47
memo=None, order=None, headers=None,
78
max_body_length=None, include_hidden=False,
80
"""Retrieve specified messages.
82
:param archive_id: The archive to retrieve messages from.
83
:param message_ids: (optional) Retrieve only messages with these ids.
84
:param limit: The maximum number of messages to return. The server
85
may, at its discretion, return fewer.
86
:param memo: (optional) Opaque identifier describing the position in
87
the list of messages to return. The combination of a memo and a
88
limit describes a batch of results. If not specified, the start
90
:param order: The order to return results in. Supported orders are
91
determined by the server. See test_client.SUPPORTED_ORDERS for an
93
:param headers: The headers to include in the message. Only headers
94
actually present in the message will be provided. If unspecified,
95
most headers will be included.
96
:param max_body_length: The maximum length for a message's body. When
97
multiple messages are nested (as with a thread), this applies to
98
each message's body, not the aggregate length of all messages'
100
:param include_hidden: If true, include messages that have been
101
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.
48
max_body_length=None, include_hidden=False):
109
50
if message_ids is not None:
110
51
parameters['message_ids'] = message_ids
118
59
parameters['headers'] = headers
119
60
if max_body_length is not None:
120
61
parameters['max_body_length'] = max_body_length
121
parameters['display_type'] = display_type
122
62
parameters['include_hidden'] = include_hidden
123
63
query = {'parameters': simplejson.dumps(parameters)}
124
response = self._method_archive('GET', archive_id, query)
64
response = self._verb_archive('GET', archive_id, query)
125
65
if response.status == httplib.BAD_REQUEST:
126
if response.reason == UnsupportedOrder.__doc__:
127
raise UnsupportedOrder
128
elif response.reason == UnsupportedDisplayType.__doc__:
129
raise UnsupportedDisplayType
131
raise ValueError('Bad request')
66
raise UnsupportedOrder
132
67
data = response.read()
133
68
return simplejson.loads(data)