diff options
author | Tim Burke <tim.burke@gmail.com> | 2016-08-23 16:17:21 -0700 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2016-11-18 11:47:14 -0800 |
commit | a1e2bcde4a54a33054c7dcb31c3c3b0e6d72d021 (patch) | |
tree | 46a4d7b28a5c2ac4a317c7f2abede8159772510d /swiftclient/utils.py | |
parent | 12d42efad23f95461692af99a77136d7591a5fee (diff) | |
download | python-swiftclient-a1e2bcde4a54a33054c7dcb31c3c3b0e6d72d021.tar.gz |
Accept more types of input for headers/meta
Previously, we only accepted iterables of strings like 'Header: Value'.
Now, we'll also accept lists of tuples like ('Header', 'Value') as well
as dictionaries like {'Header': 'Value'}.
This should be more intuitive for application developers, who are
already used to being able to pass dicts or lists of tuples to libraries
like requests.
Change-Id: I93ed2f1e8305f0168b7a4bd90c205b04730da836
Diffstat (limited to 'swiftclient/utils.py')
-rw-r--r-- | swiftclient/utils.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/swiftclient/utils.py b/swiftclient/utils.py index e14602d..aa8b28a 100644 --- a/swiftclient/utils.py +++ b/swiftclient/utils.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Miscellaneous utility functions for use with Swift.""" +import collections import gzip import hashlib import hmac @@ -148,15 +149,24 @@ def parse_api_response(headers, body): def split_request_headers(options, prefix=''): headers = {} + if isinstance(options, collections.Mapping): + options = options.items() for item in options: - split_item = item.split(':', 1) - if len(split_item) == 2: - headers[(prefix + split_item[0]).title()] = split_item[1].strip() - else: + if isinstance(item, six.string_types): + if ':' not in item: + raise ValueError( + "Metadata parameter %s must contain a ':'.\n" + "Example: 'Color:Blue' or 'Size:Large'" + % item + ) + item = item.split(':', 1) + if len(item) != 2: raise ValueError( - "Metadata parameter %s must contain a ':'.\n%s" - % (item, "Example: 'Color:Blue' or 'Size:Large'") + "Metadata parameter %r must have exactly two items.\n" + "Example: ('Color', 'Blue') or ['Size', 'Large']" + % (item, ) ) + headers[(prefix + item[0]).title()] = item[1].strip() return headers |