Skip to content

Expose and consume a RemoteAPI safely

Use RemoteAPI when a manager should expose a small REST contract to another GeneralManager service. Use RemoteManagerInterface in the consuming service. Enable websocket invalidation only when the client can maintain an authenticated ASGI session; websocket events are hints that trigger a REST refetch.

Configure the server

from typing import ClassVar

from django.db.models import CharField

from general_manager.interface import DatabaseInterface
from general_manager.manager import GeneralManager
from general_manager.permission import AdditiveManagerPermission


class Project(GeneralManager):
    class Interface(DatabaseInterface):
        name = CharField(max_length=200)

    class Permission(AdditiveManagerPermission):
        __read__: ClassVar[list[str]] = ["isAuthenticated"]

    class RemoteAPI:
        enabled = True
        base_path = "/internal/gm"
        resource_name = "projects"
        allow_filter = True
        allow_detail = True
        allow_create = True
        allow_update = True
        websocket_invalidation = True
        protocol_version = "v1"

Install Django authentication and ASGI middleware before enabling the websocket route. request.user controls REST read authorization; the protocol-version header only selects the wire format and never authenticates a caller. Detail reads return 403/permission_denied when the current row is not readable, denied fields are omitted without evaluating them, and a successful write may return items: [] when its resulting row is unreadable.

Configure the client

from general_manager.interface import (
    RemoteManagerInterface,
    RequestField,
    UrllibRequestTransport,
)
from general_manager.manager import GeneralManager, Input


class RemoteProject(GeneralManager):
    class Interface(RemoteManagerInterface):
        id = Input(int)
        name = RequestField(str)

        class Meta:
            transport = UrllibRequestTransport()
            base_url = "https://project-service.example.com"
            base_path = "/internal/gm"
            remote_manager = "projects"
            protocol_version = "v1"
            websocket_invalidation_enabled = True

Configure the client's HTTP auth_provider or transport_config to supply credentials recognized by the server's authentication middleware. The server example requires isAuthenticated; the protocol-version header does not meet that requirement. See the request transport how-to for an auth-provider declaration.

Client websocket connections must provide the same authenticated session as the server expects; the default URL-only connection factory does not copy HTTP cookies or authorization headers. Use a custom connection_factory when the websocket library needs explicit credentials, and pass manager classes to RemoteInvalidationClient.

Use bounded queries

Every RemoteAPI query is finite: omitted controls mean page=1 and page_size=10. Configure positive GENERAL_MANAGER["REMOTE_API_DEFAULT_PAGE_SIZE"] and GENERAL_MANAGER["REMOTE_API_MAX_PAGE_SIZE"] values when the defaults need to change; the default cannot exceed the maximum. Non-null page and page_size values must be positive integers; boolean values are rejected. Omitted or null controls use the defaults. Invalid controls or a page_size above the maximum return HTTP 400 before execution. Request later pages explicitly when more rows are needed.

Read permissions are applied before local ordering, counting, and slicing. An incomplete upstream response that needs per-instance checks omits total_count; a request-backed manager with multiple permission alternatives rejects global pagination or ordering because the server cannot prove a global page. RemoteManager forwards matching page controls upstream. The server accepts matching upstream pages without treating the source as complete and caps the response at the effective page size, even if upstream returns too many rows. Incomplete sources reject mismatched pagination or ordering that cannot be forwarded upstream.

See the RemoteAPI concept, the end-to-end cookbook, and the interface API reference and GraphQL API reference for the complete callable contracts.