def test_properly_partitions(self): args = dict( oauth_token="token", oauth_blah="blah", something="another", boobooo="booobooo", ) a, b = functional.partition_dict(lambda k, v: k.startswith("oauth_"), args) self.assertDictEqual(a, {"oauth_token": "token", "oauth_blah": "blah"}) self.assertDictEqual(b, {"boobooo": "booobooo", "something": "another"})
def test_properly_partitions(self): args = dict( oauth_token="token", oauth_blah="blah", something="another", boobooo="booobooo", ) a, b = partition_dict(lambda k, v: k.startswith("oauth_"), args) self.assertDictEqual(a, {'oauth_token': 'token', 'oauth_blah': 'blah'}) self.assertDictEqual(b, {'boobooo': 'booobooo', 'something': 'another'})
def _request(cls, client_credentials, method, url, params=None, body=None, headers=None, realm=None, use_authorization_header=True, auth_credentials=None, oauth_signature_method=SIGNATURE_METHOD_HMAC_SHA1, oauth_version=OAUTH_VERSION_1, **kwargs): """ Makes an OAuth request. :param client_credentials: Client credentials (consumer key and secret). :param method: HTTP method. :param url: Request URL :param params: Additional query/payload parameters. If a `body` argument to this function is specified, the parameters are appended to the URL query string. If a `body` is not specified and a method other than GET is used the parameters will be added to the entity body. :param body: Entity body string. :param headers: Request headers dictionary. :param realm: Authorization realm. :param use_authorization_header: ``True`` if we should; ``False`` otherwise. :param auth_credentials: OAuth token/temporary credentials (if available). :param oauth_signature_method: Signature method. :param kwargs: Additional parameters including those that may begin with ``oauth_``. :returns: HTTP response (:class:`pyoauth.http.ResponseAdapter`) if ``async_callback`` is not specified; otherwise, ``async_callback`` is called with the response as its argument. """ method = method.upper() body = body or SYMBOL_EMPTY_BYTES headers = headers or {} # Split all the oauth parameters and function parameters. extra_oauth_params, kwargs = \ partition_dict(lambda k, v: k.startswith(OAUTH_PARAM_PREFIX), kwargs) # Query/payload parameters must not contain OAuth-specific parameters. params = query_remove_oauth(params) if params else {} # The URL must not contain OAuth-specific parameters. url = oauth_url_sanitize(url, force_secure=False) # Temporary credentials requests don't have ``oauth_token``. if auth_credentials: oauth_token = auth_credentials.identifier oauth_token_secret = auth_credentials.shared_secret else: oauth_token = oauth_token_secret = None # Make OAuth-specific parameter dictionary. oauth_params = cls._generate_oauth_params( oauth_consumer_key=client_credentials.identifier, oauth_signature_method=oauth_signature_method, oauth_version=oauth_version, oauth_timestamp=cls.generate_timestamp(), oauth_nonce=cls.generate_nonce(), oauth_token=oauth_token, **extra_oauth_params ) # Sign the request. signature = cls._generate_signature(method, url, params, body, headers, client_credentials.shared_secret, oauth_token_secret, oauth_params) oauth_params[OAUTH_PARAM_SIGNATURE] = signature # Now build the request. return cls._build_request( method, url, params, body, headers, oauth_params, realm, use_authorization_header )