in modular-service-cli/modular_service_cli/service/api_client.py [0:0]
def add_token(self, rec: urllib.request.Request,
header: str = 'Authorization'):
"""
Adds token to the given request instance. Refreshes the token if needed
:param header:
:param rec:
:return:
"""
# access token should definitely exist here because we check its
# presence before creating this class
at = cast(str, self._config.access_token)
rt = self._config.refresh_token
if JWTToken(at).is_expired() and rt:
_LOG.info('Trying to auto-refresh token')
resp = self.refresh(rt)
if resp.ok:
_LOG.info('Token was refreshed successfully. Updating config')
at = resp.data.get('access_token')
rt = resp.data.get('refresh_token')
dct = {'access_token': at}
if rt:
# if new one. This probably won't happen because Cognito
# does not return a new refresh token. But just in case
dct['refresh_token'] = rt
self._config.update(dct)
rec.add_header(header, at)