Skip to content

Commit

Permalink
Add detailed request error response if an HTTPError occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
tehkillerbee committed Sep 5, 2024
1 parent 21e0188 commit e111d43
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions tidalapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from urllib.parse import parse_qs, urlencode, urljoin, urlsplit

import requests
from requests.exceptions import HTTPError

from tidalapi.exceptions import *
from tidalapi.types import JsonObj
Expand Down Expand Up @@ -103,6 +104,7 @@ class Config:
api_pkce_auth: str = "https://login.tidal.com/authorize"
api_v1_location: str = "https://api.tidal.com/v1/"
api_v2_location: str = "https://api.tidal.com/v2/"
openapi_v2_location: str = "https://openapi.tidal.com/v2/"
api_token: str
client_id: str
client_secret: str
Expand Down Expand Up @@ -858,10 +860,13 @@ def track(
raise

def get_tracks_by_isrc(self, isrc: str) -> list[media.Track]:
"""Function to search all tracks with a specific ISRC code.
"""Function to search all tracks with a specific ISRC code. (eg. "USSM12209515")
This method uses the TIDAL openapi (v2). See the apiref below for more details:
https://apiref.developer.tidal.com/apiref?spec=catalogue-v2&ref=get-tracks-v2
:param isrc: The ISRC of the Track.
:return: Returns a list of :class:`.Track` objects that have access to the session instance used.
An empty list will be returned if no tracks matches the ISRC
"""
try:
res = self.request.request(
Expand All @@ -870,19 +875,28 @@ def get_tracks_by_isrc(self, isrc: str) -> list[media.Track]:
params={
"filter[isrc]": isrc,
},
base_url="https://openapi.tidal.com/v2/",
base_url=self.config.openapi_v2_location,
).json()

return [self.track(track["id"]) for track in res["data"]]
except requests.HTTPError:
log.warning("Wrong ISRC code '%s'", isrc)
raise
if res["data"]:
return [self.track(tr["id"]) for tr in res["data"]]
else:
log.warning("No matching tracks found for ISRC '%s'", isrc)
return []
except HTTPError:
log.error("Invalid ISRC code '%s'", isrc)
# Get latest detailed error response and return the response given from the TIDAL api
resp_str = self.request.get_latest_err_response_str()
if resp_str:
log.error("API Response: '%s'", resp_str)

def get_albums_by_barcode(self, barcode: str) -> list[album.Album]:
"""Function to search all albums with a specific UPC code.
"""Function to search all albums with a specific UPC code (eg. "196589525444")
This method uses the TIDAL openapi (v2). See the apiref below for more details:
https://apiref.developer.tidal.com/apiref?spec=catalogue-v2&ref=get-albums-v2
:param barcode: The UPC of the Album.
:param barcode: The UPC of the Album. Eg.
:return: Returns a list of :class:`.Album` objects that have access to the session instance used.
An empty list will be returned if no tracks matches the ISRC
"""
try:
res = self.request.request(
Expand All @@ -891,13 +905,19 @@ def get_albums_by_barcode(self, barcode: str) -> list[album.Album]:
params={
"filter[barcodeId]": barcode,
},
base_url="https://openapi.tidal.com/v2/",
base_url=self.config.openapi_v2_location,
).json()

return [self.album(album["id"]) for album in res["data"]]
if res["data"]:
return [self.album(alb["id"]) for alb in res["data"]]
else:
log.warning("No matching albums found for UPC barcode '%s'", barcode)
return []
except HTTPError:
log.warning("Wrong barcode '%s'", barcode)
raise
log.error("Invalid UPC barcode '%s'.", barcode)
# Get latest detailed error response and return the response given from the TIDAL api
resp_str = self.request.get_latest_err_response_str()
if resp_str:
log.error("API Response: '%s'", resp_str)

def video(self, video_id: Optional[str] = None) -> media.Video:
"""Function to create a Video object with access to the session instance in a
Expand Down

0 comments on commit e111d43

Please sign in to comment.