diff options
| author | TommyLike <tommylikehu@gmail.com> | 2016-09-29 16:00:10 +0800 |
|---|---|---|
| committer | TommyLike <tommylikehu@gmail.com> | 2016-11-07 09:26:52 +0800 |
| commit | d8ca399aba604c2fb518eabaadf846433700eb30 (patch) | |
| tree | 0a4dd27baa4e1482f6d8541cc27e07175979d26b /cinderclient/utils.py | |
| parent | a01a3e1b7f6dd0919ffa15a4d54b84f0ac1e7915 (diff) | |
| download | python-cinderclient-d8ca399aba604c2fb518eabaadf846433700eb30.tar.gz | |
Optimize: add build_query_param method to clean code
Currently the client build query params in respective method
this patch intends to use 'utils.build_query_param' to make
the code clean.
Change-Id: I3a3ae90cc6011d1aa0cc39db4329d9bc08801904
Diffstat (limited to 'cinderclient/utils.py')
| -rw-r--r-- | cinderclient/utils.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/cinderclient/utils.py b/cinderclient/utils.py index 86c276e..31e9444 100644 --- a/cinderclient/utils.py +++ b/cinderclient/utils.py @@ -22,6 +22,7 @@ import types import uuid import six +from six.moves.urllib import parse import prettytable from cinderclient import exceptions @@ -187,6 +188,24 @@ def unicode_key_value_to_string(dictionary): for k, v in dictionary.items()) +def build_query_param(params, sort=False): + """parse list to url query parameters""" + + if params is None: + params = {} + if not sort: + param_list = list(params.items()) + else: + param_list = list(sorted(params.items())) + + query_string = parse.urlencode( + [(k, v) for (k, v) in param_list if v]) + if query_string: + query_string = "?%s" % (query_string,) + + return query_string + + def print_dict(d, property="Property", formatters=None): pt = prettytable.PrettyTable([property, 'Value'], caching=False) pt.align = 'l' |
