← Back to team overview

dhis2-devs team mailing list archive

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

 

---------- Forwarded message ----------
From: "Samson Bekele" <sbekele1@xxxxxxxxx>
Date: Jul 28, 2017 6:34 AM
Subject: Re: [Dhis2-devs] Fwd: [Dhis2-users] API calls using Node.js
examples
To: "Knut Staring" <knutst@xxxxxxxxx>
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



------------------------------
*From:* Knut Staring <knutst@xxxxxxxxx>
*To:* DHIS 2 developers <dhis2-devs@xxxxxxxxxxxxxxxxxxx>
*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@xxxxxxxxxxxxxxxxxx>
Date: Thu, Jul 27, 2017 at 3:15 PM
Subject: [Dhis2-users] API calls using Node.js examples
To: "dhis2-users lists.launchpad.net" <dhis2-users@xxxxxxxxxxxxxxxxxxx>


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
<https://launchpad.net/~dhis2-users>
Post to     : dhis2-users@lists.launchpad. net
<dhis2-users@xxxxxxxxxxxxxxxxxxx>
Unsubscribe : https://launchpad.net/~dhis2- users
<https://launchpad.net/~dhis2-users>
More help   : https://help.launchpad.net/ ListHelp
<https://help.launchpad.net/ListHelp>




-- 
Knut Staring
Dept. of Informatics, University of Oslo
Norway: +4791880522 <+47%20918%2080%20522>
Skype: knutstar
http://dhis2.org
_______________________________________________
Mailing list: https://launchpad.net/~dhis2-devs
Post to    : dhis2-devs@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~dhis2-devs
More help  : https://help.launchpad.net/ListHelp

References