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

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
10
11
class GrackleClient:
12
13
    def __init__(self, host, port):
14
        self.host = host
15
        self.port = port
6.1.5 by Aaron Bentley
Start working on GET.
16
        self.netloc = '%s:%d' % (host, port)
17
18
    def archive_url(self, archive_name, query):
19
        path = '/%s' % quote(archive_name)
20
        query = urlencode(query)
21
        url = urlunparse(('http', self.netloc, path, '', '', ''))
22
23
    def _get_connection(self):
24
        print 'Connecting to %s' % self.port
25
        return httplib.HTTPConnection(self.host, self.port)
26
27
    def _verb_archive(self, verb, archive_name, query, body=None):
28
        url = self.archive_url(archive_name, query)
29
        connection = self._get_connection()
30
        connection.request(verb, url, body)
31
        return connection.getresponse()
32
33
    def put_message(self, archive_name, key, file_obj):
34
        response = self._verb_archive(
35
            'POST', archive_name, {'key': key}, file_obj.read())
6.1.1 by Aaron Bentley
Fix URLs etc.
36
        data = response.read()
37
        if response.status == httplib.BAD_REQUEST:
38
            raise Exception('wtf')
39
        elif response.status == httplib.CREATED:
40
            return
41
        else:
42
            raise Exception('!!')
6.1.5 by Aaron Bentley
Start working on GET.
43
44
    def get_messages(self, archive_name, message_ids=None):
45
        parameters = {}
46
        if message_ids is not None:
47
            parameters[message_ids] = message_ids
48
        query = {'parameters': simplejson.dumps(parameters)}
49
        response = self._verb_archive('GET', archive_name, query)
50
        return {}