Exemplo n.º 1
0
    def request(self, method, uri, access_token=None, **kwargs):
        """Sends a request to an OAuth 2.0 endpoint, properly wrapped around
        requests.

        :param method: A string representation of the HTTP method to be used.
        :param uri: The resource to be requested.
        :param access_token: Overrides self.access_token. Defaults to None.
        :param \*\*kwargs: Optional arguments. Same as Requests.
        """

        # see if we can prepend base_url
        if self.base_url is not None and not absolute_url(uri):
            uri = urljoin(self.base_url, uri)

        # see if we can use a stored access_token
        if access_token is None and self.access_token is None:
            raise TypeError("access_token must not be None")

        if access_token is None:
            access_token = self.access_token

        kwargs.setdefault("params", {}).update(access_token=access_token)
        kwargs.setdefault("timeout", DEFAULT_TIMEOUT)

        response = self.session.request(method, uri, **kwargs)

        return Response(response)
Exemplo n.º 2
0
    def request(self, method, uri, **kwargs):
        """Sends a request to an Ofly endpoint, properly wrapped around
        requests.

        :param method: A string representation of the HTTP method to be
            used.
        :param url: The resource to be requested.
        :param header_auth: Authenication via header, defaults to False.
        :param \*\*kwargs: Optional arguments. Same as Requests.
        """
        params = kwargs.pop("params", None)
        data = kwargs.pop("data", None)

        if params is None:
            params = {}

        kwargs.setdefault("timeout", DEFAULT_TIMEOUT)

        if self.base_url is not None and not absolute_url(uri):
            uri = urljoin(self.base_url, uri)

        header_auth = kwargs.pop("header_auth", False)
        if header_auth:
            params, headers = self._sha1_sign_params(uri, header_auth, **params)

            response = self.session.request(method, uri + "?" + params, headers=headers, **kwargs)
        else:
            params = self._sha1_sign_params(uri, **params)

            response = self.session.request(method, uri + "?" + params, data=data, **kwargs)

        return Response(response)
Exemplo n.º 3
0
    def request(self, method, uri, access_token=None,
                access_token_secret=None, **kwargs):
        '''Makes a request using :class:`_construct_session`.

        :param method: A string representation of the HTTP method to be
            used.
        :param uri: The resource to be requested.
        :param access_token: The access token as returned by
            :class:`get_access_token`.
        :param access_token_secret: The access token secret as returned by
            :class:`get_access_token`.
        :param header_auth: Authenication via header, defaults to False.
        :param allow_redirects: Allows a request to redirect, defaults to True.
        :param \*\*kwargs: Optional arguments. Same as Requests.
        '''
        header_auth = kwargs.pop('header_auth', self.header_auth)

        kwargs.setdefault('allow_redirects', True)
        kwargs.setdefault('headers', {})
        kwargs.setdefault('params', {})
        kwargs.setdefault('timeout', DEFAULT_TIMEOUT)

        # set the Content-Type if unspecified
        if method in ('POST', 'PUT'):
            kwargs['headers'].setdefault('Content-Type',
                                         'application/x-www-form-urlencoded')

        # prepend a base_url to the uri if we can
        if self.base_url is not None and not absolute_url(uri):
            uri = self.base_url + uri

        # check user supplied tokens
        tokens = (access_token, access_token_secret)
        all_tokens_none = all(v is None for v in tokens)
        if None in tokens and not all_tokens_none:
            raise TypeError('Either both or neither access_token and '
                            'access_token_secret must be supplied')

        # use default tokens if user supplied tokens are not present
        if all_tokens_none:
            access_token = self.access_token
            access_token_secret = self.access_token_secret

        session_params = dict(access_token=access_token,
                              access_token_secret=access_token_secret,
                              header_auth=header_auth)
        auth_session = self._construct_session(**session_params)

        response = auth_session.request(method,
                                        uri,
                                        **kwargs)

        return Response(response)
Exemplo n.º 4
0
 def _set_url(self, url):
     if self.service is not None and self.service.base_url is not None and \
             not absolute_url(url):
         return urljoin(self.service.base_url, url)
     return url
Exemplo n.º 5
0
 def test_not_absolute_url(self):
     self.assertFalse(absolute_url('/some/resource'))
Exemplo n.º 6
0
 def test_absolute_url_https(self):
     self.assertTrue(absolute_url('https://example.com/'))