Extended logging and documentation for api handler

This commit is contained in:
agres
2025-03-23 23:05:29 +01:00
parent c65a1a8c8b
commit f714200225
+41 -8
View File
@@ -1,9 +1,13 @@
import logging as log from typing import Union
import requests import requests
from logger import LoggerWrapper
def get_last_played_track(bearer_token: str, url: str = "https://api.spotify.com/v1/me/player/recently-played?limit=50") -> dict: log = LoggerWrapper()
def get_last_played_track(bearer_token: str, url: str = "https://api.spotify.com/v1/me/player/recently-played?limit=50") -> Union[dict, None]:
""" """
This function returns the last played track based on the limit size This function returns the last played track based on the limit size
@@ -16,12 +20,17 @@ def get_last_played_track(bearer_token: str, url: str = "https://api.spotify.com
'Authorization': f'Bearer {bearer_token}' 'Authorization': f'Bearer {bearer_token}'
} }
try:
log.debug(f"GET Request: {url}")
response = requests.get(url, headers=header) response = requests.get(url, headers=header)
response_json = response.json() response_json = response.json()
return response_json return response_json
except requests.exceptions.RequestException as e:
log.error(f"Error in get_last_played_track: {e}")
return None
def get_track_information(track_id: str, bearer_token: str) -> dict: def get_track_information(track_id: str, bearer_token: str) -> Union[dict, None]:
""" """
This function returns the track information based on the track id This function returns the track information based on the track id
@@ -35,12 +44,17 @@ def get_track_information(track_id: str, bearer_token: str) -> dict:
'Authorization': f'Bearer {bearer_token}' 'Authorization': f'Bearer {bearer_token}'
} }
try:
log.debug(f"GET Request: {url}")
response = requests.get(url, headers=header) response = requests.get(url, headers=header)
response_json = response.json() response_json = response.json()
return response_json return response_json
except requests.exceptions.RequestException as e:
log.error(f"Error in get_track_information: {e}")
return None
def get_artist_information(artist_id: str, bearer_token: str) -> dict: def get_artist_information(artist_id: str, bearer_token: str) -> Union[dict, None]:
""" """
This function returns the artist information based on the artist id This function returns the artist information based on the artist id
@@ -53,13 +67,17 @@ def get_artist_information(artist_id: str, bearer_token: str) -> dict:
header = { header = {
'Authorization': f'Bearer {bearer_token}' 'Authorization': f'Bearer {bearer_token}'
} }
try:
log.debug(f"GET Request: {url}")
response = requests.get(url, headers=header) response = requests.get(url, headers=header)
response_json = response.json() response_json = response.json()
return response_json return response_json
except requests.exceptions.RequestException as e:
log.error(f"Error in get_artist_information: {e}")
return None
def get_album_information(album_id: str, bearer_token: str) -> dict: def get_album_information(album_id: str, bearer_token: str) -> Union[dict, None]:
""" """
This function returns the album information based on the album id This function returns the album information based on the album id
@@ -73,12 +91,17 @@ def get_album_information(album_id: str, bearer_token: str) -> dict:
'Authorization': f'Bearer {bearer_token}' 'Authorization': f'Bearer {bearer_token}'
} }
try:
log.debug(f"GET Request: {url}")
response = requests.get(url, headers=header) response = requests.get(url, headers=header)
response_json = response.json() response_json = response.json()
return response_json return response_json
except requests.exceptions.RequestException as e:
log.error(f"Error in get_album_information: {e}")
return None
def get_multiple_field_information(bearer_token: str, api_type: str, limit: int, *track_ids) -> dict: def get_multiple_field_information(bearer_token: str, api_type: str, limit: int, *track_ids) -> Union[dict, None]:
""" """
This function returns the track information based on the track id This function returns the track information based on the track id
@@ -88,13 +111,18 @@ def get_multiple_field_information(bearer_token: str, api_type: str, limit: int,
""" """
if len(track_ids) > limit: if len(track_ids) > limit:
log.error('Passed more than 20/50 ids to get_multiple_field_information') log.error(f'exceeding the limit if ids {limit} for endpoint {api_type}')
return None return None
url_suffix = "ids=" url_suffix = "ids="
separator = "," separator = ","
try:
for track_id in track_ids: for track_id in track_ids:
url_suffix = url_suffix + track_id + separator url_suffix = url_suffix + track_id + separator
except Exception as e:
log.error(f"Failed setting up the url for multiple ids request."
f"Error: {e}")
return None
url = f"https://api.spotify.com/v1/{api_type}?{url_suffix}" url = f"https://api.spotify.com/v1/{api_type}?{url_suffix}"
url = url[:-len(separator)] url = url[:-len(separator)]
@@ -102,6 +130,11 @@ def get_multiple_field_information(bearer_token: str, api_type: str, limit: int,
'Authorization': f'Bearer {bearer_token}' 'Authorization': f'Bearer {bearer_token}'
} }
try:
log.debug(f"GET Request: {url}")
response = requests.get(url, headers=header) response = requests.get(url, headers=header)
response_json = response.json() response_json = response.json()
return response_json return response_json
except requests.exceptions.RequestException as e:
log.error(f"Error in get_multiple_field_information: {e}")
return None