6
by Aaron Bentley
Use constants. |
1 |
import httplib |
6.1.5
by Aaron Bentley
Start working on GET. |
2 |
import simplejson |
3 |
from urlparse import urlunparse |
|
4 |
from urllib import ( |
|
5 |
quote, |
|
6 |
urlencode, |
|
7 |
)
|
|
8 |
||
6.1.1
by Aaron Bentley
Fix URLs etc. |
9 |
|
6.1.15
by Aaron Bentley
Test unsupported orders. |
10 |
class UnsupportedOrder(Exception): |
6.1.28
by Aaron Bentley
Cleanup |
11 |
"""Raised when an Unsupported order is requested."""
|
6.1.15
by Aaron Bentley
Test unsupported orders. |
12 |
|
6.1.1
by Aaron Bentley
Fix URLs etc. |
13 |
|
14 |
class GrackleClient: |
|
6.1.28
by Aaron Bentley
Cleanup |
15 |
"""Class for accessing Grackle web service."""
|
6.1.1
by Aaron Bentley
Fix URLs etc. |
16 |
|
17 |
def __init__(self, host, port): |
|
6.1.28
by Aaron Bentley
Cleanup |
18 |
"""Constructor.
|
19 |
||
20 |
:param host: The name of the server.
|
|
21 |
:param port: The port providing Grackle service.
|
|
22 |
"""
|
|
6.1.1
by Aaron Bentley
Fix URLs etc. |
23 |
self.host = host |
24 |
self.port = port |
|
6.1.5
by Aaron Bentley
Start working on GET. |
25 |
self.netloc = '%s:%d' % (host, port) |
26 |
||
6.1.12
by Aaron Bentley
archive_name -> archive_id |
27 |
def archive_url(self, archive_id, query): |
6.1.28
by Aaron Bentley
Cleanup |
28 |
"""Return the URL for an archive
|
29 |
||
30 |
:param archive_id: The id of the archive to generate the URL for.
|
|
31 |
:param query: The query to use in the URL, as a dict.
|
|
32 |
"""
|
|
6.1.20
by Aaron Bentley
Implement an archive namespace. |
33 |
path = '/archive/%s' % quote(archive_id) |
6.1.9
by Aaron Bentley
Test filtering by message-id. |
34 |
query_string = urlencode(query) |
35 |
return urlunparse(('http', self.netloc, path, '', query_string, '')) |
|
6.1.5
by Aaron Bentley
Start working on GET. |
36 |
|
37 |
def _get_connection(self): |
|
38 |
return httplib.HTTPConnection(self.host, self.port) |
|
39 |
||
6.1.28
by Aaron Bentley
Cleanup |
40 |
def _method_archive(self, method, archive_id, query, body=None): |
41 |
"""Perform an HTTP method on an archive's URL."""
|
|
6.1.12
by Aaron Bentley
archive_name -> archive_id |
42 |
url = self.archive_url(archive_id, query) |
6.1.5
by Aaron Bentley
Start working on GET. |
43 |
connection = self._get_connection() |
6.1.28
by Aaron Bentley
Cleanup |
44 |
connection.request(method, url, body) |
6.1.5
by Aaron Bentley
Start working on GET. |
45 |
return connection.getresponse() |
46 |
||
6.1.12
by Aaron Bentley
archive_name -> archive_id |
47 |
def put_message(self, archive_id, key, file_obj): |
6.1.28
by Aaron Bentley
Cleanup |
48 |
"""Put a message into an archive.
|
49 |
||
50 |
:param archive_id: The archive to put the message into.
|
|
51 |
:param key: An arbitrary identifier that can later be used to retrieve
|
|
52 |
the message.
|
|
53 |
:param file_obj: The raw text of the message, as a file.
|
|
54 |
"""
|
|
55 |
response = self._method_archive( |
|
6.1.12
by Aaron Bentley
archive_name -> archive_id |
56 |
'POST', archive_id, {'key': key}, file_obj.read()) |
6.2.1
by Curtis Hovey
Hush lint. |
57 |
response.read() |
6.1.1
by Aaron Bentley
Fix URLs etc. |
58 |
if response.status == httplib.BAD_REQUEST: |
59 |
raise Exception('wtf') |
|
60 |
elif response.status == httplib.CREATED: |
|
61 |
return
|
|
62 |
else: |
|
63 |
raise Exception('!!') |
|
6.1.5
by Aaron Bentley
Start working on GET. |
64 |
|
6.1.13
by Aaron Bentley
Implement memo/limit support. |
65 |
def get_messages(self, archive_id, message_ids=None, limit=None, |
6.1.22
by Aaron Bentley
Extract GrackleStore. |
66 |
memo=None, order=None, headers=None, |
6.1.23
by Aaron Bentley
implement include_hidden. |
67 |
max_body_length=None, include_hidden=False): |
6.1.28
by Aaron Bentley
Cleanup |
68 |
"""Retrieve specified messages.
|
69 |
||
70 |
:param archive_id: The archive to retrieve messages from.
|
|
71 |
:param message_ids: (optional) Retrieve only messages with these ids.
|
|
72 |
:param limit: The maximum number of messages to return. The server
|
|
73 |
may, at its discretion, return fewer.
|
|
74 |
:param memo: (optional) Opaque identifier describing the position in
|
|
75 |
the list of messages to return. The combination of a memo and a
|
|
76 |
limit describes a batch of results. If not specified, the start
|
|
77 |
is used.
|
|
78 |
:param order: The order to return results in. Supported orders are
|
|
79 |
determined by the server. See test_client.SUPPORTED_ORDERS for an
|
|
80 |
example.
|
|
81 |
:param headers: The headers to include in the message. Only headers
|
|
82 |
actually present in the message will be provided. If unspecified,
|
|
83 |
most headers will be included.
|
|
84 |
:param max_body_length: The maximum length for a message's body. When
|
|
85 |
multiple messages are nested (as with a thread), this applies to
|
|
86 |
each message's body, not the aggregate length of all messages'
|
|
87 |
bodies.
|
|
88 |
:param include_hidden: If true, include messages that have been
|
|
89 |
flagged "hidden" in the results.
|
|
90 |
"""
|
|
6.1.5
by Aaron Bentley
Start working on GET. |
91 |
parameters = {} |
92 |
if message_ids is not None: |
|
6.1.7
by Aaron Bentley
Retrieve messages. |
93 |
parameters['message_ids'] = message_ids |
6.1.13
by Aaron Bentley
Implement memo/limit support. |
94 |
if limit is not None: |
95 |
parameters['limit'] = limit |
|
96 |
if memo is not None: |
|
97 |
parameters['memo'] = memo |
|
6.1.14
by Aaron Bentley
Support order by date |
98 |
if order is not None: |
99 |
parameters['order'] = order |
|
6.1.21
by Aaron Bentley
get_messages supports header parameter. |
100 |
if headers is not None: |
101 |
parameters['headers'] = headers |
|
6.1.22
by Aaron Bentley
Extract GrackleStore. |
102 |
if max_body_length is not None: |
103 |
parameters['max_body_length'] = max_body_length |
|
6.1.23
by Aaron Bentley
implement include_hidden. |
104 |
parameters['include_hidden'] = include_hidden |
6.1.5
by Aaron Bentley
Start working on GET. |
105 |
query = {'parameters': simplejson.dumps(parameters)} |
6.1.28
by Aaron Bentley
Cleanup |
106 |
response = self._method_archive('GET', archive_id, query) |
6.1.15
by Aaron Bentley
Test unsupported orders. |
107 |
if response.status == httplib.BAD_REQUEST: |
108 |
raise UnsupportedOrder |
|
6.1.7
by Aaron Bentley
Retrieve messages. |
109 |
data = response.read() |
110 |
return simplejson.loads(data) |