Example #1
0
def xauth(base_url_template=DEFAULT_READER_URL_TEMPLATE, **xargs):
    """
    Returns an OAuth token tuple that can be used with clients.ReaderClient.

    :param base_url_template: Template for generating Readability API urls.
    :param consumer_key:  Readability consumer key, otherwise read from READABILITY_CONSUMER_KEY.
    :param consumer_secret: Readability consumer secret, otherwise read from READABILITY_CONSUMER_SECRET.
    :param username: A username, otherwise read from READABILITY_USERNAME.
    :param password: A password, otherwise read from READABILITY_PASSWORD.

    """
    consumer_key = xargs.get('consumer_key') or required_from_env(
        'READABILITY_CONSUMER_KEY')
    consumer_secret = xargs.get('consumer_secret') or required_from_env(
        'READABILITY_CONSUMER_SECRET')
    username = xargs.get('username') or required_from_env(
        'READABILITY_USERNAME')
    password = xargs.get('password') or required_from_env(
        'READABILITY_PASSWORD')

    client = Client(consumer_key,
                    client_secret=consumer_secret,
                    signature_type='BODY')
    url = base_url_template.format(ACCESS_TOKEN_URL)
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    params = {
        'x_auth_username': username,
        'x_auth_password': password,
        'x_auth_mode': 'client_auth'
    }

    uri, headers, body = client.sign(url,
                                     http_method='POST',
                                     body=urlencode(params),
                                     headers=headers)

    response = requests.post(uri, data=body)
    logger.debug('POST to %s.', uri)

    token = parse_qs(response.content)
    try:
        # The indexes below are a little weird. parse_qs above gives us
        # back a dict where each value is a list. We want the first value
        # in those lists.
        token = (token[b'oauth_token'][0].decode(),
                 token[b'oauth_token_secret'][0].decode())
    except KeyError:
        raise ValueError('Invalid Credentials.')

    return token
def xauth(base_url_template=DEFAULT_READER_URL_TEMPLATE, **xargs):
    """
    Returns an OAuth token tuple that can be used with clients.ReaderClient.

    :param base_url_template: Template for generating Readability API urls.
    :param consumer_key:  Readability consumer key, otherwise read from READABILITY_CONSUMER_KEY.
    :param consumer_secret: Readability consumer secret, otherwise read from READABILITY_CONSUMER_SECRET.
    :param username: A username, otherwise read from READABILITY_USERNAME.
    :param password: A password, otherwise read from READABILITY_PASSWORD.

    """
    consumer_key = xargs.get('consumer_key') or required_from_env('READABILITY_CONSUMER_KEY')
    consumer_secret = xargs.get('consumer_secret') or required_from_env('READABILITY_CONSUMER_SECRET')
    username = xargs.get('username') or required_from_env('READABILITY_USERNAME')
    password = xargs.get('password') or required_from_env('READABILITY_PASSWORD')

    client = Client(consumer_key, client_secret=consumer_secret, signature_type='BODY')
    url = base_url_template.format(ACCESS_TOKEN_URL)
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    params = {
        'x_auth_username': username,
        'x_auth_password': password,
        'x_auth_mode': 'client_auth'
    }

    uri, headers, body = client.sign(url,
        http_method='POST',
        body=urlencode(params),
        headers=headers)

    response = requests.post(uri, data=body)
    logger.debug('POST to %s.', uri)

    token = parse_qs(response.content)
    try:
        # The indexes below are a little weird. parse_qs above gives us
        # back a dict where each value is a list. We want the first value
        # in those lists.
        token = (token[b'oauth_token'][0].decode(), token[b'oauth_token_secret'][0].decode())
    except KeyError:
        raise ValueError('Invalid Credentials.')

    return token
    def __init__(self, token_key, token_secret,
        base_url_template=DEFAULT_READER_URL_TEMPLATE, **xargs):
        """
        Initialize the ReaderClient.

        :param consumer_key: Reader API key, otherwise read from READABILITY_CONSUMER_KEY.
        :param consumer_secret: Reader API secret, otherwise read from READABILITY_CONSUMER_SECRET.
        :param token_key: Readability user token key
        :param token_secret: Readability user token secret
        :param base_url_template (optional): Template used to build URL to
            which requests will be sent. This shouldn't need to be passed as the
            main purpose for it is testing environments that the user probably
            doesn't have access to (staging, local dev, etc).

        """
        consumer_key = xargs.get('consumer_key') or required_from_env('READABILITY_CONSUMER_KEY')
        consumer_secret = xargs.get('consumer_secret') or required_from_env('READABILITY_CONSUMER_SECRET')

        self.base_url_template = base_url_template
        self.oauth_session = OAuth1Session(consumer_key, consumer_secret, token_key, token_secret)
    def __init__(self, base_url_template=DEFAULT_PARSER_URL_TEMPLATE, **xargs):
        """
        Initialize client.

        :param token: parser API token, otherwise read from READABILITY_PARSER_TOKEN.
        :param base_url_template (optional): Template used to build URL to
            which requests will be sent. This shouldn't need to be passed as the
            main purpose for it is testing environments that the user probably
            doesn't have access to (staging, local dev, etc).
        """
        logger.debug('Initializing ParserClient with base url template %s',
            base_url_template)

        self.token = xargs.get('token', None) or required_from_env('READABILITY_PARSER_TOKEN')
        self.base_url_template = base_url_template
 def setUp(self):
     self.parser_token = required_from_env('READABILITY_PARSER_TOKEN')
     self.parser_client = ParserClient(token=self.parser_token)
     self.test_url = 'https://en.wikipedia.org/wiki/Mark_Twain'