_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: ]:
# TODO change to a ValueError in v3 match = re.search(regexp, id)
raise SpotifyException(400, -1, "Unexpected Spotify URI type.") if match is not None:
return uri_match_groups['id'] match_groups = match.groupdict()
if match_groups["type"] != type:
url_match = re.search(Spotify._regex_spotify_url, id) # TODO change to a ValueError in v3
if url_match is not None: raise SpotifyException(
url_match_groups = url_match.groupdict() 400,
if url_match_groups['type'] != type: -1,
raise SpotifyException(400, -1, "Unexpected Spotify URL type.") (
# TODO change to a ValueError in v3 f"Unexpected Spotify {error_msg} type."
return url_match_groups['id'] f" (expected {type}, got {match_groups['type']})"
),
)
return 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):