diff --git a/backend/auth/session.py b/backend/auth/session.py index 2f72fd9..b41ecf7 100644 --- a/backend/auth/session.py +++ b/backend/auth/session.py @@ -58,6 +58,14 @@ class SessionData(): return self.__access_token + def __eq__(self, other): + if not isinstance(other, SessionData): + return False + + return (self.__access_token == other.__access_token and + self.__refresh_token == other.__refresh_token and + self.__expires_in == other.__expires_in) + class SessionManager(): __current_session: SessionData diff --git a/backend/endpoints/spotify_api.py b/backend/endpoints/spotify_api.py index a6af4cd..82cafbf 100644 --- a/backend/endpoints/spotify_api.py +++ b/backend/endpoints/spotify_api.py @@ -1,4 +1,6 @@ import os +import urllib.parse +from email import header import requests from auth.session import SessionData, SessionManager @@ -45,9 +47,14 @@ def callback(code: str): expires_in = token_info.get("expires_in") # Salva access_token in sessione o database per usi futuri + old_session = SessionManager.instance().get_current_session() SessionManager.instance().set_session(SessionData(access_token, refresh_token, expires_in)) + session = SessionManager.instance().get_current_session() - return {} + return { + "changed": session != old_session, + "access_token": session.access_tokens, + } @music_router.get("/search") @@ -96,3 +103,21 @@ def current_song(): return requests.get(url, headers=header).json() except Exception as e: print(e) + +@music_router.get("/add_queue_track") +def add_queue_track(song_id: str): + try: + params = { + "uri": f"spotify:track:{song_id}" + } + url_encoded_params = urllib.parse.urlencode(params) + url = f"{SPOTIFY_AUTH_URL}/me/player/queue{url_encoded_params}" + + header = { + "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, + "Content-Type": "application/json" + } + + return requests.post(url, headers=header).json() + except Exception as e: + print(e) \ No newline at end of file