Metadata-Version: 2.3
Name: iaso-client
Version: 1.3.0
Summary: Python client library for interacting with IASO APIs
Keywords: iaso,api,client,sdk,python
Author: abdelrahman meroual
Author-email: abdelrahman meroual <g62496@etu.he2b.be>
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: requests>=2.32.0
Requires-Dist: pandas>=2.2
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# iaso-client

`iaso-client` is a Python client library for interacting with IASO APIs.

## Installation

From the project root:

```bash
uv sync
```
### Configuration

1. Copy .env.sample to .env at the project root, then fill in your credentials:

The `IASO_BASE_URL` can't be provided with a trailing slash at the end.

```bash
IASO_BASE_URL=https://your.iaso-server.com

IASO_LOGIN=your_login
IASO_PASSWORD=your_password
or
IASO_API_TOKEN=your_api_token
```
2. Create a symlink so Bruno can read the root `.env` file:
   - **Windows** (Run Command Prompt as Administrator):
     ```bash
     cd bruno
     mklink .env ..\.env
     ```
   - **Mac / Linux**:
     ```bash
     cd bruno
     ln -s ../.env .env
     ```
### Quickstart

```python
import os

from iaso_client import IasoClient

IASO_BASE_URL = os.getenv("IASO_BASE_URL")
IASO_LOGIN = os.getenv("IASO_LOGIN")
IASO_PASSWORD = os.getenv("IASO_PASSWORD")

client = IasoClient(base_url=IASO_BASE_URL)
client.login(username=IASO_LOGIN, password=IASO_PASSWORD)
page = client.org_unit_types_list(page=1, max_page_size=50, project=975)
items = client.org_unit_types_list(search="District/Zone de sante - DIST")

print(page)
print(items)
```

### Authentication

Use `login()` with either a username and password, or an existing API token. Arguments must be passed by name.

```python
client.login(username="your_login", password="your_password")
# or
client.login(api_token="your_api_token")
```

The API token is the same JWT IASO returns from `POST /api/token/` (`access`) or `GET /api/apitoken/`, so OpenHEXA pipelines and the IASO MCP can pass it as a secret (`IASO_API_TOKEN` / `iaso_token`) without a password. A token that already includes the `Bearer ` prefix is accepted.

Passing the username and password positionally (`client.login(user, password)`) still works but is deprecated.

### Raw API calls

Use `raw()` to call IASO GET endpoints that do not have a typed SDK method yet.
The path is relative to `/api/`. Raw JSON calls are paginated by default with
`page=1` and `max_page_size=50`.

```python
entities = client.raw("entities/")
print(entities)
```

Query parameters can be passed with `params`:

```python
entities = client.raw(
    "entities/",
    params={"entity_type_ids": 12},
    page=2,
    max_page_size=100,
)
```

### Current typed resources

The current typed client API supports:

| Resource | Endpoint |
| --- | --- |
| Org units | `/api/orgunits/` |
| Org unit types | `/api/v2/orgunittypes/` |
| Forms | `/api/forms/` |
| Form versions | `/api/formversions/` |
| Instances | `/api/instances/` |
| Instance files | `/api/instances/attachments/`, `/api/instances/attachments_count/` |

Org units:

    client.org_units_list(...)
    client.org_units_list(page=...)
    client.org_units_list(format="dataframe")
    client.org_units_list(format="csv", output_path=...)
    client.org_units_list(format="xlsx", output_path=...)
    client.org_units_retrieve(...)

### Exporting org units to files

Use `org_units_list()` with `format="csv"` or `format="xlsx"` to download the IASO org units export:

```python
client.org_units_list(format="xlsx", output_path="exports/org_units")
client.org_units_list(format="csv", output_path="/tmp/org_units.csv")
client.org_units_list(format="xlsx", output_path="~/exports/org_units")
```

Org unit list filters such as `search`, `org_unit_type_id`, `source_id`,
`project`, `group`, `version`, `with_shape`, `with_location`, `parent_id`, and
`org_unit_parent_ids` can also be used for CSV and XLSX exports.

See [Export output paths](#export-output-paths) for file path behavior.

Org unit types:

    client.org_unit_types_list(...)
    client.org_unit_types_list(page=...)
    client.org_unit_types_list(format="dataframe")
    client.org_unit_types_retrieve(...)
    client.org_unit_types_hierarchy(...)

Forms:

    client.forms_list(...)
    client.forms_list(page=...)
    client.forms_list(format="dataframe")
    client.forms_list(format="csv", output_path=...)
    client.forms_list(format="xlsx", output_path=...)
    client.forms_retrieve(...)

### Exporting forms to files

Use `forms_list()` with `format="csv"` or `format="xlsx"` to download the IASO forms export:

```python
client.forms_list(format="xlsx", output_path="exports/forms")
client.forms_list(format="csv", output_path="/tmp/forms.csv")
client.forms_list(format="xlsx", output_path="~/exports/forms")
```

See [Export output paths](#export-output-paths) for file path behavior.

Form versions:

    client.form_versions_list(...)
    client.form_versions_list(page=...)
    client.form_versions_list(format="dataframe")
    client.form_versions_retrieve(...)

Instances:

    client.instances_list(...)
    client.instances_list(page=...)
    client.instances_list(format="dataframe")
    client.instances_list(format="csv", form_id=..., output_path=...)
    client.instances_list(format="xlsx", form_id=..., output_path=...)
    client.instances_retrieve(...)

### Exporting instances to files

Use `instances_list()` with `format="csv"` or `format="xlsx"` to download the IASO instances export:

```python
client.instances_list(format="xlsx", form_id=123, output_path="exports/instances")
client.instances_list(format="csv", form_id=123, output_path="/tmp/instances.csv")
```

See [Export output paths](#export-output-paths) for file path behavior.

### Export output paths

`output_path` must be a file path, not only a directory. It can be absolute,
relative, or start with `~`. Relative paths are resolved from the current working
directory of the Python process, meaning the directory where the command is run,
not where `iaso-client` is installed.

The parent directory must already exist and the target file must not already
exist. You can omit the extension: the client automatically adds `.csv` or
`.xlsx` based on the requested `format`.

Instance files:

    client.instance_files_list(...)
    client.instance_files_list(page=...)
    client.instance_files_list(format="dataframe")
    client.instance_files_count(...)

Instances:

    client.instances_list(...)
    client.instances_list(page=...)
    client.instances_retrieve(...)



### Pagination behavior

    *_list() fetches and aggregates results across all pages.
    *_list(page=...) returns a single paginated response.
    *_list(format="dataframe") returns all matching results as a pandas DataFrame.
    The `max_page_size` parameter controls max page size and must be between `50` and `2000`.
    Paginated calls are JSON-only: `page=...` cannot be combined with `format="dataframe"`, `format="csv"`, or `format="xlsx"`.


### HTTP request logging

You can enable request logging when creating the client:

```python
import logging
from iaso_client import IasoClient

logging.basicConfig(level=logging.INFO)

client = IasoClient(
    base_url="https://your.iaso-server.com",
    enable_logging=True,
)
```

Example log output:

```text
GET https://iaso-staging.bluesquare.org/api/v2/orgunittypes/ -> 200 [143.21 ms]
```

### Development

Run tests

```bash
uv run pytest
```

Run the main script

```bash
uv run .\examples\main.py
```

**Setup Pre-commit hooks**

1. To ensure your code is properly formatted and linted before every commit, install the pre-commit hook:

```bash
uv run pre-commit install
```

2. Linting & Formatting (Ruff)
Format the code (auto-fix styling issues):

```bash
uv run ruff format .
```

3. Check for logic errors and bad practices:

```Bash
uv run ruff check .
```

## Bruno

This repository also contains a Bruno collection for manually testing IASO API requests.

### Configure the environment

Copy `.env.sample` to `.env` in the `bruno` folder, then fill in your credentials:

```bash
IASO_BASE_URL=https://your.iaso-server.com
IASO_LOGIN=your_login
IASO_PASSWORD=your_password
IASO_API_TOKEN=your_api_token
```


### Run Bruno requests
1. Open Bruno.
2. In the top-left corner, open the collection and select the `bruno` directory from this repository.
3. In the top-right corner, select the `iaso_client` environment for the current collection.
4. Before running any protected request, run the `login` request first to authenticate.
5. Then select the request you want to run and click `->` to execute it.
