_get_id make more DRY and add info to error

This commit is contained in:
bvandercar-vt 2025-12-04 15:58:13 -07:00
parent 2917abeeaa
commit 5661f2576d

View File

@ -2229,28 +2229,31 @@ class Spotify:
return path return path
def _get_id(self, type, id: str) -> str: def _get_id(self, type, id: str) -> str:
uri_match = re.search(Spotify._regex_spotify_uri, id) for regexp, error_msg in [
if uri_match is not None: (Spotify._regex_spotify_uri, "URI"),
uri_match_groups = uri_match.groupdict() (Spotify._regex_spotify_url, "URL"),
if uri_match_groups['type'] != type: ]:
match = re.search(regexp, id)
if match is not None:
match_groups = match.groupdict()
if match_groups["type"] != type:
# TODO change to a ValueError in v3 # TODO change to a ValueError in v3
raise SpotifyException(400, -1, "Unexpected Spotify URI type.") raise SpotifyException(
return uri_match_groups['id'] 400,
-1,
url_match = re.search(Spotify._regex_spotify_url, id) (
if url_match is not None: f"Unexpected Spotify {error_msg} type."
url_match_groups = url_match.groupdict() f" (expected {type}, got {match_groups['type']})"
if url_match_groups['type'] != type: ),
raise SpotifyException(400, -1, "Unexpected Spotify URL type.") )
# TODO change to a ValueError in v3 return match_groups["id"]
return url_match_groups['id']
# Raw identifiers might be passed, ensure they are also base-62 # Raw identifiers might be passed, ensure they are also base-62
if re.search(Spotify._regex_base62, id) is not None: if re.search(Spotify._regex_base62, id) is not None:
return id return id
# TODO change to a ValueError in v3 # TODO change to a ValueError in v3
raise SpotifyException(400, -1, "Unsupported URL / URI.") raise SpotifyException(400, -1, f"Unsupported URL / URI. ({id})")
def _get_uri(self, type, id: str) -> str: def _get_uri(self, type, id: str) -> str:
if self._is_uri(id): if self._is_uri(id):