Jam configuration¶
Config¶
The configuration only works for jam.Jam/jam.aio.Jam.
Standalone modules such as jam.jwt.JWT, jam.paseto.PASETOv4, etc. are configured simply by the class's __init__. For each model, see the corresponding documentation.
Instance¶
The *.Jam class itself has several parameters:
from jam import Jam
jam = Jam(
config="path/to/config/file.toml.yaml.json", # or python-dict
pointer="jam",
logger=JamLogger,
log_level="INFO",
serializer=JsonEncoder
)
config: str | dict[str, Any]¶
This is the path to your config as a string or dict with the configuration:
Python dict¶
import os
from jam import Jam
config = {
"jwt": {
"alg": "HS256",
"secret_key": os.getenv("JWT_SECRET_KEY")
},
"paseto": {
"version": "v4",
"purpose": "local",
"key": os.getenv("PASETO_SECRET_KEY")
}
}
}
jam = Jam(config=config)
jwt = jam.jwt_create(payload={"user": 1})
paseto = jam.paseto_create(payload={"user": 1}, footer=None)
TOML¶
[jam.jwt]
alg = "HS256"
secret_key = "$JWT_SECRET_KEY"
[jam.paseto]
version = "v4"
purpose = "local"
key = "$PASETO_SECRET_KEY"
from jam import Jam
jam = Jam(config="config.toml")
jwt = jam.jwt_create(payload={"user": 1})
paseto = jam.paseto_create(payload={"user": 1}, footer=None)
YAML¶
jam:
jwt:
alg: HS256
secret_key: $JWT_SECRET_KEY
paseto:
version: v4
purpose: local
key: $PASETO_SECRET_KEY
from jam import Jam
jam = Jam(config="config.yaml")
jwt = jam.jwt_create(payload={"user": 1})
paseto = jam.paseto_create(payload={"user": 1}, footer=None)
Json¶
{
"jwt": {
"alg": "HS256",
"secret_key": "$JWT_SECRET_KEY"
},
"paseto": {
"version": "v4",
"purpose": "local",
"key": "$PASETO_SECRET_KEY"
}
}
from jam import Jam
jam = Jam(config="config.json")
jwt = jam.jwt_create(payload={"user": 1})
paseto = jam.paseto_create(payload={"user": 1}, footer=None)
pointer: str = "jam"¶
This is the point that Jam will read as config.
For example, if we do it like this:
from jam import Jam
jam = Jam(
config="config.toml",
pointer="anotherpointer" # <- Another pointer
)
jwt = jam.jwt_create(payload={"user": 1})
paseto = jam.paseto_create(payload={"user": 1}, footer=None)
Our config file will look like this:
[anotherpointer.jwt] # pointer
alg = "HS256"
secret_key = "$JWT_SECRET_KEY"
[anotherpointer.paseto] # pointer
version = "v4"
purpose = "local"
key = "$PASETO_SECRET_KEY"
Tip
This can be useful for configuring two instances of Jam in a single file, for example.
logger: type[BaseLogger] = JamLogger¶
Logger class. Useful if you need to replace the standard logger.
from jam import Jam
from yourapp.logger import YourCustomLogger
jam = Jam(config="config.toml", logger=YourCustomLogger)
This parameter can also be configured in the config file, but then you need to pass the path to the class in Python style:
[jam]
logger = "yourapp.logger.YourCustomLogger"
[jam.jwt]
alg = "HS256"
secret_key = "$JWT_SECRET_KEY"
For more details, see the documentation on logging.
log_level: str = "INFO"¶
Jam logging level.
The takes one of the following values: DEBUG, INFO, WARNING, ERROR, CRITICAL.
It can also be passed in the config file:
[jam]
log_level = "INFO"
[jam.jwt]
alg = "HS256"
secret_key = "$JWT_SECRET_KEY"
serializer: type[BaseEncoder] = JsonEncoder¶
JSON object serializer. By default, JsonEncoder is used, which utilizes sdtlib.json.
It can also be passed in the config file as a string:
[jam]
serializer = "jam.encoders.JsonEncoder"
[jam.jwt]
alg = "HS256"
secret_key = "$JWT_SECRET_KEY"
For more details, see the documentation on serialization.
Environment variables¶
Jam will automatically search for environment variables
if a value begins with $ in config files. For python dict, use os.getenv.
Example:
[jwt]
log_level = "INFO"
[jam.jwt]
alg = "$JWT_ALG"
secret_key = "$JWT_SECRET"
Note
Some modules read certain environment variables by default, as described in detail in each module.