Use request global variables in FastAPI
Project description
fastapi-globals
Allows you to use request global values. Can be used to store request speficic data, like state, database connection, etc.
Installation
Just use pip install fastapi-globals to install the library.
Note: fastapi-globals is compatible with fastapi versions 0.100.0 and later on
Python 3.9, 3.10, 3.11, 3.12 and 3.13. This is also ensured running all tests on all those versions
using tox.
Usage
Just import g and then access (set/get) attributes of it:
from fastapi_globals import g
g.foo = "foo"
# In some other code
assert g.foo == "foo"
Best way to utilize the global g in your code is to set the desired
value in a FastAPI dependency, like so:
async def set_global_foo() -> None:
g.foo = "foo"
@app.get("/test/", dependencies=[Depends(set_global_foo)])
async def test():
assert g.foo == "foo"
Setup
Add the GlobalsMiddleware to your app:
app = fastapi.FastAPI(
title="Your app API",
)
app.add_middleware(GlobalsMiddleware) # <-- This line is necessary
Then just use it. ;-)
Default values
You may use g.set_default("name", some_value) to set a default value
for a global variable. This default value will then be used instead of None
when the variable is accessed before it was set.
Note that default values should only be set at startup time, never
inside dependencies or similar. Otherwise you may run into the issue that
the value was already used any thus have a value of None set already, which
would result in the default value not being used.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_globals-0.1.1.tar.gz.
File metadata
- Download URL: fastapi_globals-0.1.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1eca8a468788f982f5b1fdb07f3df7c78a0de615a513ab447f2983c9f468570
|
|
| MD5 |
117f881e26dd1cab2dbd69e26de3c859
|
|
| BLAKE2b-256 |
5c9297d55bee935b8f39f2ca487641fdef4573d9c704cd6b8dbf29e3afe62092
|
Provenance
The following attestation bundles were made for fastapi_globals-0.1.1.tar.gz:
Publisher:
release.yml on team23/fastapi-globals
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_globals-0.1.1.tar.gz -
Subject digest:
b1eca8a468788f982f5b1fdb07f3df7c78a0de615a513ab447f2983c9f468570 - Sigstore transparency entry: 151480694
- Sigstore integration time:
-
Permalink:
team23/fastapi-globals@2fdabf0efa3146ffe6b72adddae138966144e307 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/team23
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2fdabf0efa3146ffe6b72adddae138966144e307 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_globals-0.1.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_globals-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a76e3c506cc4e0ebad795a8e738934bd9795b0ef43a3dbdafd68ed34c11e192
|
|
| MD5 |
4157455cb9b527fffb1648b9164ce147
|
|
| BLAKE2b-256 |
eec5538b6ceefdc4d87058acb4afa6b67e4388dc8bc1f122b0b26246dfe1c44c
|
Provenance
The following attestation bundles were made for fastapi_globals-0.1.1-py3-none-any.whl:
Publisher:
release.yml on team23/fastapi-globals
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_globals-0.1.1-py3-none-any.whl -
Subject digest:
5a76e3c506cc4e0ebad795a8e738934bd9795b0ef43a3dbdafd68ed34c11e192 - Sigstore transparency entry: 151480701
- Sigstore integration time:
-
Permalink:
team23/fastapi-globals@2fdabf0efa3146ffe6b72adddae138966144e307 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/team23
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2fdabf0efa3146ffe6b72adddae138966144e307 -
Trigger Event:
release
-
Statement type: