From 2d5e38f30ad9f3bd0e3a87518ae79ccbdf779369 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 3 Aug 2012 22:04:43 -0400 Subject: [PATCH] params now accepts a k/v list. Also added test for params accepting k/v lists. --- requests/sessions.py | 13 +++++++++++-- tests/test_requests.py | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index 3113c78..ee47a01 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -18,6 +18,7 @@ from .hooks import dispatch_hook from .utils import header_expand from .packages.urllib3.poolmanager import PoolManager + def merge_kwargs(local_kwarg, default_kwarg): """Merges kwarg dictionaries. @@ -37,12 +38,21 @@ def merge_kwargs(local_kwarg, default_kwarg): if not hasattr(default_kwarg, 'items'): return local_kwarg + try: + dict(local_kwarg) + except ValueError: + raise ValueError('Unable to encode lists with elements that are not ' + '2-tuples.') + + if hasattr(local_kwarg, 'items'): + local_kwarg = list(local_kwarg.items()) + # Update new values. kwargs = default_kwarg.copy() kwargs.update(local_kwarg) # Remove keys that are set to None. - for (k, v) in list(local_kwarg.items()): + for (k, v) in local_kwarg: if v is None: del kwargs[k] @@ -56,7 +66,6 @@ class Session(object): 'headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks', 'params', 'config', 'verify', 'cert', 'prefetch'] - def __init__(self, headers=None, cookies=None, diff --git a/tests/test_requests.py b/tests/test_requests.py index 4e0d034..076ca6c 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -96,6 +96,11 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): self.assertEqual(request.full_url, "http://example.com/path?key=value&a=b#fragment") + def test_params_accepts_kv_list(self): + request = requests.Request('http://example.com/path', + params=[('a', 'b')]) + self.assertEqual(request.full_url, 'http://example.com/path?a=b') + def test_HTTP_200_OK_GET(self): r = get(httpbin('get')) self.assertEqual(r.status_code, 200) -- 2.7.4