K2 REST Service Broker with OneMap REST API Service

Introduction

Lately i have been exploring on retrieving the data through K2 REST service broker with OneMap REST services which return JSON object data.

Pre-Requisite

Before we proceed, you may wish to do the following:

  1. Have some basic knowledge on JSON
  2. Understand OneMap REST API services
  3. Understand how K2 REST service broker works
  4. Have some knowledge on K2 technologies

K2 REST Broker Installation

You may installed K2 REST Service Broker via this link. I used K2 REST Broker Beta 3 TR Installer.zip for my lab.

Once you had extracted the files, double click on SourceCode.Install.Patcher.exe to complete the installation. You will see the REST service broker created via the SmartObject Tester Tool.

REST

OneMap JSON Response file

I have come out with the OneMap JSON response file (aka Descriptor file) which will be used for creating the REST SmartObject. Please download it from here.

This file is created by referencing the example from Swagger.

HOW TO INSTALL

Register OneMap Rest Service Broker

1. Register the service instance under REST via SmartObject Tester Tool.

REST 2.png

2. Under Descriptor Location, fill in the file location where the downloaded JSON response file is situated.

REST 3

3. Rename the system and display name and click Add. (You should be seeing Add button instead of Update button)

REST 4

Create the SmartObject

1. Create the SmartObject for the newly created REST service instance

REST 6

2. Select only as below and click OK.

REST 5

3. Success message prompted, click OK.

REST 7

4. You will see the SmartObject being created as below:

REST 8

HOW TO USE

After the SmartObject is created using this service instance, you will be able to use it as a normal SmartObject function.

Get a Temporary Token

You will need a valid token, otherwise you will not be able to call the REST API successfully.

Go to this link , under OneMap REST APIs > click on “Get Token” to understand getting a temporary (24hrs expiry) token.

For testing purpose, click here to refresh the token expiry and your token will be:

qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr

BasicSearch SmartObject

Please refer to this link , under OneMap REST APIs > click on “Address Search” for the properties definition and explanation.

REST 10

Deserializing the JSON Data

JSON data was then return by the OneMap REST API service. With this, we can deserialize it and retrieve in a proper data format via the SmartObject.

The return results will be something like this:

{“SearchResults”:[{“PageCount”:”0″},{“SEARCHVAL”:”752350″,”CATEGORY”:”Building”,”X”:”26602.7623″,”Y”:”47967.2746″,”BLDG_NAME”:”HDB-SEMBAWANG”,”HBRN”:”350B CANBERRA ROAD”,”PostalCode”:”752350″}]}

To deserialize the return JSON data, we only require the text that is inside the square brackets (refer to above text in orange). Thus, you can write a function to trim off the unnecessary in order to automate the process.

In this case, we copy out the text in the square bracket.

1. Go to SmartObject Tester Tool and execute SearchResults SmartObject. Under method to execute, select “Serialize List to Array“.

2. Paste the text that was copied previously and click on Execute.

REST 11

3. Copy out the text results return from above step.

4. Under method to execute, select “Deserialize Typed Array“.

5. Paste the text that was copied previously and click on Execute.

REST 12

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s