Context Manager
The AmsdalContextManager
class is a utility class that manages a context dictionary. This dictionary can be used to
store and retrieve data that needs to be accessible across different parts of your application during a single request
or operation.
Examples of when you might want to use the AmsdalContextManager
:
-
Storing User Information: If you're processing a web request and you've authenticated the user, you might want to store the user's information in the context so that it can be accessed by other parts of your application without having to pass the user information around as a parameter.
-
Tracking Request Metadata: If you want to track information about the request, like the start time or the request ID, you can store this information in the context. This can be useful for logging or debugging purposes.
The context is not shared between different requests or operations, so it's a safe place to store request-specific data.
During request server automatically add Request
object to the context, so you can access it from any part of your application.
from amsdal.context.manager import AmsdalContextManager
from amsdal_data.transactions import transaction
from fastapi import Request
@transaction
def DetectIP() -> str:
context = AmsdalContextManager.get_context()
request: Request = context['request']
return request.client.host