GithubHelp home page GithubHelp logo

davidhuser / dhis2.py Goto Github PK

View Code? Open in Web Editor NEW
15.0 5.0 10.0 295 KB

Generic and lightweight Python wrapper for the DHIS2 API using requests

License: MIT License

Python 100.00%
dhis2 python requests

dhis2.py's Issues

paging for /api/analytics/events/query fails with a KeyError

dhis2.py 2.0.2 -- 2.1.2

from dhis2 import Api

PROGRAM_UID = 'IpHINAT79UW'

def get_data_no_paging(api, params):
  result = api.get(f'analytics/events/query/{PROGRAM_UID}', params=params).json()
  print(result['rows'][0])


def get_data_paging(api, params):
  for page in api.get_paged(f'analytics/events/query/{PROGRAM_UID}', params=params, page_size=100):
    print(page['rows'][0])


def main():
  api = Api('play.dhis2.org/demo', 'admin', 'district')
  
  # doesn't work with this type of params
  #params = [
  #  ('dimension', 'pe:LAST_12_MONTHS'),
  #  ('dimension', 'ou:ImspTQPwCqd'),
  #  ('dimension', 'A03MvHHogjR.a3kGcGDCuk6'),
  #  ('stage', 'A03MvHHogjR'),
  #  ('outputType', 'EVENT')
  #]

  params = {
    'dimension': [
      'pe:LAST_12_MONTHS', 
      'ou:ImspTQPwCqd', 
      'A03MvHHogjR.a3kGcGDCuk6'
      ],
    'stage': 'A03MvHHogjR',
    'outputType': 'EVENT'
  }

  get_data_no_paging(api, params)
  get_data_paging(api, params)

if __name__ == '__main__':
  main()

results in:

['q0Hcikut16c', 'A03MvHHogjR', '2019-11-12 00:00:00.0', '2020-11-12 01:00:00.0', '2020-11-12 01:00:00.0', '', '0.0', '0.0', 'Ngelehun CHC', 'OU_559', 'DiszpKrYNg8', '5.0']

Traceback (most recent call last):
  File "main.py", line 41, in <module>
    main()
  File "main.py", line 38, in main
    get_data_paging(api, params)
  File "main.py", line 11, in get_data_paging
    for page in api.get_paged(f'analytics/events/query/{PROGRAM_UID}', params=params, page_size=100):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/dhis2/api.py", line 409, in page_generator
    page_count = page["pager"]["pageCount"]
KeyError: 'pager'

Remove assertion in loading of dish file

minor bug: Remove assertion statement:

... used assert to enforce interface constraints. However, assert is removed with compiling to optimised byte code (python -o producing *.pyo files). This caused various protections to be removed.

Test results:
>> Issue: [B101:assert_used] Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
   Severity: Low   Confidence: High
   Location: dhis2.py/dhis2/api.py:176
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html
175	            password = section["password"]
176	            assert all([baseurl, username, password])
177	        except (KeyError, AssertionError):

request param validation should accept lists of tuples

Thanks for the lovely library!

I found a limitation that should be simple to fix.
Many DHIS2 API endpoints allow repeated parameters, but this library does not.

For example, table 1.29 in the 1.17.6. Reading data values section of DHIS2 API docs describes several parameters that "Can be repeated any number of times." (such as dataSet, orgUnit, period, etc)

Luckily, the requests library supports repeated parameters if the caller provides as a list of tuples: http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

However, the _validate_request method raises if params is not a dict

Org Unit Level and Paging issue

When querying a dhis2 instance with ~4200 organisation units, I wanted to find the root org unit, namely the single one with level = 1. Using the get_paged function (even though not necessary), the get_paged function returns the single root orgunit info multiple times, base on the number of pages needed for the full org unit tree - in other words, if I set the page_size to 50, I get 85 copies of the root (~4200/50) and if I set it 500, I get 9 copies, (~4200/500)

import_response_ok is fooled by new (?) return format from the api

I am seeing this on DHIS2 2.40.0:
import_response_ok assumes that the number of imports can be found through response['importCount']
Unfortunately, this has apparently been changed to response['response']['importCount'].
(There two responses, the parameter-name of import_response_ok and the element returned by the api ....)

api.delete does not allow payload

Description

Cannot remove dataElements from dataElementGroups as in DHIS2 dev guide, section:
1.9.4.2. Adding or removing multiple objects
Here you are required to DELETE with a payload of the dataElements in the group you wish to remove.
Attempting a DELETE with payload parameter of either json= or data= fails as this is not supported currently in dhis2.py.
Could you please add this feature in the next release?

Many thanks,
Pete

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.