← Back to team overview

dhis2-devs team mailing list archive

Re: [Dhis2-users] How to implement interoperability between 2 DHIS2 instance

 

Hi Vunda

Can you share what you got so far and what you would like to do eventually?
Maybe data or metadata synchronization would be an option?
https://docs.dhis2.org/master/en/user/html/dhis2_user_manual
_en_full.html#dataAdmin_dataSync
https://docs.dhis2.org/master/en/user/html/dhis2_user_manual
_en_full.html#metadata_sync

I attached a very basic Python script that just adds "myChange" to all
names of dataElements that begin with "ANC" (on Demo server).

To run it, you need to install the library *requests* (it's a very good
library for making API calls but you can use built-in methods as well) with
pip:

1) Install pip: https://pip.pypa.io/en/stable/installing/
2) In a terminal: pip install requests
3) Change directories to the folder where you downloaded de-renamer.py with cd
/path/to/folder
4) Inspect the code and what it does
5) In the same terminal:  python de-renamer.py

Good luck! Let us know how it went.

David


On Mon, Feb 27, 2017 at 9:23 PM, vunda limbe <vundalon@xxxxxxxxx> wrote:

> It seems that david change is mail adress.
>
> On Mon, Feb 27, 2017 at 9:22 PM, vunda limbe <vundalon@xxxxxxxxx> wrote:
>
>> Hi David and Everybody,
>>
>> I begun to learn what you told me, but I'm stucked at the begining. Do
>> you have an example of script that I can based on to build mine that work?
>> Thanks for your support.
>>
>> On Tue, Apr 5, 2016 at 9:18 AM, David Huser <david.huser@xxxxxxxxxxxx>
>> wrote:
>>
>>> Hi Vunda
>>>
>>> The nature of a Web API is its openness to be used by any software. As a
>>> start, I would start in trying to understand how such an API works, which
>>> is based on "HTTP REST" principles.
>>> REST in layman's terms: http://www.looah.com/source/view/2284
>>> REST basics (IBM developers): https://www.ibm.com/developerw
>>> orks/library/ws-restful/index.html
>>> Specification for DHIS2: https://dhis2.github.io/dhis2-
>>> docs/master/en/developer/html/dhis2_developer_manual_full.html
>>>
>>> As an example, the programming language Python can be used, which is
>>> installable on any system and is on Ubuntu by default. DHIS2 supports the
>>> transportation format JSON. There's a new library on github (
>>> https://github.com/dhis2/dhis2-python, but have not tried it yet) or
>>> you can write your own.
>>> So a basic procedure would be:
>>>
>>> myscript.py
>>> 1. load a configuration of both APIs (username, url, passwords)
>>> 2. make a GET request to a specific endpoint of MoH API
>>> 3. validate, transform,... the received data
>>> 4. make a POST request to another specific endpoint of your other
>>> instance's API
>>>
>>> and then install a cronjob which calls this script with "* * * * *
>>> python myscript.py"
>>>
>>> Further extensions could be logging to a file and a mail notification if
>>> something goes wrong.
>>>
>>> I hope this can help a bit.
>>>
>>> Best regards
>>> David
>>>
>>>
>>>
>>>
>>> On 03.04.2016 08:34, vunda limbe wrote:
>>>
>>> Hi All,
>>>
>>>
>>> I'm newbie on DHIS2. I implemented an instance that work for my
>>> organisation and I need to Interoperate with MOH instance to retrieve data
>>> each month for some health facilities.
>>> I need guidance on how to set up interoperability by using web API and
>>> how to create a cronjob that will fetch data from MOH instance to my
>>> instance each month.
>>>
>>> Could you please help me to find ressources (course, tutorials, step by
>>> step code, etc.) and advises on how to implement?
>>>
>>> Thank you all for your help.
>>>
>>>
>>> _______________________________________________
>>> Mailing list: https://launchpad.net/~dhis2-users
>>> Post to     : dhis2-users@xxxxxxxxxxxxxxxxxxx
>>> Unsubscribe : https://launchpad.net/~dhis2-users
>>> More help   : https://help.launchpad.net/ListHelp
>>>
>>>
>>>
>>>
>>
>


-- 


*David Huser*
DHIS2 Support Specialist
dhuser@xxxxxxxxxxxxxx | https://baosystems.com | Skype: dafhus | 2900 K
Street, Suite 406, Washington D.C. 20007
#!/usr/bin/python

import json, requests

# config
baseurl = 'https://play.dhis2.org/demo'
username = 'admin'
password = 'district'

#fetch data elements
req = requests.get(url='{}/api/dataElements?paging=false&fields=id,name,shortName,aggregationType,domainType,valueType,categoryCombo&filter=name:^like:ANC'.format(baseurl), auth=(username, password))
data_elements = req.json()

#rename data elements
for elem in data_elements['dataElements']:	
	elem['name'] = "{} myChange".format(elem['name'])

print("Written {} elements".format(len(data_elements['dataElements'])))

#dump import file
with open('renamed_upload.json', 'w') as f:
	rn = json.dump(data_elements, f, indent=4)

#post file

req2 = requests.post(url='{}/api/metadata'.format(baseurl), json=data_elements, auth=(username, password))
print(req2.text)

References