~ubuntuone-control-tower/ubuntuone-control-panel/trunk

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/web_client/__init__.py

  • Committer: Tarmac
  • Author(s): Alejandro J. Cura
  • Date: 2011-10-07 20:41:13 UTC
  • mfrom: (230.2.10 timestamp-autofix)
  • Revision ID: tarmac-20111007204113-1j9n36yyjwvyout7
Do a HEAD request on the server to get accurate timestamp (LP: #692597)

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    """The request ended with bad_request, unauthorized or forbidden."""
34
34
 
35
35
 
36
 
def build_oauth_headers(method, url, credentials):
 
36
def build_oauth_headers(method, url, credentials, timestamp):
37
37
    """Build an oauth request given some credentials."""
38
38
    consumer = oauth.OAuthConsumer(credentials["consumer_key"],
39
39
                                   credentials["consumer_secret"])
40
40
    token = oauth.OAuthToken(credentials["token"],
41
41
                             credentials["token_secret"])
 
42
    parameters = {}
 
43
    if timestamp:
 
44
        parameters["oauth_timestamp"] = timestamp
42
45
    request = oauth.OAuthRequest.from_consumer_and_token(
43
46
                                        http_url=url,
44
47
                                        http_method=method,
 
48
                                        parameters=parameters,
45
49
                                        oauth_consumer=consumer,
46
50
                                        token=token)
47
51
    sig_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
49
53
    return request.to_header()
50
54
 
51
55
 
52
 
def add_oauth_headers(append_method, method, url, credentials):
 
56
def add_oauth_headers(append_method, method, url, credentials, timestamp=None):
53
57
    """Sign a libsoup message with oauth headers."""
54
 
    headers = build_oauth_headers(method, url, credentials)
 
58
    headers = build_oauth_headers(method, url, credentials, timestamp)
55
59
    for key, value in headers.items():
56
60
        append_method(key, value)
57
61
 
58
62
 
59
 
def web_client_factory(*args, **kwargs):
60
 
    """Choose the type of the web client dynamically."""
 
63
def web_client_module():
 
64
    """Choose the module of the web client."""
61
65
    # the reactor can only be imported after Qt is initialized
62
66
    # pylint: disable=W0404
63
67
    from twisted.internet import reactor
64
68
    if getattr(reactor, "qApp", None):
65
 
        from ubuntuone.controlpanel.web_client.txwebclient import WebClient
 
69
        from ubuntuone.controlpanel.web_client import txwebclient as web_module
66
70
    else:
67
 
        from ubuntuone.controlpanel.web_client.libsoup import WebClient
68
 
    return WebClient(*args, **kwargs)
 
71
        from ubuntuone.controlpanel.web_client import libsoup as web_module
 
72
    return web_module
 
73
 
 
74
 
 
75
def web_client_factory(*args, **kwargs):
 
76
    """Choose the type of the web client dynamically."""
 
77
    web_module = web_client_module()
 
78
    return web_module.WebClient(*args, **kwargs)