~unity-2d-team/unity-2d/Shell-MultiMonitor

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_id, query):
        path = '/%s' % quote(archive_id)
        query_string = urlencode(query)
        return urlunparse(('http', self.netloc, path, '', query_string, ''))

    def _get_connection(self):
        return httplib.HTTPConnection(self.host, self.port)

    def _verb_archive(self, verb, archive_id, query, body=None):
        url = self.archive_url(archive_id, query)
        connection = self._get_connection()
        connection.request(verb, url, body)
        return connection.getresponse()

    def put_message(self, archive_id, key, file_obj):
        response = self._verb_archive(
            'POST', archive_id, {'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_id, 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_id, query)
        data = response.read()
        return simplejson.loads(data)