~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-13 13:52:09 UTC
  • Revision ID: aaron@canonical.com-20120113135209-3s1l4hu25wjyq6jj
Fix grackle-put-message.

Show diffs side-by-side

added added

removed removed

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