def ping_url(url, email, credentials): """Ping the 'url' with the 'email' attached to it. Sign the request with 'credentials'. The url must not be None. """ logger.info('Pinging server using url: %r, email: %r.', url, email) assert isinstance(url, unicode), 'Url %r must be unicode' % url target_url = url try: target_url = url.format(email=email) except IndexError: # tuple index out of range target_url = url.format(email) # format the first substitution if target_url == url: logger.debug('Original url (%r) could not be formatted, ' 'appending email (%r).', url, email) assert url.endswith(u'/'), 'Url %r must end with /.' % url target_url = url + email wc = webclient.webclient_factory() try: logger.debug('Opening the url %r with webclient.request.', url) response = yield wc.request(target_url, oauth_credentials=credentials) logger.debug('Url %r opened. Response content: %r.', url, response.content) defer.returnValue(response) finally: wc.shutdown()
def generate_captcha(self, filename): """Generate a captcha using the SSO service.""" logger.debug('generate_captcha: requesting captcha, filename: %r', filename) restful_client = restful.RestfulClient(self.service_url) try: captcha = yield restful_client.restcall("captchas.new") finally: restful_client.shutdown() # download captcha and save to 'filename' logger.debug('generate_captcha: server answered: %r', captcha) wc = webclient.webclient_factory() try: response = yield wc.request(captcha['image_url']) with open(filename, 'wb') as f: f.write(response.content) except: msg = 'generate_captcha crashed while downloading the image.' logger.exception(msg) raise finally: wc.shutdown() defer.returnValue(captcha['captcha_id'])
def ping_url(url, email, credentials): """Ping the 'url' with the 'email' attached to it. Sign the request with 'credentials'. The url must not be None. """ logger.info('Pinging server using url: %r, email: %r.', url, email) assert isinstance(url, compat.text_type), 'Url %r must be unicode' % url target_url = url try: target_url = url.format(email=email) except IndexError: # tuple index out of range target_url = url.format(email) # format the first substitution if target_url == url: logger.debug( 'Original url (%r) could not be formatted, ' 'appending email (%r).', url, email) assert url.endswith('/'), 'Url %r must end with /.' % url target_url = url + email wc = webclient.webclient_factory() try: logger.debug('Opening the url %r with webclient.request.', url) response = yield wc.request(target_url, oauth_credentials=credentials) logger.debug('Url %r opened. Response content: %r.', url, response.content) defer.returnValue(response) finally: wc.shutdown()
def __init__(self, get_credentials, base_url=WEBSERVICE_BASE_URL): """Initialize the webclient.""" self.base_url = base_url self.get_credentials = get_credentials self.wc = webclient_factory(APP_NAME) logger.debug("WebClient created: base_url is %r, inner client is %r.", self.base_url, self.wc)
def __init__(self, service_iri, username=None, password=None, oauth_credentials=None): """Initialize this instance.""" assert service_iri.endswith("/") self.service_iri = service_iri self.webclient = webclient.webclient_factory(username=username, password=password, oauth_sign_plain=True) self.oauth_credentials = oauth_credentials