From f714200225db323f5f25bb934280a4fbd5dc2d09 Mon Sep 17 00:00:00 2001 From: agres Date: Sun, 23 Mar 2025 23:05:29 +0100 Subject: [PATCH] Extended logging and documentation for api handler --- src/spotify_api.py | 83 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/src/spotify_api.py b/src/spotify_api.py index b8a2f91..c7686b8 100644 --- a/src/spotify_api.py +++ b/src/spotify_api.py @@ -1,9 +1,13 @@ -import logging as log +from typing import Union 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 @@ -16,12 +20,17 @@ def get_last_played_track(bearer_token: str, url: str = "https://api.spotify.com 'Authorization': f'Bearer {bearer_token}' } - response = requests.get(url, headers=header) - response_json = response.json() - return response_json + try: + log.debug(f"GET Request: {url}") + response = requests.get(url, headers=header) + response_json = 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 @@ -35,12 +44,17 @@ def get_track_information(track_id: str, bearer_token: str) -> dict: 'Authorization': f'Bearer {bearer_token}' } - response = requests.get(url, headers=header) - response_json = response.json() - return response_json + try: + log.debug(f"GET Request: {url}") + response = requests.get(url, headers=header) + response_json = 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 @@ -53,13 +67,17 @@ def get_artist_information(artist_id: str, bearer_token: str) -> dict: header = { 'Authorization': f'Bearer {bearer_token}' } - - response = requests.get(url, headers=header) - response_json = response.json() - return response_json + try: + log.debug(f"GET Request: {url}") + response = requests.get(url, headers=header) + response_json = 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 @@ -73,12 +91,17 @@ def get_album_information(album_id: str, bearer_token: str) -> dict: 'Authorization': f'Bearer {bearer_token}' } - response = requests.get(url, headers=header) - response_json = response.json() - return response_json + try: + log.debug(f"GET Request: {url}") + response = requests.get(url, headers=header) + response_json = 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 @@ -88,13 +111,18 @@ def get_multiple_field_information(bearer_token: str, api_type: str, limit: int, """ 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 url_suffix = "ids=" separator = "," - for track_id in track_ids: - url_suffix = url_suffix + track_id + separator + try: + for track_id in track_ids: + 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 = 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}' } - response = requests.get(url, headers=header) - response_json = response.json() - return response_json + try: + log.debug(f"GET Request: {url}") + response = requests.get(url, headers=header) + response_json = response.json() + return response_json + except requests.exceptions.RequestException as e: + log.error(f"Error in get_multiple_field_information: {e}") + return None