~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-11 13:37:23 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120111133723-cv7qfdgpd4zpk561
Cleanup.

Show diffs side-by-side

added added

removed removed

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