Skip to content

Commit

Permalink
astakosclient: Pass headers for any API call
Browse files Browse the repository at this point in the history
Introduce a headers dictionary that will be provided in every subsequent
astakos API call. If astakosclient wants to use a provided header key,
it will override any value passed in the 'static' headers.
  • Loading branch information
philipgian committed Sep 21, 2016
1 parent 61e9557 commit 2efd4f7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions astakosclient/astakosclient/__init__.py
Expand Up @@ -66,8 +66,8 @@ class AstakosClient(object):
# Too many arguments. pylint: disable-msg=R0913
# Too many local variables. pylint: disable-msg=R0914
# Too many statements. pylint: disable-msg=R0915
def __init__(self, token, auth_url,
retry=0, use_pool=False, pool_size=8, logger=None):
def __init__(self, token, auth_url, retry=0, use_pool=False, pool_size=8,
logger=None, headers=None):
"""Initialize AstakosClient Class
Keyword arguments:
Expand Down Expand Up @@ -107,6 +107,10 @@ def __init__(self, token, auth_url,
self.astakos_base_url = parsed_auth_url.netloc
self.scheme = parsed_auth_url.scheme
self.conn_class = conn_class
if headers is not None:
self.headers = copy(headers)
else:
self.headers = None

# Initialize astakos api prefixes
# API urls under auth_url
Expand Down Expand Up @@ -250,9 +254,15 @@ def _call_astakos(self, request_path, headers=None,
method, request_path, hashed_token.hexdigest(), headers,
body if log_body else "(not logged)")

if self.headers:
request_headers = copy(self.headers)
else:
request_headers = {}

if headers is not None:
request_headers.update(headers)

# Check Input
if headers is None:
headers = {}
if body is None:
body = {}
# Initialize log_request and log_response attributes
Expand All @@ -261,7 +271,7 @@ def _call_astakos(self, request_path, headers=None,

# Build request's header and body
kwargs = {}
kwargs['headers'] = copy(headers)
kwargs['headers'] = request_headers
kwargs['headers']['X-Auth-Token'] = self.token
if body:
kwargs['body'] = copy(body)
Expand Down

0 comments on commit 2efd4f7

Please sign in to comment.