Git link: https://github.com/ecelg/InterSystems-IRIS-as-a-Spotify-REST-client
Recently, I come up an idea in my mind that how can I put my playlist on IRIS.🧐
At the same time, I was told to pay for my Spotify subscription💸💸... ooo.. how about to get some data from the Spotify API... so I started to do study about it.
Like most of the development, let's start from Documentation of the API https://developer.spotify.com/documentation/web-api
In order to get the data, i am required to request an access token from for the token endpoint URL.🧐
curl -X POST "https://accounts.spotify.com/api/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret"
but before that I should check out my client_id and my client_secret from the Dashboard
by Create app
.png)
then
.png)
.png)
check the api
.png)
check the setting
.png)
copy the client_id and client_secret
.png)
OK every thing is ready😀 Let's go!!!!!😁
1. Setup a Persistent class to store the API information
.png)
.png)
.png)
.png)
.png)
.png)
replace the code as following and save
Class rest.class.apiinfo Extends (%Persistent, %JSON.Adaptor)
{
Property apiname As %String
Property clientid As %String(%JSONFIELDNAME = "client_id")
Property clientsecret As %String(%JSONFIELDNAME = "client_secret")
Property granttype As %String(%JSONFIELDNAME = "grant_type")
Property tokentype As %String(%JSONFIELDNAME = "token_type")
Property accesstoken As %String(%JSONFIELDNAME = "access_token", MAXLEN = 200)
Property expiresin As %String(%JSONFIELDNAME = "expires_in")
Property refreshtoken As %String(%JSONFIELDNAME = "refresh_token", MAXLEN = 200)
Property authurl As %String(MAXLEN = 100)
Property authheader As %String(MAXLEN = 100)
Property apiurl As %String(MAXLEN = 100)
Property refreshbefore As %String
Property updateat As %String
ClassMethod checkRowid(apiname As %String = "") As %Integer [ Language = objectscript ]
{
set rtn=0
set rowid=""
&sql(select id into :rowid from rest_class.apiinfo where apiname=:apiname )
if rowid'="" set rtn=rowid
return rtn
}
}
2. Insert the API information into the table
.png)
.png)
insert the api information into the table
.png)
example SQL
insert into rest_class.apiinfo
(apiname, apiurl, authurl, clientid, clientsecret, granttype, authheader)
values
('Spotify', 'https://api.spotify.com/v1', 'https://accounts.spotify.com/api/token', 'b43bf136********************', '45ffde***************', 'client_credentials', 'application/x-www-form-urlencoded')
verify it
SELECT
ID, accesstoken, apiname, apiurl, authheader, authurl, clientid, clientsecret, expiresin, granttype, refreshbefore, refreshtoken, tokentype, updateat
FROM rest_class.apiinfo
3. Setup a REST client to check out the token
It seems there are many options,
Option 1
Creating REST Operations in Productions, but how to insert the header "Content-Type: application/x-www-form-urlencoded" 😓. Let me check further Using the HTTP Outbound Adapter, ok.... seems... need to check further... Using the HTTP Response, ... oooo sorry... really cannot understand as there no step by step example for me.... give up.😭
Option 2
Python request, seems much easier to understand, let's start with option 2
3a. Install the requests library
If you need to setup your own python for IRIS after version 2024.3 you may reference to Use the Flexible Python Runtime Feature for IRIS on Windows Server
In the powershell type the following to install the requests library
python -m pip install requests
.png)
In the powershell type the following to install the iris library
python -m pip install iris
.png)
3b. Write a %RegisteredObject class for check out the token
create the folder utli under the folder rest
.png)
.png)
create the class requestUtli.cls
.png)
.png)
.png)
write the class method checkoutToken
Class rest.utli.requestUtli Extends %RegisteredObject
{
ClassMethod checkoutToken(apiname = "", withgrandtype = 1) As %String [ Language = python ]
{
#w ##class(rest.utli.requestUtli).checkoutToken("Spotify")
import requests
import json
import iris
# 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.authurl
params=""
if withgrandtype==1:
#print ('grandtype ='+a.granttype)
params="grant_type="+a.granttype+"&client_id="+a.clientid+"&client_secret="+a.clientsecret
else:
#print ('without grandtype')
params="client_id="+a.clientid+"&client_secret="+a.clientsecret
contenttype=a.authheader
headers={"Content-Type":contenttype}
api_url=api_baseurl+"?"+params
#print(api_url)
del a
# post it
response = requests.post(api_url,headers=headers, verify=False)
#print(response)
if response.status_code==200:
return json.dumps(response.json())
else:
return response.status_code
}
}
Save the code
.png)
4. Open a terminal to test if it is working or not
.png)
run the following command for testing
w ##class(rest.utli.requestUtli).checkoutToken("Spotify")
.png)
Yeah!! the token is check out successfully!!!!😁