Skip to content
Snippets Groups Projects

Grdf Adict Konnector

This konnector fetches consumption measures from Grdf Adict API. This is an Oauth Konnector, meaning the authentication performed to access all data is made following an Oauth2 protocol. You can clone the project here.

You should also check Cozy's official documentations for konnectors : https://docs.cozy.io/en/tutorials/konnector/getting-started/ https://docs.cozy.io/en/tutorials/konnector/oauth/

The Oauth protocol does not take place in the konnector code, therefore it is also important to take a look at the proxy documentation to fully understand all the interactions that will be told below.

All the actions performed by the stack are targeted from pre-registered parameters, here is the list of all parameters needed by the stack to perform the Oauth protocol and allow the konnector to fetch data.

grdfgrandlyon account

On its first launch, following the Oauth Client Connect authentication.

  • The cozy stack calls the authentication_endpoint and start the oauth protocol, see proxy doc.
  • The account has now an access_token and an id_token from the oauth call

!!! info "" id _token is only given when requesting the token endpoint in _authorization_code- grant_type. This token holds several meta data, including the pce_id (id of user's meter) that will be needed further to fetch user's data.

  • Konnector starts, fails to find a pce_id in database on first launch therefore decodes the id_token to store the pce_id in db (see addData).

  • Konnector restarts, this time knowing a pce_id, it fails to fetch data because the access_token from oauth call has a reduced scope (only scoping for meta data requests).

  • Konnector launches a refresh call, the proxy knows to answer it with a client_credentials grant_type call to grdf /access_token.

  • Konnector restarts with everything needed in database to fetch data and store them in CouchDB.

  • Further jobs will not need to change scope again and will only ask for refresh tokens.

Konnector Flow summarized

On delete account

When deleting one's konnector account, src/onDeleteAccount.js is triggered. In order to facilitate a new oauth connection later on. It aims to remove user's consent between its grdf account and the Metropole de Lyon.

To do so, we use the secret credentials stored in account_type grdfgrandlyon.delete (see cozy official documentation to know how we create a special account_type secret) to generate an access_token with enough permission to revoke any consents given to the Metropole.

Launch on standalone

In the project repository type yarn standalone to launch the konnector without a cozy stack running.

Installation

Build the konnector with yarn build.

Install on easy-cozy (development purposes)

sudo docker cp /home/easy-cozy/fun/grdf-konnector/build/ $container_id:/tmp/grdf-oauth

sudo docker-compose exec cozy ./cozy konnectors install grdf-oauth --domain "$alphaDomain" file:///tmp/grdf-oauth $container_id

Install on production

Type yarn deploy to build on a dedicated branch. Build branch is stored as a docker image in a registry : Container Registry.

Konnector Methods

Method Description
start Init Function
getData Retrieve data from API
processData Parse data. Remove existing data from DB using hydrateAndFilter, store filtered data and return the list of filtered data
storeData Save data in the right doctype db and prevent duplicated keys
formateData Format data for DB storage and remove bad data
buildAggregatedData Retrieve and remove old data for a specific doctype and return an array of aggregated data
buildDataFromKey Format an entry for DB storage using key and value. For year doctype: key = "YYYY". For month doctype: key = "YYYY-MM"
resetInProgressAggregatedData Function handling special case.The temporary aggregated data need to be remove in order for the most recent one to be saved. Ex for com.grandlyon.grdf.year : { load: 76.712, year: 2020, ... } need to be replace by { load: 82.212, year: 2020, ... } after grdf data reprocess