Reputation: 21
In ASP.NET Core you have the concept of adding a service/object as "scoped" which means that it is the same object within a request. We used this concept e.g. to get the user out of the Bearer token header and provide the user data project-wide over a "CurrentUserService".
I'm searching for a way to do this using Pythons FastAPI and Beanie framework. Here is the use-case: I want to use the event-based actions of beanie to automatically set the user data before a database update. Here is the current state of the code:
class DataEntity(Document):
name: str
last_modified_by: str
@before_event(Replace)
def set_version_data(self):
#here the current user should be set into last_modified_by
So my question is: How can I set the last_modified_by field on this event level?
I already tried to use the FastAPI dependencies, because they look like a scoped wrap-around I searched for:
async def get_current_user(x_user: str = Header(None)) -> str:
return x_user
class DataEntity(Document):
name: str
last_modified_by: str
@before_event(Replace)
def set_version_data(self, current_user: str = Depends(get_current_user)):
self.last_modified_by = current_user
But this is not handled right and I get the error "pydantic.error_wrappers.ValidationError: 1 validation error for DataEntity: last_modified_by - str type expected (type=type_error.str)".
Upvotes: 0
Views: 591