~ubuntuone-support/+junk/syncdaemon-perftest

« back to all changes in this revision

Viewing changes to u1perftest/rest.py

  • Committer: Roman Yepishev
  • Date: 2012-04-02 10:28:38 UTC
  • Revision ID: roman.yepishev@canonical.com-20120402102838-cftyw8c8754a1rrj
Latest version of the scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from oauth import oauth
 
2
 
 
3
import httplib2, urlparse, cgi
 
4
 
 
5
class OAuthHttpClient(object):
 
6
 
 
7
    def __init__(self):
 
8
        self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
 
9
        self.consumer = None
 
10
        self.token = None
 
11
        self.client = httplib2.Http()
 
12
 
 
13
    def set_consumer(self, consumer_key, consumer_secret):
 
14
        self.consumer = oauth.OAuthConsumer(consumer_key,
 
15
                                            consumer_secret)
 
16
 
 
17
    def set_token(self, token, token_secret):
 
18
        self.token = oauth.OAuthToken( token, token_secret)
 
19
 
 
20
    def _get_oauth_request_header(self, url, method):
 
21
        """Get an oauth request header given the token and the url"""
 
22
        query = urlparse.urlparse(url).query
 
23
 
 
24
        oauth_request = oauth.OAuthRequest.from_consumer_and_token(
 
25
            http_url=url,
 
26
            http_method=method,
 
27
            oauth_consumer=self.consumer,
 
28
            token=self.token,
 
29
            parameters=dict(cgi.parse_qsl(query))
 
30
        )
 
31
        oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
 
32
                                   self.consumer, self.token)
 
33
        return oauth_request.to_header()
 
34
 
 
35
    def request(self, url, method="GET", body=None, headers={}):
 
36
        oauth_header = self._get_oauth_request_header(url, method)
 
37
        headers.update(oauth_header)
 
38
        return self.client.request(url, method, headers=headers, body=body)
 
39