didx-xyz / aries-cloudapi-python

Apache License 2.0
12 stars 8 forks source link

Refactor the way we the agent controller is created and provided #12

Closed morrieinmaas closed 3 years ago

morrieinmaas commented 3 years ago

There is two parts to improve the way we work with the controller.

  1. 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",
        )

@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()



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. 
mrcbond88 commented 3 years ago

Open PR, awaiting Feature #19 to be merged first. @michaelwiles