Skip to content

Commit

Permalink
trying to handle and refresh expired access token from vague 'Unautho…
Browse files Browse the repository at this point in the history
…rized' API response (#54)
  • Loading branch information
grmpflh27 authored Dec 19, 2023
1 parent 9af386a commit e9fae14
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions capella_console_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class NoRefreshTokenError(CapellaConsoleClientError):
NOT_AUTHORIZED_ERROR_CODE = "NOT_AUTHORIZED"
ORDER_VALIDATION_ERROR_CODE = "ORDER_VALIDATION_ERROR"

UNAUTHORIZED_MESSAGE = "unauthorized"

ERROR_CODES = {
INVALID_TOKEN_ERROR_CODE: AuthenticationError,
ORDER_EXPIRED_ERROR_CODE: OrderExpiredError,
Expand Down Expand Up @@ -107,6 +109,9 @@ def handle_error_response_and_raise(response):
code = DEFAULT_ERROR_CODE
data = {}

if message is not None and message.lower() == UNAUTHORIZED_MESSAGE and code == DEFAULT_ERROR_CODE:
code = INVALID_TOKEN_ERROR_CODE

# try to assign some more meaningful exception class by message
if code == DEFAULT_ERROR_CODE and message:
try:
Expand Down
1 change: 1 addition & 0 deletions capella_console_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def send(self, *fct_args, **kwargs):
# retry request
orig_request = fct_args[0]
orig_request.headers["authorization"] = self.headers["authorization"]
logger.info(f"retrying {fct_args[0]}")
ret = super().send(*fct_args, **kwargs)
return ret

Expand Down

0 comments on commit e9fae14

Please sign in to comment.