# Dhis2 Python Library
An asynchronous Python library for interacting with the DHIS2 API, designed for scalability, ease of use, and robust error handling. This library provides a comprehensive set of tools for data and metadata import/export, rate limiting, and streaming responses, optimized for DHIS2 versions >= 2.25.
---
## 🧭 Overview
The `dhis2` library wraps the `aiohttp` library to provide an asynchronous client for the DHIS2 API. It supports authentication, retry mechanisms for network issues, rate limiting, and both synchronous and streaming data retrieval. Ideal for developers building applications that integrate with DHIS2 instances.
---
## 🚀 Features
- **Asynchronous API Calls**: Leverage Python's `asyncio` for non-blocking requests.
- **Retry Mechanism**: Automatically retries failed requests (e.g., network errors) up to 3 times with exponential backoff.
- **Rate Limiting**: Enforces a configurable limit (default: 200 requests/second) to comply with DHIS2 API policies.
- **Streaming Responses**: Retrieve large datasets in chunks for memory-efficient processing.
- **Error Handling**: Custom handling for non-retryable errors (e.g., HTTP 400) and retryable server errors (e.g., HTTP 500).
- **Context Manager Support**: Use with `async with` for session management.
- **JSON Support**: Native handling of JSON data for metadata and data value operations.
---
## 📦 Installation
Install the library using pip:
```bash
pip install dhis2-async
# ✅ Prerequisites
- Python 3.6 or higher
- aiohttp, tenacity, and nest_asyncio (installed automatically with the package)
# Usage
# Basic example
`
import asyncio
from dhis2 import Dhis2, run_async
async def main():
# Initialize the client
dhis2_client = Dhis2(
username="your_username",
password="your_password",
url="
your-dhis2-instance"
)
try:
# Fetch organization units
response = await dhis2_client.get("organisationUnits", params={"fields": "id,name"})
print(response)
# Post metadata
metada …