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.2.4
by Curtis Hovey
Added SUPPORTED_DISPLAY_TYPES. |
10 |
class UnsupportedDisplayType(Exception): |
11 |
"""Raised when an Unsupported display_type is requested."""
|
|
12 |
||
13 |
||
6.1.15
by Aaron Bentley
Test unsupported orders. |
14 |
class UnsupportedOrder(Exception): |
6.1.28
by Aaron Bentley
Cleanup |
15 |
"""Raised when an Unsupported order is requested."""
|
6.1.15
by Aaron Bentley
Test unsupported orders. |
16 |
|
6.1.1
by Aaron Bentley
Fix URLs etc. |
17 |
|
6.2.4
by Curtis Hovey
Added SUPPORTED_DISPLAY_TYPES. |
18 |
SUPPORTED_DISPLAY_TYPES = ( |
19 |
'all', |
|
20 |
'text-only', |
|
21 |
'headers-only', |
|
22 |
)
|
|
23 |
||
24 |
||
6.1.1
by Aaron Bentley
Fix URLs etc. |
25 |
class GrackleClient: |
6.1.28
by Aaron Bentley
Cleanup |
26 |
"""Class for accessing Grackle web service."""
|
6.1.1
by Aaron Bentley
Fix URLs etc. |
27 |
|
28 |
def __init__(self, host, port): |
|
6.1.28
by Aaron Bentley
Cleanup |
29 |
"""Constructor.
|
30 |
||
31 |
:param host: The name of the server.
|
|
32 |
:param port: The port providing Grackle service.
|
|
33 |
"""
|
|
6.1.1
by Aaron Bentley
Fix URLs etc. |
34 |
self.host = host |
35 |
self.port = port |
|
6.1.5
by Aaron Bentley
Start working on GET. |
36 |
self.netloc = '%s:%d' % (host, port) |
37 |
||
6.1.12
by Aaron Bentley
archive_name -> archive_id |
38 |
def archive_url(self, archive_id, query): |
6.1.28
by Aaron Bentley
Cleanup |
39 |
"""Return the URL for an archive
|
40 |
||
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.
|
|
43 |
"""
|
|
6.1.20
by Aaron Bentley
Implement an archive namespace. |
44 |
path = '/archive/%s' % quote(archive_id) |
6.1.9
by Aaron Bentley
Test filtering by message-id. |
45 |
query_string = urlencode(query) |
46 |
return urlunparse(('http', self.netloc, path, '', query_string, '')) |
|
6.1.5
by Aaron Bentley
Start working on GET. |
47 |
|
48 |
def _get_connection(self): |
|
49 |
return httplib.HTTPConnection(self.host, self.port) |
|
50 |
||
6.1.28
by Aaron Bentley
Cleanup |
51 |
def _method_archive(self, method, archive_id, query, body=None): |
52 |
"""Perform an HTTP method on an archive's URL."""
|
|
6.1.12
by Aaron Bentley
archive_name -> archive_id |
53 |
url = self.archive_url(archive_id, query) |
6.1.5
by Aaron Bentley
Start working on GET. |
54 |
connection = self._get_connection() |
6.1.28
by Aaron Bentley
Cleanup |
55 |
connection.request(method, url, body) |
6.1.5
by Aaron Bentley
Start working on GET. |
56 |
return connection.getresponse() |
57 |
||
6.1.12
by Aaron Bentley
archive_name -> archive_id |
58 |
def put_message(self, archive_id, key, file_obj): |
6.1.28
by Aaron Bentley
Cleanup |
59 |
"""Put a message into an archive.
|
60 |
||
61 |
:param archive_id: The archive to put the message into.
|
|
62 |
:param key: An arbitrary identifier that can later be used to retrieve
|
|
63 |
the message.
|
|
64 |
:param file_obj: The raw text of the message, as a file.
|
|
65 |
"""
|
|
66 |
response = self._method_archive( |
|
6.1.12
by Aaron Bentley
archive_name -> archive_id |
67 |
'POST', archive_id, {'key': key}, file_obj.read()) |
6.2.1
by Curtis Hovey
Hush lint. |
68 |
response.read() |
6.1.1
by Aaron Bentley
Fix URLs etc. |
69 |
if response.status == httplib.BAD_REQUEST: |
70 |
raise Exception('wtf') |
|
71 |
elif response.status == httplib.CREATED: |
|
72 |
return
|
|
73 |
else: |
|
74 |
raise Exception('!!') |
|
6.1.5
by Aaron Bentley
Start working on GET. |
75 |
|
6.1.13
by Aaron Bentley
Implement memo/limit support. |
76 |
def get_messages(self, archive_id, message_ids=None, limit=None, |
6.2.5
by Curtis Hovey
Moved the display_type arg. |
77 |
memo=None, order=None, headers=None, |
78 |
max_body_length=None, include_hidden=False, |
|
79 |
display_type='all'): |
|
6.1.28
by Aaron Bentley
Cleanup |
80 |
"""Retrieve specified messages.
|
81 |
||
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
|
|
89 |
is used.
|
|
90 |
:param order: The order to return results in. Supported orders are
|
|
91 |
determined by the server. See test_client.SUPPORTED_ORDERS for an
|
|
92 |
example.
|
|
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'
|
|
99 |
bodies.
|
|
100 |
:param include_hidden: If true, include messages that have been
|
|
101 |
flagged "hidden" in the results.
|
|
6.2.5
by Curtis Hovey
Moved the display_type arg. |
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.
|
|
6.1.28
by Aaron Bentley
Cleanup |
107 |
"""
|
6.1.5
by Aaron Bentley
Start working on GET. |
108 |
parameters = {} |
109 |
if message_ids is not None: |
|
6.1.7
by Aaron Bentley
Retrieve messages. |
110 |
parameters['message_ids'] = message_ids |
6.1.13
by Aaron Bentley
Implement memo/limit support. |
111 |
if limit is not None: |
112 |
parameters['limit'] = limit |
|
113 |
if memo is not None: |
|
114 |
parameters['memo'] = memo |
|
6.1.14
by Aaron Bentley
Support order by date |
115 |
if order is not None: |
116 |
parameters['order'] = order |
|
6.1.21
by Aaron Bentley
get_messages supports header parameter. |
117 |
if headers is not None: |
118 |
parameters['headers'] = headers |
|
6.2.5
by Curtis Hovey
Moved the display_type arg. |
119 |
if max_body_length is not None: |
120 |
parameters['max_body_length'] = max_body_length |
|
121 |
parameters['display_type'] = display_type |
|
6.1.23
by Aaron Bentley
implement include_hidden. |
122 |
parameters['include_hidden'] = include_hidden |
6.1.5
by Aaron Bentley
Start working on GET. |
123 |
query = {'parameters': simplejson.dumps(parameters)} |
6.1.28
by Aaron Bentley
Cleanup |
124 |
response = self._method_archive('GET', archive_id, query) |
6.1.15
by Aaron Bentley
Test unsupported orders. |
125 |
if response.status == httplib.BAD_REQUEST: |
6.2.8
by Curtis Hovey
Use the exception __doc__ to ensure client and server can match exceptions. |
126 |
if response.reason == UnsupportedOrder.__doc__: |
6.2.7
by Curtis Hovey
Moved the handling of unsupported display_type to server. |
127 |
raise UnsupportedOrder |
6.2.8
by Curtis Hovey
Use the exception __doc__ to ensure client and server can match exceptions. |
128 |
elif response.reason == UnsupportedDisplayType.__doc__: |
6.2.7
by Curtis Hovey
Moved the handling of unsupported display_type to server. |
129 |
raise UnsupportedDisplayType |
130 |
else: |
|
131 |
raise ValueError('Bad request') |
|
6.1.7
by Aaron Bentley
Retrieve messages. |
132 |
data = response.read() |
133 |
return simplejson.loads(data) |