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

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-12 11:31:51 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120112113151-qe327t15vwogee6t
Order by thread subject.

Show diffs side-by-side

added added

removed removed

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