1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import httplib
import simplejson
from urlparse import urlunparse
from urllib import (
quote,
urlencode,
)
class GrackleClient:
def __init__(self, host, port):
self.host = host
self.port = port
self.netloc = '%s:%d' % (host, port)
def archive_url(self, archive_name, query):
path = '/%s' % quote(archive_name)
query = urlencode(query)
url = urlunparse(('http', self.netloc, path, '', '', ''))
def _get_connection(self):
print 'Connecting to %s' % self.port
return httplib.HTTPConnection(self.host, self.port)
def _verb_archive(self, verb, archive_name, query, body=None):
url = self.archive_url(archive_name, query)
connection = self._get_connection()
connection.request(verb, url, body)
return connection.getresponse()
def put_message(self, archive_name, key, file_obj):
response = self._verb_archive(
'POST', archive_name, {'key': key}, file_obj.read())
data = response.read()
if response.status == httplib.BAD_REQUEST:
raise Exception('wtf')
elif response.status == httplib.CREATED:
return
else:
raise Exception('!!')
def get_messages(self, archive_name, message_ids=None):
parameters = {}
if message_ids is not None:
parameters[message_ids] = message_ids
query = {'parameters': simplejson.dumps(parameters)}
response = self._verb_archive('GET', archive_name, query)
return {}
|