Skip to content

Commit e70b107

Browse files
author
Patrick J. McNerthney
committed
Allow for async refresh_api_key_hook methods.
1 parent b377772 commit e70b107

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

kubernetes_asyncio/client/api_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ async def __call_api(
165165
post_params.extend(self.files_parameters(files))
166166

167167
# auth setting
168-
self.update_params_for_auth(
168+
await self.update_params_for_auth(
169169
header_params, query_params, auth_settings,
170170
request_auth=_request_auth)
171171

@@ -548,7 +548,7 @@ def select_header_content_type(self, content_types, method=None, body=None):
548548
else:
549549
return content_types[0]
550550

551-
def update_params_for_auth(self, headers, queries, auth_settings,
551+
async def update_params_for_auth(self, headers, queries, auth_settings,
552552
request_auth=None):
553553
"""Updates header and query params based on authentication setting.
554554
@@ -566,7 +566,7 @@ def update_params_for_auth(self, headers, queries, auth_settings,
566566
return
567567

568568
for auth in auth_settings:
569-
auth_setting = self.configuration.auth_settings().get(auth)
569+
auth_setting = (await self.configuration.auth_settings()).get(auth)
570570
if auth_setting:
571571
self._apply_auth_params(headers, queries, auth_setting)
572572

kubernetes_asyncio/client/configuration.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from __future__ import absolute_import
1414

15+
import asyncio
1516
import copy
1617
import logging
1718
import sys
@@ -370,15 +371,17 @@ def logger_format(self, value):
370371
self.__logger_format = value
371372
self.logger_formatter = logging.Formatter(self.__logger_format)
372373

373-
def get_api_key_with_prefix(self, identifier, alias=None):
374+
async def get_api_key_with_prefix(self, identifier, alias=None):
374375
"""Gets API key (with prefix if set).
375376
376377
:param identifier: The identifier of apiKey.
377378
:param alias: The alternative identifier of apiKey.
378379
:return: The token for api key authentication.
379380
"""
380381
if self.refresh_api_key_hook is not None:
381-
self.refresh_api_key_hook(self)
382+
result = self.refresh_api_key_hook(self)
383+
if asyncio.iscoroutine(result):
384+
await result
382385
key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None)
383386
if key:
384387
prefix = self.api_key_prefix.get(identifier)
@@ -402,7 +405,7 @@ def get_basic_auth_token(self):
402405
basic_auth=username + ':' + password
403406
).get('authorization')
404407

405-
def auth_settings(self):
408+
async def auth_settings(self):
406409
"""Gets Auth Settings dict for api client.
407410
408411
:return: The Auth Settings information dict.
@@ -413,7 +416,7 @@ def auth_settings(self):
413416
'type': 'api_key',
414417
'in': 'header',
415418
'key': 'authorization',
416-
'value': self.get_api_key_with_prefix(
419+
'value': await self.get_api_key_with_prefix(
417420
'BearerToken',
418421
),
419422
}

kubernetes_asyncio/config/incluster_config_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,29 @@ def test_load_config(self):
9999
self.assertEqual(cert_filename, loader.ssl_ca_cert)
100100
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
101101

102-
def test_refresh_token(self):
102+
async def test_refresh_token(self):
103103
loader = self.get_test_loader()
104104
config = Configuration()
105105
loader.load_and_set(config)
106106

107107
self.assertEqual('Bearer ' + _TEST_TOKEN,
108-
config.get_api_key_with_prefix('BearerToken'))
108+
await config.get_api_key_with_prefix('BearerToken'))
109109
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
110110
self.assertIsNotNone(loader.token_expires_at)
111111

112112
old_token_expires_at = loader.token_expires_at
113113
loader._token_filename = self._create_file_with_temp_content(
114114
_TEST_NEW_TOKEN)
115115
self.assertEqual('Bearer ' + _TEST_TOKEN,
116-
config.get_api_key_with_prefix('BearerToken'))
116+
await config.get_api_key_with_prefix('BearerToken'))
117117

118118
loader.token_expires_at = datetime.datetime.now()
119119
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN,
120-
config.get_api_key_with_prefix('BearerToken'))
120+
await config.get_api_key_with_prefix('BearerToken'))
121121
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN, loader.token)
122122
self.assertGreater(loader.token_expires_at, old_token_expires_at)
123123

124-
def test_refresh_token_default_config_with_copies(self):
124+
async def test_refresh_token_default_config_with_copies(self):
125125
loader = self.get_test_loader()
126126
loader.load_and_set()
127127

@@ -132,7 +132,7 @@ def test_refresh_token_default_config_with_copies(self):
132132

133133
for config in configs:
134134
self.assertEqual('Bearer ' + _TEST_TOKEN,
135-
config.get_api_key_with_prefix('BearerToken'))
135+
await config.get_api_key_with_prefix('BearerToken'))
136136
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
137137
self.assertIsNotNone(loader.token_expires_at)
138138

@@ -142,13 +142,13 @@ def test_refresh_token_default_config_with_copies(self):
142142

143143
for config in configs:
144144
self.assertEqual('Bearer ' + _TEST_TOKEN,
145-
config.get_api_key_with_prefix('BearerToken'))
145+
await config.get_api_key_with_prefix('BearerToken'))
146146

147147
loader.token_expires_at = datetime.datetime.now()
148148

149149
for config in configs:
150150
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN,
151-
config.get_api_key_with_prefix('BearerToken'))
151+
await config.get_api_key_with_prefix('BearerToken'))
152152

153153
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN, loader.token)
154154
self.assertGreater(loader.token_expires_at, old_token_expires_at)

0 commit comments

Comments
 (0)