New post

Find

Article
· Dec 30, 2024 2m read

Creating a REST client to get Tracks from Spotify REST API - Part3 Get some data (e.g. Artists)

Last Chapter: Creating a REST client to get Tracks from Spotify REST API - Part2 Save and Refresh Token

Git link: https://github.com/ecelg/InterSystems-IRIS-as-a-Spotify-REST-client
 

Ok, now I am pretty sure i have a valid token for making query.😀

Shall we try to query something from the API.

Again, its time to go through the API document https://developer.spotify.com/documentation/web-api/tutorials/getting-started

Search for Request artist data

the suggested code is like the following

curl "https://api.spotify.com/v1/artists/4Z8W4fKeB5YxbusRsdQVPb" \
     -H "Authorization: Bearer  BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11"

Which mean that we can get the artists by the id 4Z8W4fKeB5YxbusRsdQVPb and we need to attach our token as Authorization: Bearer  {our token} at the header

 


OK let start writing the class method getdata

Open the class rest.utli.requestUtli.cls

Add the following class method 

ClassMethod getdata(apiname = "", path = "", query = "0") As %String [ Language = python ]
{
	#w ##class(rest.utli.requestUtli).getdata("Spotify","/artists/4Z8W4fKeB5YxbusRsdQVPb")
	import requests
	import json
	import iris
	
	# get the token by apiname
	token=iris.cls('rest.utli.requestUtli').getToken(apiname)
	
	# get the apiinfo object by apiname
	rowid=iris.cls('rest.class.apiinfo').checkRowid(apiname)
	a=iris.cls('rest.class.apiinfo')._OpenId(rowid)
	
	# parameter perparation
	api_baseurl=a.apiurl
	api_url=api_baseurl+path
	if query!="0":
		api_url=api_baseurl+path+"?"+query
	
	atoken=a.tokentype+" "+token
	headers={"Authorization":atoken}
	
	del a
	
	# get it
	response = requests.get(api_url,headers=headers, verify=False)
	return json.dumps(response.json())
}

Save it

 


Now open a terminal and test the code

Run the following line

w ##class(rest.utli.requestUtli).getdata("Spotify","/artists/4Z8W4fKeB5YxbusRsdQVPb")

Yeah!!! 😀 Can get the detail of the artists!!!!

Let's go to the link to check who is this artist

https://open.spotify.com/artist/4Z8W4fKeB5YxbusRsdQVPb

Sorry... I am not sure if I know him/her.... can I search for someone I know?

 

Let try another path

w ##class(rest.utli.requestUtli).getdata("Spotify","/search","offset=2&limit=2&query=Ed%20Sheeran&type=artist&market=SG")

Let's go to the link to check

https://open.spotify.com/artist/2Vvz9VlewaR5wMQPrEZhC2

ok ...🤐 seems something making sense🤨

Discussion (0)1
Log in or sign up to continue
Digest
· Dec 30, 2024

Nuevas publicaciones en la Comunidad de InterSystems, 23-29 diciembre

23-29 diciembreWeek at a GlanceInterSystems Developer Community
Article
· Dec 30, 2024 2m read

Creating a REST client to get Tracks from Spotify REST API - Part2 Save and Refresh Token

Last Chapter:  Creating a REST client to get Tracks from Spotify REST API - Part1 Check out token

Git link: https://github.com/ecelg/InterSystems-IRIS-as-a-Spotify-REST-client

 

Ok... Now we can check out a token but it will be expired in 3600 seconds.

There are 2 questions come up🤔

1. How to save this token????🙄

2. How to refresh this token????🤨🤔 

 

Lets come back to the API document https://developer.spotify.com/documentation/web-api/tutorials/getting-started

Base on my understanding, this piece of API do not have a token called refresh_token, as a result, we can assume the logic like following

 


OK let start writing the class method getToken

Open the class rest.utli.requestUtli.cls

Add the following class method 

ClassMethod getToken(apiname As %String = "") As %String [ Language = objectscript ]
{
	//w ##class(rest.utli.requestUtli).getToken("Spotify")
	set token="0"
	set rowid=##class(rest.class.apiinfo).checkRowid(apiname)
	if (rowid'=0)
	{
		kill a
		set a=##class(rest.class.apiinfo).%OpenId(rowid)
		set token=a.accesstoken
		
		// check expirey
		set tokenexpired=0
		set refreashbefore=a.refreshbefore
		if (token="")||(refreashbefore="")
		{
			set tokenexpired=1
		}else
		{
			set rfdate=+$p(refreashbefore,",",1)
			set rftime=+$p(refreashbefore,",",2)
			if (rftime<+$p($h,",",2)) 
			{
				if (rfdate<=+$p($h,",",1)) set tokenexpired=1
			}else
			{
				if (rfdate<+$p($h,",",1)) set tokenexpired=1
			}			
			 
		}
		
		// checkout token
		if (token="")||(tokenexpired=1)
		{
			set b={}.%FromJSON(##class(rest.utli.requestUtli).checkoutToken("Spotify"))
			//w b."access_token",",",b."token_type",",",b."expires_in",!
			//set the update before
			set rbdate=$p($h,",",1)
			set rbtime=+$p($h,",",2)+b."expires_in"
			if (rbtime>86400)
			{
				set rbdate=+rbdate+1
				set rbtime=+rbtime-86400
			}
			//w rbdate_","_rbtime,!
			set a.accesstoken=b."access_token"
			set a.tokentype=b."token_type"
			set a.expiresin=b."expires_in"
			set a.refreshbefore=rbdate_","_rbtime
			set a.updateat=$h
			set std=a.%Save()
			
			return a.accesstoken
		}
	}
	return token
}

Save it

 


Now open a terminal and test the code

Run the following line

w ##class(rest.utli.requestUtli).getToken("Spotify")

Yeah!!! 😆It is working!!!!

Discussion (0)1
Log in or sign up to continue
Digest
· Dec 30, 2024
Digest
· Dec 30, 2024

InterSystems 开发者出版物,十二月 23 - 29, 2024,摘要