~nataliabidart/ubuntu-sso-client/move-ping

« back to all changes in this revision

Viewing changes to ubuntu_sso/utils/__init__.py

  • Committer: Natalia B. Bidart
  • Date: 2012-02-09 21:17:54 UTC
  • Revision ID: natalia.bidart@canonical.com-20120209211754-6hmk8hkg1jy80v86
- Move the ping_url code to utils, and make the UserManagement service use it
  (LP: #929670).

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from oauth import oauth
24
24
from urlparse import urlparse
25
25
 
 
26
from twisted.internet import defer
 
27
 
26
28
from ubuntu_sso.logger import setup_logging
 
29
from ubuntu_sso.utils import webclient
27
30
 
28
31
 
29
32
logger = setup_logging("ubuntu_sso.utils")
119
122
    headers = oauth_req.to_header()
120
123
 
121
124
    return headers
 
125
 
 
126
 
 
127
@defer.inlineCallbacks
 
128
def ping_url(url, email, credentials):
 
129
    """Ping the 'url' with the 'email' attached to it.
 
130
 
 
131
    Sign the request with 'credentials'. The url must not be None.
 
132
 
 
133
    """
 
134
    logger.info('Pinging server using url: %r, email: %r.',
 
135
                url, email)
 
136
    assert isinstance(url, unicode), 'Url %r must be unicode' % url
 
137
 
 
138
    target_url = url
 
139
    try:
 
140
        target_url = url.format(email=email)
 
141
    except IndexError:  # tuple index out of range
 
142
        target_url = url.format(email)  # format the first substitution
 
143
 
 
144
    if target_url == url:
 
145
        logger.debug('Original url (%r) could not be formatted, '
 
146
                     'appending email (%r).', url, email)
 
147
        assert url.endswith(u'/'), 'Url %r must end with /.' % url
 
148
        target_url = url + email
 
149
 
 
150
    wc = webclient.webclient_factory()
 
151
    try:
 
152
        logger.debug('Opening the url %r with webclient.request.', url)
 
153
        response = yield wc.request(target_url, oauth_credentials=credentials)
 
154
        logger.debug('Url %r opened. Response content: %r.',
 
155
                     url, response.content)
 
156
        defer.returnValue(response)
 
157
    finally:
 
158
        wc.shutdown()