From 580a88b1355c2ebfe902dd9f4fe7b4515edc77ab Mon Sep 17 00:00:00 2001 From: Willem Renes Date: Sun, 2 May 2021 13:22:48 +0200 Subject: Exit if server response is not OK. --- getspot.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'getspot.py') diff --git a/getspot.py b/getspot.py index 40012a5..13d8324 100644 --- a/getspot.py +++ b/getspot.py @@ -68,30 +68,42 @@ BASE_URL = 'https://api.spotify.com/v1/' PLAYLIST_ID = '37i9dQZF1EMdXVibVNv347' # temp -def get_access_token(): +def get_auth_response(client_id=CLIENT_ID, client_secret=CLIENT_SECRET): """ - Get the temporary access token from the Spotify servers - (POST request) + Get the auth response from the spotify servers, with token and timeout info """ - auth_response = requests.post(AUTH_URL, { 'grant_type': 'client_credentials', - 'client_id': CLIENT_ID, - 'client_secret': CLIENT_SECRET, + 'client_id': client_id, + 'client_secret': client_secret, }) - # convert the response to JSON - auth_response_data = auth_response.json() + if not auth_response.ok: + raise SystemExit('Response from server not OK:\n %s' + % auth_response.text) + + return auth_response.json() + + +def get_access_token(): + """ + Get the temporary access token from the Spotify servers + (POST request) + """ + + auth_response_data = get_auth_response() # save the access token access_token = auth_response_data['access_token'] - return access_token + return access_token, auth_response_data def main(): """ main function """ - token = get_access_token() + token, auth_response = get_access_token() print(token) + print("Token expires in {expires} seconds.".format( + expires=auth_response['expires_in'])) if __name__ == '__main__': -- cgit v1.2.3-70-g09d2