How to create a REST API with a custom query and processing?

EN | ES

Intro

In this tutorial we will see how to create a method in a REST API with parameters and with a post-processing.

Create API 

For the example we will use the public ontology HelsinkiPopulation.

To do this:

  1. We will access the CloudLab Environment Control Panel with a Developer user: 

https://lab.onesaitplatform.com/controlpanel

  1. We will go to 

My APIs section in the DEVELOPMENT menu and select CREATE.

  1. 3) In the API data, we will select the ontology 

HelsinkiPopulation:

   

Create QUERY for API

  1. Then, in Operations, we will mark 

QUERY (CUSTOM) to create a custom operation:

   

As we are working with the HelsinkiPopulation ontology, which returns the population in Helsinki by years (we can see it by going to the Query Tool):

   

We will make a method that returns the population when we pass the year as a parameter. The query would be:

   

  1. We return to the operation and complete the data of the operation, to pass the year as a parameter, we will mark it like this {$ year}

   

The platform will ask us for the data type of the parameter and whether it is a query in SQL or not:

   

We select SAVE the API.

Invoke the API

  1. Now we can invoke it with a User Token. We can obtain the User Token from the option: .

   

   To invoke it, we will select the SWAGGER option:

  We will select HTTPS in the invocation:

  

  We will select the method:

  

  and click . This will request the User Token and the year:

   

   When invoking it, we will get the results:

   

Craate a Processing for the API 

  1. Now let's see how to add a post-processed to this method. For that, we will edit the API method:

   

  and enable the Process by selecting  . This will enable the processing of the method in JavaScript (via Nashorn).

  The data results of the invocation to the method are returned in an attribute called data and we must end up returning a String. An example processing is generated.

  

  We will modify this process to keep only the year and the population, by deleting the root element:

  

Upon invoking the API again, we will now get:

 Â