[Dhis2-users] API calls using Node.js examples

This is a good question for the dev list.

···

Knut Staring

Dept. of Informatics, University of Oslo

Norway: +4791880522

Skype: knutstar

http://dhis2.org

Hi

I don’t think we have any immediate plans to include this in the docs, the idea about using cURL is that it’s completely language neutral and any language these days should give you the tools to interact with the API.

That said, a simple example for fetch the data elements from a DHIS2 server would be (you need node-fetch installed):

const fetch = require( “node-fetch” );

const createAuthenticationHeader = (username, password) => {

return "Basic " + new Buffer( username + “:” + password ).toString( “base64” );

};

fetch( ‘https://play.dhis2.org/dev/api/dataElements?paging=false&filter=domainType:eq:TRACKER’, {

headers: {

“Accept”: “application/json”,

“Authorization”: createAuthenticationHeader( ‘admin’, ‘district’ )

}

} )

.catch( err => console.error( err ) )

.then( res => res.json() )

.then( res => {

console.log( res.dataElements.length );

} )

.catch( err => console.error( err ) );

If you would want to use an OAuth2 bearer token instead you would change the Authorization header.

···

On Thu, Jul 27, 2017 at 10:36 PM, Knut Staring knutst@gmail.com wrote:

This is a good question for the dev list.

---------- Forwarded message ----------
From: Jesus Solano-Roman asolano@broadinstitute.org
Date: Thu, Jul 27, 2017 at 3:15 PM
Subject: [Dhis2-users] API calls using Node.js examples
To: “dhis2-users lists.launchpad.netdhis2-users@lists.launchpad.net

Hello all.

I would like to know if someone could point out to examples or perhaps give an example of a typical Web API call done using Node.js

The examples in the documentation are either using cURL or using the direct browser “endpoint”, but usually do not take into account oAuth authentication (e.g. using a token instead of user credentials) and how to wrap all that into a Node.js request.

In the use case of developing a web application, both the “Bearer token” and the following API calls should be done using Node (or say, PHP) and not cURL. So for instance, this example from the documentation should actually exist as a Node request:

curl -H “Authorization: Bearer 07fc551c-806c-41a4-9a8c-10658bd15435” $SERVER/api/26/dataElements.json

Any help would be greatly appreciated.

Thanks,

Antonio Solano.

Broad Institute of MIT and Harvard, Boston, MA, USA.


Mailing list: https://launchpad.net/~dhis2-users

Post to : dhis2-users@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-users

More help : https://help.launchpad.net/ListHelp

Knut Staring

Dept. of Informatics, University of Oslo

Norway: +4791880522

Skype: knutstar

http://dhis2.org


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Morten Olav Hansen

Senior Engineer, DHIS 2

University of Oslo

http://www.dhis2.org

Cc:

I tried to call the api from node js application. Here the lesson I learned.

  1. CORS, because your node.js and the api are running in different server, the browser doesn’t allow to call the api especially you are developing your node.js application in local host. In order to avoid this issue, you need to configure a proxy, to tell your api location and port. But I think if you are working on real web domain, I think this is not an issue, or you modify the host file and instead of local host, you call testdev.com, may be you will not have this issue.

I can start by cloning the dhis app which are developed using react, redux and node and webpack, they wrap the api call through d2, and you can check the code and have a better understanding of the react application. But once you build the react application, you need to add your running node app url as white list in the dhis2, I think in setting, there is place called whitelist, other wise, it will not communication to the dhis2 application, I cloned the maintenance-app, I build, run it and it works fine.

In my case, I am using freezer instead of redux, and I use request module which are wrap around another js file. or you can use fetch or axios but you need to figure how once you called the api and getting the data, to put this data on your state management you selected, in the case of redux, in your store,

the final advice, you can also generate your own api directly from the postgres database, this depends on what data you are looking for, for example in my case I want to get all the user in the user table, I can create the route

{
path: ‘/api/users’,
method: ‘GET’,
handler: Handlers.getuserHandler,
config: {
auth: false
}
}

here is my handlers

Handlers.getuserHandler= function(request, reply){
//to run a query we just pass it to the pool
//after we’re done nothing has to be taken care of
//we don’t have to return any client to the pool or close a connection
pool.query(‘SELECT * from users’, function(err, result) {
if(err) {
return console.error(‘error running query’, err);
}
reply(result.rows);
});
};

I am using hapi as web application framework for this example, and pg module to connect to postgres, this works if your really know how the dhis2 postgres database are structured and you know how to get the data, or you need to go in the code and figure out how they structure the sql for end point.

I using pg module to help me to connect to the postgres database and query the database.

I am not actually working on real dhis2 application, I just like the application and working on the app in my free time. I deployed the instance in my digital ocean cloud server, and looking how to use it to manage my data I want to visualize. I think this might help you, if I confuse you more, you can discard my suggestion.

Thanks,

Samson

···

---------- Forwarded message ----------
From: “Samson Bekele” sbekele1@yahoo.com
Date: Jul 28, 2017 6:34 AM
Subject: Re: [Dhis2-devs] Fwd: [Dhis2-users] API calls using Node.js examples
To: “Knut Staring” knutst@gmail.com

From: Knut Staring knutst@gmail.com
To: DHIS 2 developers dhis2-devs@lists.launchpad.net
Sent: Thursday, July 27, 2017 9:37 AM
Subject: [Dhis2-devs] Fwd: [Dhis2-users] API calls using Node.js examples

This is a good question for the dev list.

---------- Forwarded message ----------
From: Jesus Solano-Roman asolano@broadinstitute.org
Date: Thu, Jul 27, 2017 at 3:15 PM
Subject: [Dhis2-users] API calls using Node.js examples
To: “dhis2-users lists.launchpad.netdhis2-users@lists.launchpad.net

Hello all.

I would like to know if someone could point out to examples or perhaps give an example of a typical Web API call done using Node.js

The examples in the documentation are either using cURL or using the direct browser “endpoint”, but usually do not take into account oAuth authentication (e.g. using a token instead of user credentials) and how to wrap all that into a Node.js request.

In the use case of developing a web application, both the “Bearer token” and the following API calls should be done using Node (or say, PHP) and not cURL. So for instance, this example from the documentation should actually exist as a Node request:

curl -H “Authorization: Bearer 07fc551c-806c-41a4-9a8c- 10658bd15435” $SERVER/api/26/dataElements. json

Any help would be greatly appreciated.

Thanks,

Antonio Solano.

Broad Institute of MIT and Harvard, Boston, MA, USA.


Mailing list: https://launchpad.net/~dhis2- users

Post to : dhis2-users@lists.launchpad. net

Unsubscribe : https://launchpad.net/~dhis2- users

More help : https://help.launchpad.net/ ListHelp


Knut Staring

Dept. of Informatics, University of Oslo

Norway: +4791880522

Skype: knutstar

http://dhis2.org


Mailing list: https://launchpad.net/~dhis2-devs
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : https://launchpad.net/~dhis2-devs
More help : https://help.launchpad.net/ListHelp