There is two parts to improve the way we work with the controller.
We can split the way we validate the header and ibased on that instantiate the controller into two parts. Something along the lines of:
def _init_controller(req_header: Header): # or eg controller_factory(req_header):
"""
I'm a doctsring
"""
is_valid_tenant_header = "wallet_id" in req_header and "tenant_jwt" in req_header
is_valid_admin_header = "api_key" in req_header
is_valid_header = req_header and (is_valid_tenant_header or is_valid_admin_header)
if is_valid_header:
req_header = eval(req_header)
if "api_key" in req_header:
controller = AriesAgentController(
admin_url=f"{admin_url}:{admin_port}",
api_key=req_header["api_key"],
is_multitenant=is_multitenant,
)
else:
controller = AriesTenantController(
admin_url=f"{admin_url}:{admin_port}",
wallet_id=req_header["wallet_id"],
tenant_jwt=req_header["tenant_jwt"],
)
return controller
else:
raise HTTPException(
status_code=400,
detail="Bad headers. Either provide an api_key or both wallet_id and tenant_jwt",
)
2. We could actually make use of FastAPIs yield dependencies to pass the controller to other contexts. See [the docs here](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-with-yield/)
Happy, to work on that/pick that up myself soon. If anyone else does that meanwhile, please go ahead.
There is two parts to improve the way we work with the controller.
@asynccontextmanager async def generate_controller(req_header: Header): """ I'm a docstring """ try: controller = _init_controller(req_header) yield controller except Exception as e: logger.error(f"{e!r}") finally: await controller.terminate()