~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 20:16:12 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316201612-lr7b32umqgduaja6
Added a rudimentary put_archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
        self.port = port
28
28
        self.netloc = '%s:%d' % (host, port)
29
29
 
30
 
    def archive_url(self, archive_id, query):
 
30
    def archive_url(self, path, query):
31
31
        """Return the URL for an archive
32
32
 
33
 
        :param archive_id: The id of the archive to generate the URL for.
 
33
        :param path: The path to generate the URL for.
 
34
            Maybe be '', 'archive_id', or 'archive_id/message_id'
34
35
        :param query: The query to use in the URL, as a dict.
35
36
        """
36
 
        path = '/archive/%s' % quote(archive_id)
 
37
        path = '/archive/%s' % quote(path)
37
38
        query_string = urlencode(query)
38
39
        return urlunparse(('http', self.netloc, path, '', query_string, ''))
39
40
 
40
41
    def _get_connection(self):
41
42
        return httplib.HTTPConnection(self.host, self.port)
42
43
 
43
 
    def _method_archive(self, method, archive_id, query, body=None):
 
44
    def _method_archive(self, method, path, query, body=None):
44
45
        """Perform an HTTP method on an archive's URL."""
45
 
        url = self.archive_url(archive_id, query)
 
46
        url = self.archive_url(path, query)
46
47
        connection = self._get_connection()
47
48
        connection.request(method, url, body)
48
49
        return connection.getresponse()
54
55
        :param mbox: An optional mbox with messages to add to the new archive.
55
56
        """
56
57
        response = self._method_archive(
57
 
            'POST', '', {'archive_id': archive_id}, None)
 
58
            'PUT', archive_id, {}, None)
58
59
        response.read()
59
60
        if response.status == httplib.BAD_REQUEST:
60
61
            raise Exception('wtf')
73
74
        """
74
75
        path = '%s/%s' % (archive_id, key)
75
76
        response = self._method_archive(
76
 
            'POST', path, {}, file_obj.read())
 
77
            'PUT', path, {}, file_obj.read())
77
78
        response.read()
78
79
        if response.status == httplib.BAD_REQUEST:
79
80
            raise Exception('wtf')