Skip to content

Hook Module

mosaicolabs.hook.HookServer

HookServer()

Receives the events mosaicod sends to a hook, and passes each one to every receiver.

Register the server's URL with mosaicod hook create <URL>.

Requests aren't authenticated, so anything that can reach the server can send it events. Keep it on a network only mosaicod can reach.

Example
from mosaicolabs.hook import Event, HookServer, TopicUploaded

hook = HookServer()

@hook
def on_event(e: Event):
    if isinstance(e, TopicUploaded):
        print(f"{e.topic_name} uploaded to {e.sequence_name}")

if __name__ == "__main__":
    hook.start(host="0.0.0.0", port=8080)

start

start(host='127.0.0.1', port=DEFAULT_PORT, log_level=None)

Listens for events until interrupted, for example with Ctrl-C.

host and port are where this server listens. Register that URL with mosaicod hook create.

Parameters:

Name Type Description Default
host str

The address to listen on. The default, 127.0.0.1, is only reachable from this machine. Use "0.0.0.0" when mosaicod runs on another machine or container.

'127.0.0.1'
port int

The port to listen on. Keep it fixed, so the URL registered in mosaicod stays valid across restarts.

DEFAULT_PORT
log_level str | None

If set, for example to "DEBUG", it first calls setup_sdk_logging(level=log_level), which takes precedence over any logging set up before.

None

It logs where it listens, and any receiver that raises. Unless log_level is set or logging is already set up, for example with setup_sdk_logging or logging.basicConfig, it first calls setup_sdk_logging(), which logs INFO and above to stderr.

mosaicolabs.hook.Event

Bases: BaseModel

Base class of the events mosaicod sends to hooks.

A receiver gets every event, and tells them apart by their class:

Example
@hook
def on_event(e: Event):
    match e:
        case TopicUploaded():
            print(f"{e.topic_name} uploaded to {e.sequence_name}")
        case SessionUploaded():
            print(f"{len(e.topics)} topics uploaded to {e.sequence_name}")

Attributes:

Name Type Description
id str

Identifies the event. A retry delivers the same event with the same id.

mosaicolabs.hook.TopicUploaded

Bases: Event

A topic's upload finished, and its data can't change anymore.

Attributes:

Name Type Description
sequence_name str

The topic's sequence.

topic_name str

The topic.

session_locator str

The session the topic was uploaded in, as sequence_name:session_identifier.

ontology_tag str

The ontology tag of the topic's data.

user_metadata dict[str, Any] | None

The metadata given when the topic was created, if any.

created_timestamp int

When the topic was created, in Unix nanoseconds.

mosaicolabs.hook.SessionUploaded

Bases: Event

A session was finalized, and all its topics are uploaded.

Attributes:

Name Type Description
sequence_name str

The session's sequence.

session_locator str

The session, as sequence_name:session_identifier.

topics list[str]

The names of the session's topics.

created_timestamp int

When the session was created, in Unix nanoseconds.