Context Manager¶
AmsdalContextManager provides a request-scoped dictionary for storing data that needs to be accessible across different parts of your application. The context is isolated per request — it is not shared between concurrent requests.
from amsdal import AmsdalContextManager
Usage¶
from amsdal import AmsdalContextManager
from amsdal.transactions import transaction
@transaction
def GetCurrentUserIP() -> str:
context = AmsdalContextManager.get_context()
request = context['request']
return request.client.host
Available Methods¶
| Method | Description |
|---|---|
get_context() |
Returns the current context dictionary (dict[str, Any]) |
set_context(context) |
Replaces the entire context dictionary |
add_to_context(key, value) |
Adds a single key-value pair to the context |
Server Request Context¶
When running with AMSDAL Server, the request key is automatically added to the context for every incoming HTTP request. This gives you access to the full Starlette Request object:
@transaction
def ProcessRequest() -> dict:
context = AmsdalContextManager.get_context()
request = context['request']
return {
'ip': request.client.host,
'method': request.method,
'path': request.url.path,
'headers': dict(request.headers),
}
Common Use Cases¶
- User authentication: store the authenticated user for access in hooks or nested transactions
- Request metadata: track request IDs, start times, or client info for logging
- Feature flags: pass runtime configuration without threading it through function parameters