Consume REST service (Angular)
This guide demonstrates how to consume a custom REST service and authenticate via OAuth. It requires some knowledge about Radzen as it covers advanced topics. Check the quickstart guide for the basics.
We will create a Spotify player application which will show the latest releases and allow us to play the tracks.
Running this application requires a Spotify or Facebook account.
How to build the application
- Step 1: Create and configure the application
- Step 2: Add the REST service
- Step 3: Create the New Releases page
- Step 4: Create the Tracks page
Step 1: Create and configure the application
Create a new Radzen application by following the first step from the quickstart guide. Name the application Spotify Player
Step 2: Add the REST service
In this step we will add a new REST data source which consumes two Spotify endpoints:
- Open the Spotify Player application.
- Click data to go to My DataSources.
- Click new to add a new data source.
- Select REST.
- Set Name to Spotify.
- Set the REST endpoint as
https://api.spotify.com/v1/
. - Set Authorization to OAuth.
- Set ClientID to
da4bd9113dec43578cca7c59c6bf6e44
. - Set Authorization Url to
https://accounts.spotify.com/authorize
. - Click Next
Now we have to describe the responses that those two REST endpoints return. We will do so in the second step of the data source creation wizard by defining a few schemas.
First define the schemas returned by the Get a List of New Releases endpoint endpoint. A sample response is available in the Spotify documentation. We will use a few of the response properties and start defining schemas inside out - from the simplest objects to the ones that contain them.
First describe the images
property. It is an array of objects:
{
"height": 640,
"url": "https://i.scdn.co/image/e6b635ebe3ef4ba22492f5698a7b5d417f78b88a",
"width": 640
}
We only need the url
property for the purpose of this demo.
- Click add schema.
- Set Name to Image.
- Click the + button to add a new Property.
- Set the property Name to url.
Now describe the artists
property. We only need name
.
- Click add schema.
- Set Name to Artist.
- Click the + button to add a new Property.
- Set the property Name to name.
We are now ready to define the Album schema.
- Click add schema.
- Set Name to Album.
- Click the + button to add a new Property.
- Set the property Name to album_type.
- Add a new property.
- Set the property Name to artists. Set Type to Artist. Check Is Array. This means that the
artists
property is an array ofAlbum
schemas. - Add a new property.
- Set the property Name to id.
- Add a new property.
- Set the property Name to images. Set Type to Image. Check Is Array.
- Add a new property.
- Set the property Name to name.
Define the remaining schemas.
- Add a new schema.
- Set Name to Albums
- Add a new property which is array of Album and is named items.
- Add a new schema.
- Set Name to AlbumsResponse
- Add a new property of Albums type named albums.
We are ready with the responses of the Get a List of new Releases endpoint!
Now we have to define the actual resource that endpoint represents.
- Click Next.
- Click add resource.
- Set Path to
browse/new-releases
which is the URL of the Get a List of new Releases endpoint. - Click the + button next to Operations to define a new operation with that path.
- Set Name to getNewReleases. Radzen will create a function with this name that invokes this operation.
- Set Method to GET.
- Click the + button next to Responses to add a new response.
- Set HTTP Status to 200. Set schema to AlbumsResponse.
Click Finish to complete the data source creation.
Step 3 Create the New Releases page
In this step we will create a new page, add a grid and bind it to the result of the getNewReleases
operation.
- Add a new page called New Releases.
- Create a new page property called releases which is set to the result of the
getNewReleases
invokation. - Add new handler of the page Load event.
- Set Type to Invoke data source method.
- Set Name to getNewReleases.
- Add Then handler.
- Set Type to Set property.
- Set Name to releases.
- Set Value to
${result.albums.items}
.
Add the data grid which will display the new releases.
- Drag and drop a DataGrid component and set its Data property to releases.
- Add a column that will show the album image.
- Click the + button next to the Columns property to add a new grid column.
- Set Property to images.
- Set Title to
- Set Template to
<img src="${data.images[2].url}">
. - By default a data grid column column displays the property value as is. To display it as an image we set the Template property.
- Add a column that will show the album name.
- Click the + button next to the Columns property to add a new grid column.
- Set Property to name.
- Set Title to Name.
- Add a column that will show the first artist name.
- Click the + button next to the Columns property to add a new grid column.
- Set Property to artists.
- Set Title to Artist.
- Set Template to
${data.artists[0].name}
.
- Add a column that will show the album type.
- Click the + button next to the Columns property to add a new grid column.
- Set Property to album_type.
- Set Title to Type.
We are done! Run the application to see the latest Spotify releases.
Step 4: Create the Tracks page
Now create the page which will display the tracks of a release and allow us to play them. We will use the Get an Album’s tracks endpoint so we have to define it’s schemas and operations first.
- Click data to go to the My DataSources screen.
- Click Spotify to update the Spotify data source.
- Click Next to skip the first step.
- Add a new schema called Track.
- Add two properties: name and preview_url.
- Add a new schema called TracksResponse.
- Add an array property of type Track and named items.
Now define the resource and operation.
- Click Next.
- Click add resource.
- Set Path to
albums/{id}/tracks
. Notice the{id}
? This is a placeholder for a path parameter calledid
. For example an actual URL will look likealbums/123456/tracks
. - Click the + button next to Operations to define a new operation with that path.
- Set Name to getAlbumTracks.
- Set Method to GET.
- Click the + button next to Parameters to define a parameter for the
getAlbumTracks
operation. - Set Name to id.
- Set In to path. This means this parameter is part of the path. Remember the path has a placeholder for it.
- Click the + button next to Responses to add a response.
- Set HTTP Status to 200. Set schema to TracksResponse.
We are done with the data source. Click Finish to save the data source.
Next create the Tracks page and display the tracks in a data grid.
- Add a new page called Tracks. Exclude it from the navigation.
- Add a new page property called tracks.
- Add new handler of the page Load event.
- Set Type to Invoke data source method.
- Set Name to getAlbumTracks.
- Add Parameter.
- Set Name to id.
- Set Value to
${parameters.id}
. - Add Then handler.
- Set Type to Set property.
- Set Name to tracks.
- Set Value to
${result.items}
.
Add the data grid component that will display the tracks.
- Drag and drop a DataGrid component and set its Data property to tracks.
- Add a column that will show the track name.
- Click the + button next to the Columns property to add a new grid column.
- Set Property to name.
- Set Title to Name.
- Add a column that will allow the user to play the current track.
- Click the + button next to the Columns property to add a new grid column.
- Set Property to preview_url.
- Set Title to Play
- Set Template to
<audio controls *ngIf="data.preview_url" src="${data.preview_url}"></audio>
.
The final step is to configure the data grid from the New Releases page to display the tracks of a selected album.
- Load the New Releases page.
- Select the data grid component.
- Go to the Events
- Add a handler of the Select event.
- Set Type to Navigate to page.
- Set Path to Tracks.
- Add a parameter.
- Set Name to id.
- Set Value to
${event.id}
.
That’s all! Run the application to see the final result.