Internal APIs

A Flask extension to add support for OpenHeart protocol.

class flask_openheart.internal.Backend(*args, **kwargs)

A simple protocol for allowing emoji-based reactions to URIs.

abstractmethod connect(*args, **kwargs)

Initiate the connection.

Parameters:
  • args – Positional arguments to pass to the connect function.

  • kwargs – Keyword arguments to pass to the connect function.

abstractmethod disconnect()

Close the connection.

abstractmethod incr(slug, reaction)

Add a reaction for a given page.

Parameters:
  • slug – A slug representing the page.

  • reaction – The reaction to add.

abstractmethod is_connected()

Check whether the backend is connected.

Returns:

True if connected, False otherwise.

abstractmethod iter(slug)

Iterate all reactions for a given page.

Parameters:

slug – A slug representing the page.

Returns:

A generator yielding: reaction, count.

exception flask_openheart.internal.BackendError

Exception raised whenever a backend encounters some kind of database error.

class flask_openheart.internal.Storage(slug, config)

The storage is an interface between the extension and the backend.

react(reaction)

Add a reaction for a given page.

Parameters:

reaction – The reaction to add.

Returns:

The updated reactions, as a dict.

property reactions

Get all reactions for a given page, as a dict.

Returns:

A dict in which the reactions are the keys and the counts are the values.

flask_openheart.internal.get_backend(uri, *args, **kwargs)

Get a backend context manager, automatically detecting the type from URI.

Parameters:
  • uri – The database uri.

  • args – Additional args passed to the backend connection function.

  • kwargs – Additional keyword args passed to the backend connection function.

Raises:

RuntimeError – Unrecognized URI prefix

Returns:

An OpenHeartBackend object (not yet connected).

The Sqlite backend can be used to to store data in a local database file.

class flask_openheart.internal.sqlite.SqliteBackend(*args, **kwargs)

The Sqlite backend can be used to to store data in a local database file.

connect(uri, *args, **kwargs)

Initiate the connection.

Parameters:
  • uri – The database URI.

  • args – Extra positional arguments to pass to the connect function.

  • kwargs – Extra keyword arguments to pass to the connect function.

disconnect()

Close the connection.

incr(slug, reaction)

Increment the reaction count for a certain reaction on a certain page.

Parameters:
  • slug – A slug representing the page.

  • reaction – The emoji reaction to be incremented.

is_connected()

Check whether the backend is connected.

Returns:

True if connected, False otherwise.

iter(slug)

Iterate through all the reactions on a certain page.

Parameters:

slug – A slug representing the page.

Returns:

A generator yielding: reaction, count.

The valkey backend can be used to connect to Valkey or Redis servers.

class flask_openheart.internal.keystore.ValkeyBackend(*args, **kwargs)

The Valkey backend can be used to connect to Valkey or Redis servers.

connect(uri, *args, **kwargs)

Initiate the connection.

Parameters:
  • uri – The database URI.

  • args – Extra positional arguments to pass to the connect function.

  • kwargs – Extra keyword arguments to pass to the connect function.

disconnect()

Close the connection.

incr(slug, reaction)

Increment the reaction count for a certain reaction on a certain page.

Parameters:
  • slug – A slug representing the page.

  • reaction – The emoji reaction to be incremented.

is_connected()

Check whether the backend is connected.

Returns:

True if connected, False otherwise.

iter(slug)

Iterate through all the reactions on a certain page.

Parameters:

slug – A slug representing the page.

Returns:

A generator yielding: reaction, count.