Skip to content

Commit

Permalink
Specify check for invalid api key and update prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Amund211 committed Jul 26, 2023
1 parent 3560be5 commit 964766b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/prism/overlay/antisniper_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ class Flag(Enum):
SESSION.headers.update({"Reason": f"Prism overlay {VERSION_STRING}"})


def is_invalid_api_key_response(response: requests.Response) -> bool: # pragma: nocover
"""Return True if the response is a 403: invalid api key"""
if response.status_code != 403:
return False

try:
response_json = response.json()
except JSONDecodeError:
return False

if response_json.get("success", None) is not False:
return False

cause = response_json.get("cause", None)

if not isinstance(cause, str) or "invalid api key" not in cause.lower():
return False

return True


def set_denick_cache(nick: str, uuid: str | None) -> str | None:
"""Set the cache entry for nick, and return the uuid"""
with DENICK_MUTEX:
Expand Down Expand Up @@ -109,7 +130,7 @@ def get_antisniper_playerdata(
if response.status_code == 404:
raise HypixelPlayerNotFoundError(f"Could not find a user with {uuid=} (404)")

if response.status_code == 403:
if is_invalid_api_key_response(response):
raise HypixelAPIKeyError(
f"Request to Hypixel API failed with status code {response.status_code}. "
f"Assumed invalid API key. Response: {response.text}"
Expand Down
2 changes: 1 addition & 1 deletion src/prism/overlay/output/overlay/run_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_new_data() -> tuple[bool, list[InfoCellValue], list[OverlayRowData] | No
if controller.api_key_invalid:
info_cells.append(
InfoCellValue(
text="Invalid API key. Use /api new",
text="Invalid API key. Update it in the AntiSniper discord",
color="red" if time.monotonic() % 2 > 1 else "white",
url=None,
)
Expand Down

0 comments on commit 964766b

Please sign in to comment.