Car data (RDW)

Home / Car data (RDW)
In this Chapter we will create an application which exposes an API to collect license plates and extend them with all public data that is available for them. For this application we will use the api available at www.overheid.io. Be sure to go there and register since an API key is necessasry in the DimML code.

Copy the following code to the sandbox

concept restAPI {
     match '*restapi*'

     plugin rest " 
        /create => restCreate
        /update => restUpdate
     "
}

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

     flow (json) 
     => code [
         Kenteken = `Kenteken?Kenteken:'geen kenteken'`@groovy,
        versie=`1`@groovy] 
    => sql["insert into expo_cars(kenteken) values(:Kenteken)", `dataSource`]
    => out
}

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

    flow (json) 
    => sql['select Kenteken from demo.expo_cars WHERE Prijs=0',`dataSource`,limit='100',batch='1'] 
    => filter['result']
    => split['result']
    => expand['result']
    => code[url = `"https://overheid.io/api/voertuiggegevens/"+Kenteken+"?ovio-api-key=$APIKEY"`@groovy]
    => http[url='@url',method='GET', headers = 'Accept: application/json']
    => code[Zitplaatsen = `getParam(result,"aantalzitplaatsen")`@groovy,
            Bpm = `getParam(result,"bpm")`@groovy,
            Kleur = `getParam(result,"eerstekleur")`@groovy,
            Naam = `getParam(result,"handelsbenaming")`@groovy,
            Brandstof = `getParam(result,"hoofdbrandstof")`@groovy,
            Zuinigheidslabel = `getParam(result,"zuinigheidslabel")`@groovy,
            Voertuigsoort = `getParam(result,"voertuigsoort")`@groovy,
            Merk = `getParam(result,"merk")`@groovy,
            Prijs = `getParam(result,"catalogusprijs")==""?0:getParam(result,"catalogusprijs")`@groovy
    ]
    => sql["UPDATE demo.expo_cars
        SET Zitplaatsen=:Zitplaatsen, 
            Bpm=:Bpm, 
            Kleur=:Kleur, 
            Naam=:Naam, 
            Brandstof=:Brandstof, 
            Zuinigheidslabel=:Zuinigheidslabel, 
            Voertuigsoort=:Voertuigsoort,
            Merk=:Merk,
            Prijs=:Prijs
        WHERE Kenteken=:Kenteken;",`dataSource`]
    => filter['Zitplaatsen','Bpm','Kleur','Naam','Brandstof','Zuinigheidslabel','Voertuigsoort','Merk','result']
    => console
    => out
}

def getParam  = {str, param => `
    if (str.indexOf('"'+param+'":"')>-1) {
        String temp = str.split('"'+param+'":"')[1]
        return temp.split('"')[0]
    } else if (str.indexOf('"'+param+'":')>-1) {
        String temp = str.split('"'+param+'":')[1]
        return temp.split(',')[0]
    } else {
        return ""
    }
`@groovy}

const dataSource = `{
    type: 'mysql',
    serverName: 'db.server.io',
    databaseName: 'name',
    user: 'user',
    password: 'password'
}`
The application first has a concept defining both REST API calls that are possible. The create URL stores the license plate passed as a parameter. With the following cURL command shows how the REST API is called and a license plate passed
http://baltar-dev.dimml.io/create?dimml.concept=//[sandboxid]@[domain]/restAPI&Kenteken=22-zjz-4
The sandbox id should be replaced by your specific sandbox ID (something like “sandbox/efgk…”. The domain should be replaced by the domain (file name) for which you publish your DimML application. As with any Rest API, there is no link to an actual website so the value is arbitrary. Furthermore the DimML code contains $APIKEY which should be replaced by the API KEY from www.overheid.io

In the DimML application the restUpdate concept looks at all the license plates (Kenteken) in the database, specifically at the ones which do not have a price yet (which means no data was available previously or the license plate is new since the last update). Note that several columns have been chosen in this example, but any setup can be used.

The Select query will result in an additional field result which contains all the rows of the query as a map. The filter statement will make sure only the query data is available, the split element will split the (1) input tuple into multiple input tuples for each of the rows of the query. Therefor the steps after the split query will be taken for each row of the query. Furthermore the expand statement will take the rows and add all the elements as field. As a result each row will be processed and all the columns of the rows are available as field (e.g. you can use Kenteken, Prijs as if they are vals).

The http element will perform the actual call to the API. That API will return a string (available in the field result). To get an actual parameter out of that string, an additional function is used. Once all parameters are defined, the data is stored in the database