Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Examples

This directory contains examples demonstrating how to use the iMednet Python SDK.

## Prerequisites

Before running the examples, you need to set up your environment with your iMednet API credentials.

1. Copy `.env.example` to `.env` in the root of the project:
```bash
cp .env.example .env
```

2. Fill in your API credentials in `.env`:
```ini
IMEDNET_API_KEY=your_api_key_here
IMEDNET_SECURITY_KEY=your_security_key_here
# Optional: Set base URL if using a custom instance
# IMEDNET_BASE_URL=https://edc.prod.imednetapi.com
```

## Running Examples

You can run any example directly using Python:

```bash
python examples/basic/get_studies.py
```

## Structure

- `basic/`: Simple synchronous examples for common endpoints (studies, sites, subjects, records, etc.).
- `async/`: Asynchronous examples using `asyncio`.
- `workflows/`: Examples of higher-level workflows.
29 changes: 14 additions & 15 deletions examples/async/get_records_async.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import asyncio

from imednet import AsyncImednetSDK
from imednet import AsyncImednetSDK, load_config
from imednet.utils import configure_json_logging

"""Example script demonstrating basic usage of the AsyncImednetSDK.

This script initializes the asynchronous client with API credentials, retrieves a list of
studies, selects the first study, and then fetches and prints a few records associated with
that study.
This script initializes the asynchronous client with API credentials loaded
from environment variables, retrieves a list of studies, selects the first study,
and then fetches and prints a few records associated with that study.

Replace the "XXXXXXXXXX" placeholders with your API key, security key and optionally a
specific study key. ``base_url`` can be left ``None`` to use the default iMednet endpoint or
set to a custom URL if needed.
Ensure your environment variables (IMEDNET_API_KEY, IMEDNET_SECURITY_KEY) are set correctly.
"""

api_key = "XXXXXXXXXX"
security_key = "XXXXXXXXXX"
base_url = None # Or set to your custom base URL if needed
study_key = "XXXXXXXXXX"


async def main() -> None:
configure_json_logging()

try:
cfg = load_config()
async with AsyncImednetSDK(
api_key=api_key,
security_key=security_key,
base_url=base_url,
api_key=cfg.api_key,
security_key=cfg.security_key,
base_url=cfg.base_url,
) as sdk:
studies = await sdk.studies.async_list()
if not studies:
print("No studies returned from API.")
return

for study in studies[:1]:
study_key = study.study_key
records = await sdk.records.async_list(study_key=study_key)
Expand Down
66 changes: 41 additions & 25 deletions examples/basic/get_codings.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
from imednet import ImednetSDK as ImednetClient
from imednet import load_config
from imednet.utils import configure_json_logging

"""
This script demonstrates how to retrieve coding information from the iMednet API
using the imednet package.
It initializes the ImednetClient with API credentials, lists the available studies,
retrieves the codings for the first study found, and prints the total count
of codings along with details for the first five codings.

It initializes the ImednetClient with API credentials loaded from environment variables,
lists the available studies, retrieves the codings for the first study found,
and prints the total count of codings along with details for the first five codings.

Requires:
- imednet installed (`pip install imednet`)
- Valid API key and security key (replace placeholders).
- Valid API key and security key set in environment variables
(IMEDNET_API_KEY, IMEDNET_SECURITY_KEY).

Usage:
1. Replace the placeholder values for `api_key` and `security_key`.
2. Optionally set `base_url` if using a custom iMednet instance.
3. Run the script.
1. Ensure your .env file is configured correctly.
2. Run the script.

The script will output:
- The total number of codings found for the first study accessed via the API key.
- The Coding ID and Variable name for the first 5 codings (if available).
- An error message if any issues occur during the API interaction.
"""

api_key = "XXXXXXXXXX"
security_key = "XXXXXXXXXX"
base_url = None # Or set to your custom base URL if needed
study_key = "XXXXXXXXXX"

try:
client = ImednetClient(api_key=api_key, security_key=security_key, base_url=base_url)
studies = client.studies.list()
if not studies:
print("No studies returned from API.")
for study in studies[:1]:
study_key = study.study_key
codings = client.codings.list(study_key=study_key)
print(f"Codings for study '{study_key}': {len(codings)}")
for coding in codings[:5]:
print(f"- Coding ID: {coding.coding_id}, Variable: {coding.variable}")
except Exception as e:
print(f"Error: {e}")

def main():
configure_json_logging()

try:
cfg = load_config()
client = ImednetClient(
api_key=cfg.api_key, security_key=cfg.security_key, base_url=cfg.base_url
)

studies = client.studies.list()
if not studies:
print("No studies returned from API.")
return

for study in studies[:1]:
study_key = study.study_key
codings = client.codings.list(study_key=study_key)
print(f"Codings for study '{study_key}': {len(codings)}")
for coding in codings[:5]:
print(f"- Coding ID: {coding.coding_id}, Variable: {coding.variable}")

except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
main()
61 changes: 37 additions & 24 deletions examples/basic/get_record_revisions.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
from imednet import ImednetSDK as ImednetClient
from imednet import load_config
from imednet.utils import configure_json_logging

"""
This script demonstrates how to retrieve record revisions for a specific study
using the iMednet Python SDK.
It initializes the iMednet client with API credentials, lists available studies,
selects the first study found, and then retrieves and prints the record revisions
associated with that study. It prints the total count of revisions and details
for the first five revisions found. Basic error handling is included.
Note: Replace placeholder API keys, security keys, and potentially the base URL
with actual values before running.

It initializes the iMednet client with API credentials loaded from environment variables,
lists available studies, selects the first study found, and then retrieves and prints
the record revisions associated with that study. It prints the total count of revisions
and details for the first five revisions found. Basic error handling is included.

Note: Ensure your environment variables (IMEDNET_API_KEY, IMEDNET_SECURITY_KEY) are set correctly.
"""

api_key = "XXXXXXXXXX"
security_key = "XXXXXXXXXX"
base_url = None # Or set to your custom base URL if needed
study_key = "XXXXXXXXXX"

try:
client = ImednetClient(api_key=api_key, security_key=security_key, base_url=base_url)
studies = client.studies.list()
if not studies:
print("No studies returned from API.")
for study in studies[:1]:
study_key = study.study_key
record_revisions = client.record_revisions.list(study_key=study_key)
print(f"Record Revisions for study '{study_key}': {len(record_revisions)}")
for rev in record_revisions[:5]:
print(f"- Revision ID: {rev.record_revision_id}, Record ID: {rev.record_id}")
except Exception as e:
print(f"Error: {e}")

def main():
configure_json_logging()

try:
cfg = load_config()
client = ImednetClient(
api_key=cfg.api_key, security_key=cfg.security_key, base_url=cfg.base_url
)

studies = client.studies.list()
if not studies:
print("No studies returned from API.")
return

for study in studies[:1]:
study_key = study.study_key
record_revisions = client.record_revisions.list(study_key=study_key)
print(f"Record Revisions for study '{study_key}': {len(record_revisions)}")
for rev in record_revisions[:5]:
print(f"- Revision ID: {rev.record_revision_id}, Record ID: {rev.record_id}")

except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
main()
60 changes: 36 additions & 24 deletions examples/basic/get_records.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
from imednet import ImednetSDK as ImednetClient
from imednet import load_config
from imednet.utils import configure_json_logging

"""
Example script demonstrating basic usage of the imednet package.
This script initializes the ImednetClient with API credentials,

This script initializes the ImednetClient with API credentials loaded from environment variables,
retrieves a list of studies associated with the account, selects the
first study, and then fetches and prints the first few records
associated with that study.

It showcases:
- Initializing the ImednetSDK client.
- Initializing the ImednetSDK client using configuration loading.
- Listing studies using `client.studies.list()`.
- Listing records for a specific study using `client.records.list()`.
- Basic iteration and printing of retrieved data.
- Simple error handling for API calls.

Note:
Replace "XXXXXXXXXX" placeholders with your actual API key,
security key, and optionally a specific study key if you want to
target a particular study directly. The `base_url` can be left as None
to use the default iMednet API endpoint or set to a custom URL if required.
Ensure your environment variables (IMEDNET_API_KEY, IMEDNET_SECURITY_KEY) are set correctly.
"""

api_key = "XXXXXXXXXX"
security_key = "XXXXXXXXXX"
base_url = None # Or set to your custom base URL if needed
study_key = "XXXXXXXXXX"

try:
client = ImednetClient(api_key=api_key, security_key=security_key, base_url=base_url)
studies = client.studies.list()
if not studies:
print("No studies returned from API.")
for study in studies[:1]:
study_key = study.study_key
records = client.records.list(study_key=study_key)
print(f"Records for study '{study_key}': {len(records)}")
for record in records[:5]:
print(f"- Record ID: {record.record_id}, Subject Key: {record.subject_key}")
except Exception as e:
print(f"Error: {e}")

def main():
configure_json_logging()

try:
cfg = load_config()
client = ImednetClient(
api_key=cfg.api_key, security_key=cfg.security_key, base_url=cfg.base_url
)

studies = client.studies.list()
if not studies:
print("No studies returned from API.")
return

for study in studies[:1]:
study_key = study.study_key
records = client.records.list(study_key=study_key)
print(f"Records for study '{study_key}': {len(records)}")
for record in records[:5]:
print(f"- Record ID: {record.record_id}, Subject Key: {record.subject_key}")

except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
main()
62 changes: 39 additions & 23 deletions examples/basic/get_sites.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
from imednet import ImednetSDK as ImednetClient
from imednet import load_config
from imednet.utils import configure_json_logging

"""
This script demonstrates how to use the Imednet Python SDK to retrieve a list of studies
and then list the sites associated with the first study found.
It initializes the ImednetClient with API credentials, fetches the list of studies
accessible with those credentials, selects the first study, and then retrieves
and prints the details (name and ID) of the first few sites belonging to that study.

It initializes the ImednetClient with API credentials loaded from environment variables,
fetches the list of studies accessible with those credentials, selects the first study,
and then retrieves and prints the details (name and ID) of the first few sites belonging
to that study.

Requires:
- An active Imednet account with API access.
- API Key and Security Key for authentication.
- API Key and Security Key set in environment variables (IMEDNET_API_KEY, IMEDNET_SECURITY_KEY).
- The `imednet` package installed (`pip install imednet`).

Usage:
1. Replace the placeholder values for `api_key`, `security_key`, and optionally `base_url`.
1. Ensure your .env file is configured correctly.
2. Run the script. It will print the total number of sites for the first study found
and the details of up to the first five sites.
3. If an error occurs during the API interaction, an error message will be printed.
"""

api_key = "XXXXXXXXXX"
security_key = "XXXXXXXXXX"
base_url = None # Or set to your custom base URL if needed
study_key = "XXXXXXXXXX"

try:
client = ImednetClient(api_key=api_key, security_key=security_key, base_url=base_url)
studies = client.studies.list()
if not studies:
print("No studies returned from API.")
for study in studies[:1]:
study_key = study.study_key
sites = client.sites.list(study_key=study_key)
print(f"Sites for study '{study_key}': {len(sites)}")
for site in sites[:5]:
print(f"- Site Name: {site.site_name}, ID: {site.site_id}")
except Exception as e:
print(f"Error: {e}")

def main():
configure_json_logging()

try:
cfg = load_config()
client = ImednetClient(
api_key=cfg.api_key, security_key=cfg.security_key, base_url=cfg.base_url
)

studies = client.studies.list()
if not studies:
print("No studies returned from API.")
return

for study in studies[:1]:
study_key = study.study_key
sites = client.sites.list(study_key=study_key)
print(f"Sites for study '{study_key}': {len(sites)}")
for site in sites[:5]:
print(f"- Site Name: {site.site_name}, ID: {site.site_id}")

except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
main()
Loading