Token Store#

A token store is required to save the session token received from the Momotor broker.

The token store to use is configured using the MOMOTOR_BROKER.TOKEN_STORE_CLASS settings.

Provided classes#

There are four token store implementations provided:

  • class momotor.django.token_store.dummy.DummyTokenStore#

    Does not store the token. A new session to the broker is created every time.

    Useful for testing.

  • class momotor.django.token_store.memory.InMemoryTokenStore#

    Stores the token in memory for the current thread. A separate session to Momotor is created for each thread and process.

    Not very useful in itself, but used as base class for more complex token stores, where the in-memory token is used to reduce the number of times an external resource needs to be accessed. Only when the token does not exist in-memory, the external resource is accessed.

  • class momotor.django.token_store.cache.CachedTokenStore#

    Stores the token in Django’s cache. This store adds two more settings to the MOMOTOR_BROKER dictionary: TOKEN_CACHE_NAME and TOKEN_KEY.

  • class momotor.django.token_store.model.ModelTokenStore#

    Stores the token in a Django database model. This store adds one more setting to the MOMOTOR_BROKER dictionary: TOKEN_DATABASE_NAME

Providing a custom token store#

A custom token store can be provided, for example when the Django app is not using Django’s default database and cache mechanisms.

The custom token store should subclass from the abstract base class BaseTokenStore

class momotor.django.token_store.base.BaseTokenStore(settings, *, loop=None, executor=None, **kwargs)#

Abstract base class for token stores. The store is provided with the MOMOTOR_BROKER settings and an asyncio loop and executor.

BrokerConnection uses this token store. Any additional keyword arguments provided to BrokerConnection at creation are passed on to the token store to allow for addition parameters to custom token stores.

Parameters:
  • settings (dict) – The MOMOTOR_BROKER dictionary from the settings

  • loop – The asyncio loop to use the executor with. If None, uses the current active event loop.

  • executor (Executor) – An executor to run I/O blocking tasks on. If None, uses the current loop’s default executor.

  • kwargs – The additional token store arguments passed to BrokerConnection

_run_in_executor(func, *args, **kwargs)#

Run a function in the executor provided to __init__

Parameters:
  • func (Callable) – function to run

  • args – arguments to the function

  • kwargs – keyword arguments to the function

Returns:

return value of the function

async delete()#

Delete the token

async get()#

Get the current token from the store

Return type:

str | None

Returns:

The token. Or None if not currently authorized

async set(token)#

Set the token

Parameters:

token (str) – The token to save

executor#

The executor argument as provided to the constructor

loop#

The loop argument as provided to the constructor

settings#

The settings argument as provided to the constructor