Skip to content

Commit

Permalink
Fix bug causing old token to be used with requests after refreshing (#…
Browse files Browse the repository at this point in the history
…530)

* Fix bug causing old token to be used with requests after refreshing

* Improved exception handling

* Update token cache to use human-readable URL

---------

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Jun 25, 2024
1 parent 21bcc64 commit ed8d889
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion neon_utils/hana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def set_default_backend_url(url: Optional[str] = None):


def _get_client_config_path(url: str):
url_key = hash(url)
url_key = url.split('/')[2]
return join(xdg_cache_home(), "neon", f"hana_token_{url_key}.json")


Expand Down Expand Up @@ -132,6 +132,11 @@ def _refresh_token(backend_address: str):
raise ServerException(f"Error updating token from {backend_address}. "
f"{update.status_code}: {update.text}")
_client_config = update.json()

# Update request headers with new token
global _headers
_headers['Authorization'] = f"Bearer {_client_config['access_token']}"

client_config_path = _get_client_config_path(backend_address)
with open(client_config_path, "w+") as f:
json.dump(_client_config, f, indent=2)
Expand Down Expand Up @@ -184,4 +189,7 @@ def request_backend(endpoint: str, request_data: dict,
return resp.json()
except Exception as e:
LOG.error(e)
# Clear cached config to force re-evaluation on next request
_client_config = {}
_headers = {}
raise ServerException(f"Error response {resp.status_code}: {resp.text}")

0 comments on commit ed8d889

Please sign in to comment.