Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
TO SQUASH: Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Aug 15, 2016
1 parent e9eab00 commit b9abeae
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions oauth2client/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,35 @@ def string_to_scopes(scopes):
return scopes


def update_query_params(uri, params):
"""Updates a URI with new query parameters.
If a given key from ``params`` is repeated in the ``uri``, then
the new value from ``params`` will be added to that list. If the
key occurs zero or once in the ``uri``, the value from ``params``
will be used as the only value.
Args:
uri: string, A valid URI, with potential existing query parameters.
params: dict, A dictionary of query parameters.
Returns:
The same URI but with the new query parameters added.
"""
parts = urllib.parse.urlparse(uri)
query_params = urllib.parse.parse_qs(parts.query)
for key, value in six.iteritems(params):
curr_vals = query_params.setdefault(key, [])
if len(curr_vals) > 1:
curr_vals.append(value)
else:
curr_vals[::] = [value]

new_query = urllib.parse.urlencode(query_params, doseq=True)
new_parts = parts._replace(query=new_query)
return urllib.parse.urlunparse(new_parts)


def _add_query_parameter(url, name, value):
"""Adds a query parameter to a url.
Expand All @@ -199,7 +228,8 @@ def _add_query_parameter(url, name, value):
"""
if value is None:
return url
return update_query_params(url, {name: value})
else:
return update_query_params(url, {name: value})


def validate_file(filename):
Expand Down Expand Up @@ -297,35 +327,6 @@ def _urlsafe_b64decode(b64string):
return base64.urlsafe_b64decode(padded)


def update_query_params(uri, params):
"""Updates a URI with new query parameters.
If a given key from ``params`` is repeated in the ``uri``, then
the new value from ``params`` will be added to that list. If the
key occurs zero or once in the ``uri``, the value from ``params``
will be used as the only value.
Args:
uri: string, A valid URI, with potential existing query parameters.
params: dict, A dictionary of query parameters.
Returns:
The same URI but with the new query parameters added.
"""
parts = urllib.parse.urlparse(uri)
query_params = urllib.parse.parse_qs(parts.query)
for key, value in six.iteritems(params):
curr_vals = query_params.setdefault(key, [])
if len(curr_vals) > 1:
curr_vals.append(value)
else:
curr_vals[::] = [value]

new_query = urllib.parse.urlencode(query_params, doseq=True)
new_parts = parts._replace(query=new_query)
return urllib.parse.urlunparse(new_parts)


def parse_unique_urlencoded(content):
"""Parses unique key-value parameters from urlencoded content.
Expand Down

0 comments on commit b9abeae

Please sign in to comment.