Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server helpers for creating notification bundle #13

Open
ruscoder opened this issue Nov 28, 2024 · 0 comments
Open

Add server helpers for creating notification bundle #13

ruscoder opened this issue Nov 28, 2024 · 0 comments

Comments

@ruscoder
Copy link
Member

For some cases e.g. testing it's extermely useful to have something like:

def notification_bundle_factory(
    main_resource: r4b.AnyResource,
    subscription_id: str,
    payload_content: Literal["id-only", "full-resource"] = "id-only",
    included_resources: list[r4b.AnyResource] | None = None,
) -> r4b.Bundle:
    now_datetime = "2024-10-31T17:32:46.228024Z"

    host = "http://host.internal/fhir"
    subscription_topic = f"{host}/SubscriptionTopic/{subscription_id}-event"

    def _make_full_url(resource: ResourceProtocol) -> str:
        return f"{host}/{make_reference_str(resource)}"

    subscription_status = r4b.SubscriptionStatus(
        type="event-notification",
        eventsSinceSubscriptionStart="1",
        status="active",
        subscription=r4b.Reference(reference=f"{host}/Subscription/{subscription_id}"),
        topic=subscription_topic,
        notificationEvent=[
            r4b.SubscriptionStatusNotificationEvent(
                timestamp=now_datetime,
                eventNumber="1",
                focus=r4b.Reference(reference=make_reference_str(main_resource)),
                additionalContext=(
                    [
                        r4b.Reference(
                            reference=make_reference_str(resource),
                        )
                        for resource in included_resources
                    ]
                    if included_resources
                    else None
                ),
            )
        ],
    )
    return r4b.Bundle(
        type="history",
        timestamp=now_datetime,
        entry=[
            r4b.BundleEntry(
                request=r4b.BundleEntryRequest(
                    method="GET",
                    url=f"Subscription/{subscription_id}/$status",
                ),
                response=r4b.BundleEntryResponse(status="200"),
                resource=subscription_status,
            ),
            *[
                r4b.BundleEntry(
                    request=r4b.BundleEntryRequest(
                        method="POST" if resource.id == main_resource.id else "GET",
                        url=make_reference_str(resource),
                    ),
                    response=r4b.BundleEntryResponse(
                        status="201" if resource.id == main_resource.id else "200"
                    ),
                    fullUrl=_make_full_url(resource),
                    resource=resource,
                )
                for resource in (
                    [main_resource, *(included_resources or [])]
                    if payload_content == "full-resource"
                    else []
                )
            ],
        ],
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant