Permalink
Browse files

Make method signatures in parameters.py consistent

* Use oauthlib.common.extract_params
* replace _add_params_to_qs with _append_parms
* remove no-longer-needed order_params decorator
* Yes I know this breaks tests right now :(
  • Loading branch information...
1 parent 33c1625 commit 37bd92d1b5bfa0bac80dde79a38bca72c03e95a8 @idan idan committed Apr 23, 2012
Showing with 51 additions and 66 deletions.
  1. +32 −54 oauthlib/oauth1/rfc5849/parameters.py
  2. +19 −12 tests/oauth1/rfc5849/test_parameters.py
@@ -10,47 +10,16 @@
.. _`section 3.5`: http://tools.ietf.org/html/rfc5849#section-3.5
"""
-from urlparse import urlparse, urlunparse, parse_qsl
+from urlparse import urlparse, urlunparse
+from urllib import urlencode
from . import utils
+from oauthlib.common import extract_params
-def order_params(target):
- """Decorator which reorders params contents to start with oauth_* params
-
- Assumes the decorated method takes a params dict or list of tuples as its
- first argument.
- """
- def wrapper(params, *args, **kwargs):
- ordered_params = order_oauth_parameters(params)
- return target(ordered_params, *args, **kwargs)
-
- wrapper.__doc__ = target.__doc__
- return wrapper
-
-
-def order_oauth_parameters(params):
- """Order a parameters dict or list of tuples with OAuth ones first
-
- Per `section 3.5`_ of the spec.
-
- .. _`section 3.5`: http://tools.ietf.org/html/rfc5849#section-3.5
- """
- # Convert dictionaries to list of tuples
- if isinstance(params, dict):
- params = params.items()
-
- ordered = []
- for k, v in params:
- if k.startswith("oauth_"):
- ordered.insert(0, (k, v))
- else:
- ordered.append((k, v))
-
- return ordered
-
-
+# TODO: do we need filter_params now that oauth_params are handled by Request?
+# We can easily pass in just oauth protocol params.
@utils.filter_params
-def prepare_headers(params, headers=None, realm=None):
+def prepare_headers(oauth_params, headers=None, realm=None):
"""**Prepare the Authorization header.**
Per `section 3.5.1`_ of the spec.
@@ -79,7 +48,7 @@ def prepare_headers(params, headers=None, realm=None):
# Protocol parameters SHALL be included in the "Authorization" header
# field as follows:
authorization_header_parameters_parts = []
- for oauth_parameter_name, value in params:
+ for oauth_parameter_name, value in oauth_params:
# 1. Parameter names and values are encoded per Parameter Encoding
# (`Section 3.6`_)
#
@@ -120,41 +89,50 @@ def prepare_headers(params, headers=None, realm=None):
return full_headers
-def _add_params_to_qs(query, params):
- queryparams = parse_qsl(query, True)
- queryparams.extend(params)
- queryparams.sort(key=lambda i: i[0].startswith('oauth_'))
- return utils.urlencode(queryparams)
+def _append_params(oauth_params, params):
+ """Append OAuth params to an existing set of parameters.
+ Both params and oauth_params is must be lists of 2-tuples.
-@order_params
-def prepare_form_encoded_body(params, body):
+ Per `section 3.5.2`_ and `3.5.3`_ of the spec.
+
+ .. _`section 3.5.2`: http://tools.ietf.org/html/rfc5849#section-3.5.2
+ .. _`3.5.3`: http://tools.ietf.org/html/rfc5849#section-3.5.3
+
+ """
+ merged = list(params)
+ merged.extend(oauth_params)
+ # The request URI / entity-body MAY include other request-specific
+ # parameters, in which case, the protocol parameters SHOULD be appended
+ # following the request-specific parameters, properly separated by an "&"
+ # character (ASCII code 38)
+ merged.sort(key=lambda i: i[0].startswith('oauth_'))
+ return merged
+
+
+# @order_params
+def prepare_form_encoded_body(oauth_params, body):
"""Prepare the Form-Encoded Body.
Per `section 3.5.2`_ of the spec.
- params: OAuth parameters and data (i.e. POST data).
-
.. _`section 3.5.2`: http://tools.ietf.org/html/rfc5849#section-3.5.2
"""
# append OAuth params to the existing body
- return _add_params_to_qs(body, params)
+ return _append_params(oauth_params, body)
-@order_params
-def prepare_request_uri_query(params, uri):
+# @order_params
+def prepare_request_uri_query(oauth_params, uri):
"""Prepare the Request URI Query.
Per `section 3.5.3`_ of the spec.
- params: OAuth parameters and data (i.e. POST data).
- url: The request url. Query components will be removed.
-
.. _`section 3.5.3`: http://tools.ietf.org/html/rfc5849#section-3.5.3
"""
# append OAuth params to the existing set of query components
sch, net, path, par, query, fra = urlparse(uri)
- query = _add_params_to_qs(query, params)
+ query = urlencode(_append_params(oauth_params, extract_params(query) or []))
return urlunparse((sch, net, path, par, query, fra))
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
-from oauthlib.oauth1.rfc5849.parameters import *
+from urllib import urlencode
+from oauthlib.oauth1.rfc5849.parameters import (_append_params, prepare_headers,
+ prepare_form_encoded_body, prepare_request_uri_query)
from ...unittest import TestCase
@@ -37,18 +39,23 @@ class ParameterTests(TestCase):
u'oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"',
))
- def test_order_oauth_parameters(self):
- unordered = {
- 'oauth_foo': 'foo',
- 'oauth_bar': 'bar',
- 'lala': 123,
- 'oauth_baz': 'baz', }
- expected = [
- ('oauth_bar', 'bar'),
+ def test_append_params(self):
+ unordered_1 = [
+ ('oauth_foo', 'foo'),
+ ('lala', 123),
('oauth_baz', 'baz'),
+ ('oauth_bar', 'bar'), ]
+ unordered_2 = [
+ ('teehee', 456),
+ ('oauth_quux', 'quux'), ]
+ expected = [
+ ('teehee', 456),
+ ('lala', 123),
+ ('oauth_quux', 'quux'),
('oauth_foo', 'foo'),
- ('lala', 123)]
- self.assertEqual(order_oauth_parameters(unordered), expected)
+ ('oauth_baz', 'baz'),
+ ('oauth_bar', 'bar'), ]
+ self.assertEqual(_append_params(unordered_1, unordered_2), expected)
def test_prepare_headers(self):
self.assertEqual(
@@ -70,7 +77,7 @@ def test_prepare_form_encoded_body(self):
existing_body = u''
form_encoded_body = 'data_param_foo=foo&data_param_1=1&oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D&oauth_nonce=7d8f3e4a&oauth_timestamp=137131201&oauth_signature_method=HMAC-SHA1&oauth_token=kkk9d7dh3k39sjv7&oauth_consumer_key=9djdj82h48djs9d2'
self.assertEqual(
- prepare_form_encoded_body(self.auth_and_data, existing_body),
+ urlencode(prepare_form_encoded_body(self.auth_and_data, existing_body)),
form_encoded_body)
def test_prepare_request_uri_query(self):

0 comments on commit 37bd92d

Please sign in to comment.