Article
· Jan 2 5m read

Creating a REST client to get Tracks from Spotify REST API - Part5 creating my own REST service

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

Create a class RestService.cls under the folder bs

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
{
	//define a DynamicObject
	#dim tJSON As %DynamicObject={}
	
	//set the keys and values
	set tJSON."test_message"="Hello World!!"
	// write to response
	do ##class(%JSON.Formatter).%New().Format(tJSON)
	
	return $$$OK
}

}

Save it

 


2. Create a Web Application for this REST service

Open the management portal

Go to the page Web Applications

Name : /csp/myapi

Namespace : demo

Dispatch Class: rest.bs.RestService

Save

Assign %All in the application roles

 


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

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

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" />

 

 


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
	//open an object with id
	set a=##class(rest.spotify.track).%OpenId(pId)
	
	//convert to JSON String
	set b=""
	do a.%JSONExportToString(.b)
	kill a
	
	// write to response
	w b
	return $$$OK
}

save the class

 


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

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" />

 



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
{
	// get the parameter "name"
	#dim tname As %String=$GET(%request.Data("name",1),"")
	// get the parameter "name"
	#dim tmarket As %String=$GET(%request.Data("market",1),"")
	// get the parameter "name"
	#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
	/*//debug
	//define a DynamicObject
	#dim tJSON As %DynamicObject={}
	//set the keys and values
	set tJSON."debug_message_name"=$replace(tname," ","%20")
	set tJSON."debug_message_market"=tmarket
	set tJSON."debug_message_rowid"=trowid
	// write to response
	do ##class(%JSON.Formatter).%New().Format(tJSON)
	*/
	if (trowid="")
	{
		/*//debug
		//define a DynamicObject
		#dim tJSON As %DynamicObject={}
		//set the keys and values
		set tJSON."debug_message_rowid"="not found"
		// write to response
		do ##class(%JSON.Formatter).%New().Format(tJSON)
		*/
		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)
	{
		//open an object with id
		set a=##class(rest.spotify.track).%OpenId(trowid)
		
		//convert to JSON String
		set b=""
		do a.%JSONExportToString(.b)
	
		// write to response
		w b
		return $$$OK
	}
	
	return $$$OK
}

save the class

 


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.RestServicename, market, artist are the parameter input for searching

Yeah!! working!!!😁

Let's try one more

GET localhost/csp/myapi/searchtrack?name=call me maybe&market=SG&artists=Carly Rae Jepsen

Discussion (0)1
Log in or sign up to continue