~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-10 14:41:33 UTC
  • Revision ID: aaron@canonical.com-20120110144133-p9dk4oktqnp3lnlw
Fix URLs etc.

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', 'foo', 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
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))
 
8
 
 
9
 
 
10
class GrackleClient:
 
11
 
 
12
    def __init__(self, host, port):
 
13
        self.host = host
 
14
        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()
 
26
        data = response.read()
 
27
        if response.status == httplib.BAD_REQUEST:
 
28
            raise Exception('wtf')
 
29
        elif response.status == httplib.CREATED:
 
30
            return
 
31
        else:
 
32
            raise Exception('!!')