"""Facade protocols for the iMednet SDK.
These protocols define the expected interface for the iMednet SDK clients,
allowing for easier mocking and decoupling in dependent packages.
"""
from contextlib import AbstractContextManager
from typing import TYPE_CHECKING, Protocol, Self
from imednet.auth import AuthStrategy
from imednet.models.codings import Coding
from imednet.models.forms import Form
from imednet.models.intervals import Interval
from imednet.models.jobs import Job, JobStatus
from imednet.models.queries import Query
from imednet.models.record_revisions import RecordRevision
from imednet.models.records import Record
from imednet.models.sites import Site
from imednet.models.studies import Study
from imednet.models.subjects import Subject
from imednet.models.users import User
from imednet.models.variables import Variable
from imednet.models.visits import Visit
from imednet.plugins import (
AsyncSinksNamespaceProtocol,
AsyncWorkflowsNamespaceProtocol,
PluginProtocol,
SinksNamespaceProtocol,
SyncWorkflowsNamespaceProtocol,
TriageStoreProtocol,
WorkflowsNamespaceProtocol,
)
from imednet.utils.batch_coordinator import DEFAULT_BATCH_CHUNK_SIZE, BatchSubmissionSummary
from imednet.utils.job_poller import JobPollSummary, JobProgressHandler
from imednet.utils.typing import FilterValue, RecordsInput
if TYPE_CHECKING:
from imednet.validation.cache import ValidationSchemaProtocol
[docs]class ImednetFacade(Protocol):
"""Protocol for the synchronous iMednet SDK facade."""
[docs] def study_context(self, study_key: str) -> AbstractContextManager[Self]:
"""Bind a study key to this facade instance for the duration of the with-block."""
...
[docs] def get_codings(self, study_key: str | None = None, **filters: FilterValue) -> list[Coding]:
"""Fetch code list definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Coding models.
"""
...
[docs] def get_intervals(self, study_key: str | None = None, **filters: FilterValue) -> list[Interval]:
"""Fetch visit interval definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Interval models.
"""
...
[docs] def get_job(self, study_key: str, batch_id: str) -> JobStatus:
"""Fetch the status of a specific background job.
Args:
study_key: The unique identifier for the study.
batch_id: The unique identifier for the job batch.
Returns:
A JobStatus model.
"""
...
[docs] def get_queries(self, study_key: str | None = None, **filters: FilterValue) -> list[Query]:
"""Fetch queries for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Query models.
"""
...
[docs] def get_record_revisions(
self, study_key: str | None = None, **filters: FilterValue
) -> list[RecordRevision]:
"""Fetch record revision history for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of RecordRevision models.
"""
...
[docs] def get_records(self, study_key: str | None = None, **filters: FilterValue) -> list[Record]:
"""Fetch data records for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Record models.
"""
...
[docs] def get_sites(self, study_key: str | None = None, **filters: FilterValue) -> list[Site]:
"""Fetch site definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Site models.
"""
...
[docs] def get_studies(self, **filters: FilterValue) -> list[Study]:
"""Fetch all studies accessible to the current user.
Args:
**filters: Optional API filters to apply.
Returns:
A list of Study models.
"""
...
[docs] def get_subjects(self, study_key: str | None = None, **filters: FilterValue) -> list[Subject]:
"""Fetch subject records for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Subject models.
"""
...
[docs] def get_users(self, study_key: str | None = None, **filters: FilterValue) -> list[User]:
"""Fetch user definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of User models.
"""
...
[docs] def get_variables(self, study_key: str | None = None, **filters: FilterValue) -> list[Variable]:
"""Fetch variable definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Variable models.
"""
...
[docs] def get_visits(self, study_key: str | None = None, **filters: FilterValue) -> list[Visit]:
"""Fetch visit definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Visit models.
"""
...
[docs] def create_record(
self,
study_key: str,
records_data: RecordsInput,
email_notify: bool | str | None = None,
*,
schema: "ValidationSchemaProtocol | None" = None,
skip_validation: bool = False,
) -> Job:
"""Submit a request to create one or more data records.
Args:
study_key: The unique identifier for the study.
records_data: Record dicts, a pandas DataFrame, or a CSV file path.
email_notify: Optional email notification settings.
schema: Optional validation schema.
skip_validation: Optional flag to skip validation when pre-validated.
Returns:
A Job model representing the background operation.
"""
...
[docs] def create_records_batch(
self,
study_key: str,
records_data: RecordsInput,
email_notify: bool | str | None = None,
*,
schema: "ValidationSchemaProtocol | None" = None,
chunk_size: int = DEFAULT_BATCH_CHUNK_SIZE,
skip_validation: bool = False,
wait_for_completion: bool = False,
timeout: float = 300.0,
interval: float = 5.0,
on_progress: JobProgressHandler | None = None,
dry_run: bool = False,
collect_errors: bool = False,
resolve_results: bool = True,
) -> BatchSubmissionSummary:
"""Submit records in bounded chunks with optional multi-job polling."""
...
[docs] def poll_job(
self,
study_key: str,
batch_id: str,
*,
interval: float = 5,
timeout: float = 300,
on_progress: JobProgressHandler | None = None,
resolve_results: bool = False,
) -> JobStatus:
"""Wait for a background job to reach a terminal state.
Args:
study_key: The unique identifier for the study.
batch_id: The unique identifier for the job batch.
interval: Time between poll attempts in seconds.
timeout: Maximum time to wait in seconds.
on_progress: Optional callback invoked on each poll cycle.
resolve_results: Whether to fetch and validate terminal ``resultUrl`` payloads.
Returns:
The final JobStatus of the operation.
"""
...
[docs] def poll_jobs(
self,
study_key: str,
batch_ids: list[str],
*,
interval: float = 5.0,
timeout: float = 300.0,
on_progress: JobProgressHandler | None = None,
fail_fast: bool = False,
resolve_results: bool = False,
) -> JobPollSummary:
"""Wait for multiple background jobs concurrently, optionally resolving ``resultUrl`` payloads."""
...
@property
def auth(self) -> AuthStrategy | None:
"""Access the authentication provider for the facade."""
...
[docs]class AsyncImednetFacade(Protocol):
"""Protocol for the asynchronous iMednet SDK facade."""
[docs] def study_context(self, study_key: str) -> AbstractContextManager[Self]:
"""Bind a study key to this facade instance for the duration of the with-block."""
...
[docs] async def get_codings(
self, study_key: str | None = None, **filters: FilterValue
) -> list[Coding]:
"""Asynchronously fetch code list definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Coding models.
"""
...
[docs] async def get_intervals(
self, study_key: str | None = None, **filters: FilterValue
) -> list[Interval]:
"""Asynchronously fetch visit interval definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Interval models.
"""
...
[docs] async def get_job(self, study_key: str, batch_id: str) -> JobStatus:
"""Asynchronously fetch the status of a specific background job.
Args:
study_key: The unique identifier for the study.
batch_id: The unique identifier for the job batch.
Returns:
A JobStatus model.
"""
...
[docs] async def get_queries(
self, study_key: str | None = None, **filters: FilterValue
) -> list[Query]:
"""Asynchronously fetch queries for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Query models.
"""
...
[docs] async def get_record_revisions(
self, study_key: str | None = None, **filters: FilterValue
) -> list[RecordRevision]:
"""Asynchronously fetch record revision history for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of RecordRevision models.
"""
...
[docs] async def get_records(
self, study_key: str | None = None, **filters: FilterValue
) -> list[Record]:
"""Asynchronously fetch data records for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Record models.
"""
...
[docs] async def get_sites(self, study_key: str | None = None, **filters: FilterValue) -> list[Site]:
"""Asynchronously fetch site definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Site models.
"""
...
[docs] async def get_studies(self, **filters: FilterValue) -> list[Study]:
"""Asynchronously fetch all studies accessible to the current user.
Args:
**filters: Optional API filters to apply.
Returns:
A list of Study models.
"""
...
[docs] async def get_subjects(
self, study_key: str | None = None, **filters: FilterValue
) -> list[Subject]:
"""Asynchronously fetch subject records for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Subject models.
"""
...
[docs] async def get_users(self, study_key: str | None = None, **filters: FilterValue) -> list[User]:
"""Asynchronously fetch user definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of User models.
"""
...
[docs] async def get_variables(
self, study_key: str | None = None, **filters: FilterValue
) -> list[Variable]:
"""Asynchronously fetch variable definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Variable models.
"""
...
[docs] async def get_visits(self, study_key: str | None = None, **filters: FilterValue) -> list[Visit]:
"""Asynchronously fetch visit definitions for a specific study.
Args:
study_key: The unique identifier for the study.
**filters: Optional API filters to apply.
Returns:
A list of Visit models.
"""
...
[docs] async def create_record(
self,
study_key: str,
records_data: RecordsInput,
email_notify: bool | str | None = None,
*,
schema: "ValidationSchemaProtocol | None" = None,
skip_validation: bool = False,
) -> Job:
"""Asynchronously submit a request to create one or more data records.
Args:
study_key: The unique identifier for the study.
records_data: Record dicts, a pandas DataFrame, or a CSV file path.
email_notify: Optional email notification settings.
schema: Optional validation schema.
skip_validation: Optional flag to skip validation when pre-validated.
Returns:
A Job model representing the background operation.
"""
...
[docs] async def create_records_batch(
self,
study_key: str,
records_data: RecordsInput,
email_notify: bool | str | None = None,
*,
schema: "ValidationSchemaProtocol | None" = None,
chunk_size: int = DEFAULT_BATCH_CHUNK_SIZE,
skip_validation: bool = False,
wait_for_completion: bool = False,
timeout: float = 300.0,
interval: float = 5.0,
on_progress: JobProgressHandler | None = None,
dry_run: bool = False,
collect_errors: bool = False,
resolve_results: bool = True,
) -> BatchSubmissionSummary:
"""Asynchronously submit records in bounded chunks with optional multi-job polling."""
...
[docs] async def poll_job(
self,
study_key: str,
batch_id: str,
*,
interval: float = 5,
timeout: float = 300,
on_progress: JobProgressHandler | None = None,
resolve_results: bool = False,
) -> JobStatus:
"""Asynchronously wait for a background job to reach a terminal state.
Args:
study_key: The unique identifier for the study.
batch_id: The unique identifier for the job batch.
interval: Time between poll attempts in seconds.
timeout: Maximum time to wait in seconds.
on_progress: Optional callback invoked on each poll cycle.
resolve_results: Whether to fetch and validate terminal ``resultUrl`` payloads.
Returns:
The final JobStatus of the operation.
"""
...
[docs] async def poll_jobs(
self,
study_key: str,
batch_ids: list[str],
*,
interval: float = 5.0,
timeout: float = 300.0,
on_progress: JobProgressHandler | None = None,
fail_fast: bool = False,
resolve_results: bool = False,
) -> JobPollSummary:
"""Asynchronously wait for multiple background jobs, optionally resolving ``resultUrl`` payloads."""
...
@property
def auth(self) -> AuthStrategy | None:
"""Access the authentication provider for the asynchronous facade."""
...
__all__ = [
"AsyncImednetFacade",
"AsyncSinksNamespaceProtocol",
"AsyncWorkflowsNamespaceProtocol",
"ImednetFacade",
"PluginProtocol",
"SinksNamespaceProtocol",
"SyncWorkflowsNamespaceProtocol",
"TriageStoreProtocol",
"WorkflowsNamespaceProtocol",
]