schedr.api

schedr’s REST API from Python (M4.11h): events posted and listed.

Inside a running task or trigger, nothing is configured: requests go through the task’s agent, which has its master make them as the task (task:<dag>/<run>/<task> is who posted). A command task’s Python script does the same over its context channel. Elsewhere (a script of one’s own), the client goes over HTTP (the http extra), to server or SCHEDR_SERVER, as the schedr CLI does:

from schedr import api

api.post_event("payment-completed", key=order, data={"amount": 12})
for event in api.events(name="payment-completed", limit=10):
    ...

Tasks may call only the routes open to them (posting and listing events, for now); a route outside them is refused (403). The async functions (apost_event, aevents) run the call in a thread, so a trigger’s loop goes on meanwhile.

exception schedr.api.ApiError(status, body)

Bases: Exception

The API answered an error status.

Parameters:
  • status (int)

  • body (object)

Return type:

None

status

The HTTP status (503: the master couldn’t be reached in time; the call may still be made, so send it again with the same idempotency_key).

message

What the API said.

body

Its whole JSON answer.

class schedr.api.Client(server=None, *, token=None, user=None, password=None, caller=None)

Bases: object

schedr’s REST API: through the task’s agent inside a task, over HTTP elsewhere.

Parameters:
  • server (str | None)

  • token (str | None)

  • user (str | None)

  • password (str | None)

  • caller (ApiCaller | None)

request(method, path, *, json=None, idempotency_key=None)

A request to the API.

Parameters:
  • method (str) – GET, POST, …

  • path (str) – The path and its query, e.g. /api/v1/events?name=paid.

  • json (object) – The body; None for none.

  • idempotency_key (str | None) – Sent again with the same key, the request is the one made first.

Returns:

The JSON answer.

Raises:
  • ApiError – For an error status.

  • ValueError – For a path not starting with /: joined to the server’s URL, it could name another host or port, and the credentials would go there.

Return type:

object

post_event(name, *, key, data=None, at=None, idempotency_key=None)

Posts an event.

A trigger on its name starts a run per key; a step waiting for it goes on.

Parameters:
  • name (str) – Its name, written as a DAG ID is.

  • key (str) – Its key.

  • data (object) – Its data, JSON.

  • at (datetime | None) – When it happened (aware); now by default.

  • idempotency_key (str | None) – Sent again with the same key (a retried task), it is the event stored the first time.

Returns:

The event as stored.

Raises:

ValueError – For a naive at.

Return type:

PostedEvent

notify(channel, *, text=None, data=None)

Sends a message to a notification channel, from a running task (decision 421).

The channel is one of the task’s DAG (Dag(notify=...)) or a server channel; the master fills its body (text and data among the values) and delivers it later: this returns once it is queued.

Parameters:
  • channel (str) – The channel’s name.

  • text (str | None) – Its text (the text placeholder; schedr’s summary otherwise).

  • data (object) – Its data, JSON (the data placeholder).

Raises:

ApiError – For a channel the DAG doesn’t have, a field of data the channel’s body uses and data lacks, or more than [notifications] max_per_attempt sends (429). Outside a task, the server has no such route (404).

Return type:

None

events(*, name=None, limit=50)

The events posted, newest first.

Parameters:
  • name (str | None) – Only those of this name.

  • limit (int) – How many at most (500 at most).

Returns:

The events.

Return type:

list[PostedEvent]

class schedr.api.PostedEvent(id, name, key, data, at, posted_at, posted_by)

Bases: object

An event posted through the API (EventNamed).

Parameters:
  • id (str)

  • name (str)

  • key (str)

  • data (object)

  • at (datetime)

  • posted_at (datetime)

  • posted_by (str)

id

Its ID.

Type:

str

name

Its name.

Type:

str

key

Its key.

Type:

str

data

Its data, JSON (masked where it holds a secret’s value).

Type:

object

at

When it happened.

Type:

datetime.datetime

posted_at

When it was posted.

Type:

datetime.datetime

posted_by

Who posted it: a user, or task:<dag>/<run>/<task>.

Type:

str

classmethod from_json(value)

The event in the API’s answer.

Parameters:

value (Mapping[str, Any]) – Its JSON.

Returns:

The event.

Return type:

PostedEvent

async schedr.api.aevents(*, name=None, limit=50)

events() from a coroutine, run in a thread.

Parameters:
  • name (str | None) – Only those of this name.

  • limit (int) – How many at most.

Returns:

The events.

Return type:

list[PostedEvent]

async schedr.api.apost_event(name, *, key, data=None, at=None, idempotency_key=None)

post_event() from a coroutine: run in a thread, the loop goes on meanwhile.

Parameters:
  • name (str) – Its name.

  • key (str) – Its key.

  • data (object) – Its data, JSON.

  • at (datetime | None) – When it happened (aware); now by default.

  • idempotency_key (str | None) – Sent again with the same key, it is the event stored the first time.

Returns:

The event as stored.

Return type:

PostedEvent

schedr.api.events(*, name=None, limit=50)

The events posted, newest first, as Client.events().

Parameters:
  • name (str | None) – Only those of this name.

  • limit (int) – How many at most.

Returns:

The events.

Return type:

list[PostedEvent]

schedr.api.post_event(name, *, key, data=None, at=None, idempotency_key=None)

Posts an event as Client.post_event(), with a client made here.

Parameters:
  • name (str) – Its name.

  • key (str) – Its key.

  • data (object) – Its data, JSON.

  • at (datetime | None) – When it happened (aware); now by default.

  • idempotency_key (str | None) – Sent again with the same key, it is the event stored the first time.

Returns:

The event as stored.

Return type:

PostedEvent