From 640f56a1dbe9682e600d1a2a4b430dca5559b51b Mon Sep 17 00:00:00 2001 From: Eva Balycheva Date: Mon, 25 Jan 2016 06:53:39 +0300 Subject: [PATCH] Fix client not working on Python 3 Currently the client is not working at all on Python 3 when it uses Keystone authentication, and no project id is specified by user. It is because in this case the client prepares a request with 'X-Project-Id' header assigned to {} (empty dictionary) value. The Python 3 standard HTTP library expects all passed headers to be strings and fails when one of the headers is dictionary: File "/usr/lib/python3.4/http/client.py", line 1069, in putheader value = b'\r\n\t'.join(values) TypeError: sequence item 0: expected bytes, bytearray, or an object with the buffer interface, dict found This patch solves the problem by making the client not add 'X-Project-Id' to request at all in this case. Change-Id: I7296b08d273b7173a9f6deb0ae9fb6ff929366db Closes-Bug: 1525973 --- zaqarclient/transport/request.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/zaqarclient/transport/request.py b/zaqarclient/transport/request.py index 32d7a799..a5259e99 100644 --- a/zaqarclient/transport/request.py +++ b/zaqarclient/transport/request.py @@ -46,11 +46,16 @@ def prepare_request(auth_opts=None, data=None, **kwargs): # TODO(flaper87): Do something smarter # to get the api_version. req = auth_backend.authenticate(1, req) - req.headers['X-Project-Id'] = auth_opts.get( - 'options', {}).get('os_project_id', {}) - # In case of noauth backend, a default project id will be added to header. - if (not req.headers['X-Project-Id'] and + project_id = auth_opts.get('options', {}).get('os_project_id', {}) + + # Let's add project id header, only if it will have non-empty value. + if project_id: + req.headers['X-Project-Id'] = project_id + + # In case of noauth backend and no specified project id, the default + # project id will be added as header. + if ('X-Project-Id' not in req.headers and auth_opts.get("backend") == "noauth"): req.headers['X-Project-Id'] = "fake_project_id_for_noauth"