Last Chapter: Creating a REST client to get Tracks from Spotify REST API - Part4 Save the Search Result
Git link: https://github.com/ecelg/InterSystems-IRIS-as-a-Spotify-REST-client
OK.... based on what I have done.... I am able to
1. Query Track information by making use of the Spotify API
2. Store the necessary data into my own album, artists, and track table
so.... what next?🤔 How about I set up my own REST API service on my IRIS for the other people to query my table?🤔🤨
ok... 1st... start from document Introduction to Creating REST Services
so we have 2 ways to implement a REST service on IRIS
Way 1. Using the OpenAPI 2.0 specification
Way 2. Creating a REST Service Manually
mummmm...both are ok for me.... how about trying the way 2?
OK Let's start with a simple HelloWorld!!!
1. Create a business service class rest.bs.RestService.cls extend the class %CSP.REST
Create a folder bs under the folder rest
.png)
Create a class RestService.cls under the folder bs
.png)
.png)
replace the code by the following
Class rest.bs.RestService Extends %CSP.REST
{
Parameter CONTENTTYPE = "application/json"
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/hello" Method="GET" Call="HelloWorld" />
</Routes>
}
ClassMethod HelloWorld() As %Status
{
#dim tJSON As %DynamicObject={}
set tJSON."test_message"="Hello World!!"
do ##class(%JSON.Formatter).%New().Format(tJSON)
return $$$OK
}
}
Save it
.png)
2. Create a Web Application for this REST service
Open the management portal
.png)
Go to the page Web Applications
.png)
.png)
Name : /csp/myapi
Namespace : demo
Dispatch Class: rest.bs.RestService
Save
.png)
Assign %All in the application roles
.png)
.png)
3. Test the api
GET localhost/csp/myapi/hello
localhost can be replace as the url of your server, /csp/myapi is the path that I defined in the web application, /hello
is the path of the class method I would like to call, which is defined in the class rest.bs.RestService
.png)
.png)
Yeah!!! My REST API is working!!!😆😆
Now... let's try to let our API users search our tracks by the id of the track table
.png)
and write it to JSON as response to our API users
4. add the routing path to the class rest.bs.RestService.cls
We add the /:id as the arguments(in this case, it will pass into the 1st argument) for query, this argument will pass into the class method Track(will be implement in the next session) for process
<Route Url="/spotify/track/:id" Method="GET" Call="Track" />
.png)
5. Add the Class Method Track
This method will look up the related data based on the argument /:id and return as JSON response
Add the following class method in to the class rest.bs.RestService.cls
ClassMethod Track(pId As %Integer) As %Status
{
kill a
set a=##class(rest.spotify.track).%OpenId(pId)
set b=""
do a.%JSONExportToString(.b)
kill a
w b
return $$$OK
}
save the class
.png)
6. Test it!!!!
GET localhost/csp/myapi/track/5
localhost can be replace as the url of your server, /csp/myapi is the path that I defined in the web application, /track
is the path of the class method I would like to call, which is defined in the class rest.bs.RestService, /5 is the argument for looking up the track by id
.png)
.png)
Yeah!!! working!!!!😀
Ok seems working well!!!😁 My final question is... how to let our user search for a track???
mummmm... at least.. we can search by name of the track, market, or maybe by artist
OK... let's add another route and class method to handle it!!
7. add the routing path to the class rest.bs.RestService.cls
<Route Url="/spotify/searchtrack" Method="GET" Call="SearchTrack" />
.png)
8. Add the Class Method SearchTrack
This method will look up the related data based on the parameters name, market, artist and return as JSON response
to get the parameters, you may use the code $GET(%request.Data({paramete_key},1),""), for example to get the parameter name $GET(%request.Data("name",1),"")
Add the following class method in to the class rest.bs.RestService.cls
ClassMethod SearchTrack() As %Status
{
#dim tname As %String=$GET(%request.Data("name",1),"")
#dim tmarket As %String=$GET(%request.Data("market",1),"")
#dim tartists As %String=$GET(%request.Data("artists",1),"")
set sname="%"_tname_"%"
set trowid=0
&sql(SELECT top 1 ID into :trowid FROM rest_spotify.track where name like :sname order by popularity desc)
set tarowid=""
if (tartists'="")
{
set sartists=tartists
&sql(SELECT top 1 ID into :trowid FROM rest_spotify.track where name like :sname and album->artists->name like :sartists order by popularity )
}
if tarowid'="" set trowid=tarowid
if (trowid="")
{
set tquery="offset=5&limit=10&query="_tname_"&type=track&market="_tmarket
set str=##class(rest.utli.requestUtli).getdata("Spotify","/search",tquery)
set std=##class(rest.utli.spotifyUtli).getSearchResult(str)
&sql(SELECT top 1 ID into :trowid FROM rest_spotify.track where name like :sname order by popularity desc)
}
if (trowid>0)
{
set a=##class(rest.spotify.track).%OpenId(trowid)
set b=""
do a.%JSONExportToString(.b)
w b
return $$$OK
}
return $$$OK
}
save the class
.png)
9. Test it again!!!
GET localhost/csp/myapi/searchtrack?name=APT&market=SG&artists=
localhost can be replace as the url of your server, /csp/myapi is the path that I defined in the web application, /track
is the path of the class method I would like to call, which is defined in the class rest.bs.RestService, name, market, artist are the parameter input for searching
.png)
.png)
Yeah!! working!!!😁
Let's try one more
GET localhost/csp/myapi/searchtrack?name=call me maybe&market=SG&artists=Carly Rae Jepsen
.png)