Rest API

Home / Rest API
In this use case we are going to extend the DimML application to communicate with the HTTP protocol without requiring the SLOC. You can use DimML to create a Rest API. The first example we want to give is a DimML application which return the result of a MySQL query.

concept API {
    match '*restapi*'

    plugin rest " 
        /getPages => getPages
     "
}

concept getPages {

    plugin rest:json[flowName = 'json']

     flow (json)
     => sql['select * from Pages',`database`,limit='100',batch='1'] 
     => out
}
The code is quite compact as can be seen. Further details about curl can be found below, but with the following command the call to the DimML application is made:

curl -H "Content-Type: application/json" --referer http://www.domain.nl/restapi?dimml=sandbox/d994df195bd2129815029892f0df11f4439c14ee http://baltar-dev.dimml.io/getPages
In the second case we are going to extend the DimML application to communicate with the HTTP protocol without requiring the SLOC. You can use DimML to create a Rest API.

  1. To show that we can write 1 DimML file for both clickstream data collection and the configuration of a (RESTfull) API. Therefore we will add a new concept specifically for the API in the existing file.

Add the following code to the sandbox

concept restAPI {
     match '*restapi*'

     plugin rest " 
		/create => restCreate
     "
}

concept restCreate extends dataExtension {
	plugin rest:json[flowName = 'json']

     flow (json) 
     => nop (extend)

     flow (extendIn)
     => out
}

concept dataExtension {
  flow (extend) 
  => code [views=`1`] 
  => nop (extendIn)
}

concept all extends dataExtension {
 match '*'

 val versions = 'v2'

 flow
 => nop (extend)

 flow (extendIn)
 => debug

 plugin debug

}
This will create a Rest API which can be called with http://baltar-dev.dimml.io/create. With this code, the API will do only one thing: add the property ‘view’ to the existing data set. The value for this property is (currently) 1.

Also the last concept will be used for handling clickstream interactions. This concept also uses the data extension concept

  1. Download and install curl at http://curl.haxx.se/download.html (for Windows) With cURL we can make calls to the API we created
  1. Go to the folder where the cURL executable is located with command line. Execute the following statement
curl -H "Content-Type: application/json" -X POST --data "{\"calltype\":\"restapi\"}" --referer http://www.domain.nl/restapi?dimml=sandbox/d994df195bd2129815029892f0df11f4439c14ee http://baltar-dev.dimml.io/create
Where

  • domain.nl contains the domain for which you have written your DimML file
  • dimml=sandbox/XXX is the code you also see in the browser when opening the page in previous exercises
  • /restapi corrensponds to the match rule in the restAPI concept
  • /create matches the folder defined in the restAPI plugin
  • data “{ .. }” contains the dataset
  1. The result should be this:
[{“calltype”:”restapi”,”view”:”1”,”sql_count”:”1”}]
  1. Extend the data of the cURL command and validate that also with additional parameters as input. Also extend the view computation to incorporate a more extensive calculation
  1. Extend the concept dataExtension such that the data is written to an MySQL table. In the database there is a table “calltypes” defined with columns “calltype” and “views” which correspond to the properties in the DimML program.

Extend the restAPI such that the total number of views for each calltype is available. The new concept should look something like this

concept restRetrieve {
	plugin rest:json[flowName = 'json']

	flow (json)
		=> sql['select calltype,SUM(views) from calltypes GROUP BY calltype',`dataSource`,limit='100',batch='1'] 
		=> filter['result']
		=> split['result']
        	=> out
}

const dataSource = `{
    type: 'mysql',
    port: 3306,
    serverName: 'dimml-train.o2mc.io',
    databaseName: 'dimml_training',
    user: 'control',
    password: '963paarsOranje51'
}`
  1. Extend the rest API such that a function is included which provides the total number of views for a specific calltype