Plugin Authoring Guide

The iMednet SDK monorepo contains the following first-party packages:

The built-in imednet-workflows package is the reference implementation for third-party SDK plugins, while imednet-tui demonstrates CLI subcommand registration through the imednet.cli_plugins entry-point group:

[project.entry-points."imednet.cli_plugins"]
tui = "imednet_tui.cli:setup_parser"

Any third-party package can register its own SDK or CLI plugin using the standard Python entry-point mechanism.

Optional-plugin CLI graceful-degradation verification

CLI commands that depend on optional plugins should be tested in two modes:

  • Plugin present: patch importlib.util.find_spec (or module import) to return a module and assert the command runs.

  • Plugin absent: patch discovery to return None (or raise ImportError) and assert the command exits with code 1 and prints install guidance.

See tests/unit/cli/test_cli.py and tests/unit/cli/test_cli_export.py for repository examples of this pattern.

Plugin authoring reference

How it works

The SDK supports typed synchronous and asynchronous Python entry-point groups for extending functionality on ImednetSDK and AsyncImednetSDK:

  • Workflows (``imednet.workflows`` and ``imednet.async_workflows``) — sdk.workflows is a WorkflowRegistry that resolves synchronous adapters from imednet.workflows on ImednetSDK and native asynchronous adapters from imednet.async_workflows on AsyncImednetSDK (falling back to imednet.workflows entries that declare supports_async = True). Each entry point is loaded and instantiated lazily on first attribute access by passing the active SDK facade.

  • Export sinks (``imednet.sinks`` and ``imednet.async_sinks``) — export() discovers ExportSink classes in imednet.sinks, while async_export() discovers AsyncExportSink classes in imednet.async_sinks.

  • Namespace plugins (``imednet.plugins``) — Top-level SDK namespaces such as sdk.sinks query the imednet.plugins entry-point group (for example, sinks = "imednet_sinks.plugin:create_sinks") and invoke the registered factory with the SDK instance to return a mode-matched namespace (SinksNamespaceProtocol on ImednetSDK and AsyncSinksNamespaceProtocol on AsyncImednetSDK).

If a workflow or plugin package is not installed, accessing the missing workflow or namespace attribute raises AttributeError or ImportError with installation guidance.

If a namespace plugin is installed under imednet.plugins but fails to load (duplicate registration, broken import, or non-callable target), a PluginLoadError is raised when loading the namespace.

Plugin contract

Workflow adapters registered under imednet.workflows accept ImednetFacade, while asynchronous adapters registered under imednet.async_workflows accept AsyncImednetFacade and expose symmetric unprefixed coroutine methods. Custom workflows registered only under imednet.workflows default to synchronous-only and raise TypeError on AsyncImednetSDK unless they also register an imednet.async_workflows adapter or declare supports_async = True.

A namespace-level plugin factory registered under imednet.plugins satisfies PluginProtocol (or SinksPluginProtocol, returning SinksNamespaceProtocol / AsyncSinksNamespaceProtocol or SyncWorkflowsNamespaceProtocol / AsyncWorkflowsNamespaceProtocol):

from typing import Any, Protocol
from imednet.plugins import SyncWorkflowsNamespaceProtocol

class PluginProtocol(Protocol):
    def __call__(self, sdk_instance: Any) -> SyncWorkflowsNamespaceProtocol: ...

The built-in imednet-workflows package registers all nine workflows symmetrically across imednet.workflows (SyncWorkflowsNamespaceProtocol on ImednetSDK) and imednet.async_workflows (AsyncWorkflowsNamespaceProtocol on AsyncImednetSDK), while imednet-sinks binds export_to_mongodb, export_to_neo4j, and export_to_snowflake on both ImednetSDK.sinks and AsyncImednetSDK.sinks:

  • data_extraction

  • duckdb_centralizer

  • query_management

  • record_mapper

  • record_update

  • register_subjects

  • subject_data

  • uat

  • uat_inspector

Minimal working example

# myplugin/workflows.py
from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from imednet.sdk import ImednetSDK


class MyDataExtraction:
    def __init__(self, sdk: "ImednetSDK") -> None:
        self._sdk = sdk

    def run(self, study_key: str) -> list[Any]:
        return list(self._sdk.records.list(study_key=study_key))

Registering the entry point

Add the following standard PEP 621 entry-point table to your package’s pyproject.toml to register a workflow on sdk.workflows (or a namespace plugin under imednet.plugins):

[project.entry-points."imednet.workflows"]
my_extraction = "myplugin.workflows:MyDataExtraction"

[project.entry-points."imednet.plugins"]
sinks = "imednet_sinks.plugin:create_sinks"

Or, when using setuptools, in setup.cfg:

[options.entry_points]
imednet.workflows =
    my_extraction = myplugin.workflows:MyDataExtraction
imednet.plugins =
    sinks = imednet_sinks.plugin:create_sinks

Once installed (e.g. pip install myplugin), the SDK discovers and instantiates registered workflows automatically:

from imednet import ImednetSDK

sdk = ImednetSDK(api_key="...", security_key="...")
records = sdk.workflows.data_extraction.extract_records_by_criteria("MY_STUDY")

Error handling

PluginLoadError is raised when loading a namespace plugin from imednet.plugins if:

  • More than one plugin is registered for the same entry-point name in the imednet.plugins group.

  • The entry point fails to import (e.g. broken dependency).

  • The loaded object is not callable.

  • The factory raises TypeError when called with the SDK instance.

PluginLoadError is a subclass of ImednetError so it can be caught uniformly:

from imednet import ImednetSDK
from imednet.errors import ImednetError

try:
    sdk = ImednetSDK(api_key="...", security_key="...")
except ImednetError as exc:
    print(f"SDK initialisation failed: {exc}")

API reference

class imednet.plugins.PluginProtocol[source]

Bases: Protocol

Protocol that every iMednet plugin factory must satisfy.

A conforming factory is a callable that accepts an SDK instance and returns an object implementing WorkflowsNamespaceProtocol.

Example:

from imednet.plugins import PluginProtocol
from imednet.sdk import ImednetSDK

class MyWorkflows:
    def __init__(self, sdk: ImednetSDK) -> None:
        self.data_extraction = ...
        self.duckdb_centralizer = ...
        self.query_management = ...
        self.record_mapper = ...
        self.record_update = ...
        self.register_subjects = ...
        self.subject_data = ...
        self.uat = ...
        self.uat_inspector = ...

def create_workflows(sdk: ImednetSDK) -> MyWorkflows:
    return MyWorkflows(sdk)

# Verify conformance at import time (optional, for development use):
assert isinstance(create_workflows, PluginProtocol)
__call__(sdk_instance, /)[source]

Initialize the workflows plugin with an SDK instance.

Parameters:

sdk_instance (ImednetFacade) –

Return type:

SyncWorkflowsNamespaceProtocol

class imednet.plugins.SyncWorkflowsNamespaceProtocol[source]

Bases: Protocol

Synchronous workflows namespace contract exposed on ImednetSDK.workflows.

property data_extraction: DataExtractionWorkflowProtocol

Access the synchronous data extraction workflow.

property duckdb_centralizer: DuckDBIngestionWorkflowProtocol

Access the lazy DuckDB bronze/silver ingestion workflow.

property query_management: QueryManagementWorkflowProtocol

Access the query management workflow.

property record_mapper: RecordMapperProtocol

Access the tabular and SDTM record mapper workflow.

property record_update: SyncRecordUpdateWorkflowProtocol

Access the synchronous record submission and update workflow.

property register_subjects: SyncRegisterSubjectsWorkflowProtocol

Access the synchronous bulk subject registration workflow.

property subject_data: SubjectDataWorkflowProtocol

Access the comprehensive subject data retrieval workflow.

property uat: UATWorkflowProtocol

Access the end-to-end UAT orchestration workflow.

property uat_inspector: SyncStudySchemaInspectorProtocol

Access the synchronous study schema inspector.

class imednet.plugins.AsyncWorkflowsNamespaceProtocol[source]

Bases: Protocol

Asynchronous workflows namespace contract exposed on AsyncImednetSDK.workflows.

property data_extraction: AsyncDataExtractionWorkflowProtocol

Access the asynchronous data extraction workflow.

property duckdb_centralizer: AsyncDuckDBIngestionWorkflowProtocol

Access the asynchronous DuckDB bronze/silver ingestion workflow.

property query_management: AsyncQueryManagementWorkflowProtocol

Access the asynchronous query management workflow.

property record_mapper: AsyncRecordMapperProtocol

Access the asynchronous tabular and SDTM record mapper workflow.

property record_update: AsyncRecordUpdateWorkflowProtocol

Access the asynchronous record submission and update workflow.

property register_subjects: AsyncRegisterSubjectsWorkflowProtocol

Access the asynchronous bulk subject registration workflow.

property subject_data: AsyncSubjectDataWorkflowProtocol

Access the asynchronous comprehensive subject data retrieval workflow.

property uat: AsyncUATWorkflowProtocol

Access the asynchronous end-to-end UAT orchestration workflow.

property uat_inspector: AsyncStudySchemaInspectorProtocol

Access the asynchronous study schema inspector.

class imednet.plugins.SinksNamespaceProtocol[source]

Bases: Protocol

Synchronous sinks namespace contract exposed on ImednetSDK.sinks.

property MongoDbExportSink: MongoDbExportSinkFactoryProtocol

Sink class for MongoDB record exports.

property MongoDbSinkConfig: MongoDbSinkConfigFactoryProtocol

Configuration class for MongoDB export sinks.

property Neo4jExportSink: Neo4jExportSinkFactoryProtocol

Sink class for Neo4j record exports.

property Neo4jSinkConfig: Neo4jSinkConfigFactoryProtocol

Configuration class for Neo4j export sinks.

property SnowflakeExportSink: SnowflakeExportSinkFactoryProtocol

Sink class for Snowflake record exports.

property SnowflakeSinkConfig: SnowflakeSinkConfigFactoryProtocol

Configuration class for Snowflake export sinks.

property export_to_mongodb: BoundExportToMongoDbProtocol

Bound helper to export study records to MongoDB.

property export_to_neo4j: BoundExportToNeo4jProtocol

Bound helper to export study records to Neo4j.

property export_to_snowflake: BoundExportToSnowflakeProtocol

Bound helper to export study records to Snowflake.

class imednet.plugins.AsyncSinksNamespaceProtocol[source]

Bases: Protocol

Asynchronous sinks namespace contract exposed on AsyncImednetSDK.sinks.

property AsyncMongoDbExportSink: AsyncMongoDbExportSinkFactoryProtocol

Asynchronous sink class for MongoDB record exports.

property AsyncNeo4jExportSink: AsyncNeo4jExportSinkFactoryProtocol

Asynchronous sink class for Neo4j record exports.

property AsyncSnowflakeExportSink: AsyncSnowflakeExportSinkFactoryProtocol

Asynchronous sink class for Snowflake record exports.

property MongoDbExportSink: MongoDbExportSinkFactoryProtocol

Synchronous sink class for MongoDB record exports.

property MongoDbSinkConfig: MongoDbSinkConfigFactoryProtocol

Configuration class for MongoDB export sinks.

property Neo4jExportSink: Neo4jExportSinkFactoryProtocol

Synchronous sink class for Neo4j record exports.

property Neo4jSinkConfig: Neo4jSinkConfigFactoryProtocol

Configuration class for Neo4j export sinks.

property SnowflakeExportSink: SnowflakeExportSinkFactoryProtocol

Synchronous sink class for Snowflake record exports.

property SnowflakeSinkConfig: SnowflakeSinkConfigFactoryProtocol

Configuration class for Snowflake export sinks.

property export_to_mongodb: BoundAsyncExportToMongoDbProtocol

Bound coroutine helper to export study records to MongoDB.

property export_to_neo4j: BoundAsyncExportToNeo4jProtocol

Bound coroutine helper to export study records to Neo4j.

property export_to_snowflake: BoundAsyncExportToSnowflakeProtocol

Bound coroutine helper to export study records to Snowflake.

exception imednet.errors.PluginLoadError[source]

Bases: ImednetError

Raised when a plugin fails to load or does not satisfy the PluginProtocol.

__init__(message='', status_code=None, code=None, status=None, *, diagnostic_context=None)

Initialize the error.

Parameters:
  • message (str) –

  • status_code (int | None) –

  • code (int | None) –

  • status (Any) –

  • diagnostic_context (DiagnosticContext | None) –

Return type:

None

property attempt_count: int | None

Return the number of attempts executed before failure, if present.

property cause_type: str | None

Return the underlying exception class name, if present.

property method: str | None

Return the HTTP method from diagnostic_context, if present.

property retry_after: float | None

Return the parsed Retry-After delay in seconds, if present.

property url: str | None

Return the sanitized request URL from diagnostic_context, if present.