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