Skip to content

Following and saving position

LogFollower

LogFollower(api, logs=('Events',), *, sections=(), limit=None, cursor=None, save_to=None)

Bases: _FollowerBase

Polls GET /v1/server with Since and hands back only entries you haven't seen.

Handles the API's cursor rules for you: entries are returned oldest first, entries you've already handled (by Seq, per log) are skipped, and the request is timed for just after the server's next heartbeat (slowly while it's offline).

Looping over it keeps going through network problems, rate limits and API errors, waiting a little longer after each failure in a row (up to a minute). It only stops for errors that waiting won't fix, like a wrong key, and raises those.

An entry that changes (a mod call resolved, a command result arriving, a shot gaining its hit) comes again with a higher Seq and the same Id: replace what you have by Id.

Example::

for data, new in api.follow("Events", "KillLogs", sections=["Players"]):
    for event in new["Events"]:
        print(event["Type"], event)

Parameters:

Name Type Description Default
api Any

The client.

required
logs Sequence[str]

Log sections to follow (Events, KillLogs, ModCalls, Shots…).

('Events',)
sections Sequence[str]

Other sections to fetch on every poll (e.g. Players).

()
limit Optional[int]

Most entries per log per request (API default 100, max 500).

None
cursor Optional[int]

Resume from a Cursor you saved. None starts with the newest limit entries of each log.

None
save_to Any

A file path, or an object with load()/save(state), to remember the position across restarts. See :mod:oxrpy.state.

None

poll

poll()

Make one request. Returns (data, new) where new[log] lists unseen entries, oldest first. Errors are raised, not retried.

iter

iter(*, retry=True, stop=None, on_error=None)

Poll until stop is set (or forever). With retry=False the first error is raised.

state

Remembering where a follower got to, so a restarted bot carries on where it left off.

Pass save_to= to run_forever() or follow():

  • a file path ("cursor.json"), which oxrpy reads at start and rewrites after each poll, or
  • your own object with load() -> dict | None and save(state: dict) methods, to keep it in a database, Redis, or wherever your bot keeps things.

The position is saved after your handlers have run for a poll. If your bot crashes part-way through, the entries from that poll come again on restart, so nothing is lost. A handler may very occasionally see an entry twice; use the entry's Id if that matters.

StateStore

Bases: Protocol

Anything with these two methods can hold a follower's position.

FileState

FileState(path)

Keeps the position in a small JSON file. Writes are atomic: a crash mid-write never leaves a half-written file.

as_store

as_store(save_to)

Turn a save_to= argument into a store.

read_state

read_state(state)

Check a loaded state; returns {"Cursor", "Handled"} or None if it's unusable.