easyauth icon indicating copy to clipboard operation
easyauth copied to clipboard

How to add routes of APIRouter to easy auth api router

Open aghure opened this issue 2 years ago • 2 comments

I am trying to use easy auth with fast api class base view. To do the same , i need to have APIRouter.routes type property to easyAuthAPIRouter. Is there any way to add the same to EasyAuthAPIRouter.

aghure avatar Apr 27 '23 17:04 aghure

Hello @aghure , every EasyAuthAPIRouter has an api_router attribute which is a direct reference to the APIRouter instance created, perhaps you can reference the routes property here? If that does not work for you, I would need a more substantial code example of what you are trying to accomplish along with additional context.

codemation avatar Apr 27 '23 23:04 codemation

Hi @codemation I am trying to use the EasyAuthAPIRouter with class base views. Adding example below about what i am trying to do and adding sample output below.


import logging
from sqlalchemy.orm import  Session
from fastapi import APIRouter, Request, Body ,Depends
from fastapi.responses import JSONResponse

from fastapi_utils.cbv import cbv
from ml_app.core import  get_db
from easyauth.router import  EasyAuthAPIRouter
logger = logging.getLogger(__name__)
test_cbv_router = EasyAuthAPIRouter.create(prefix='/finance', tags=['finance'])
@cbv(test_cbv_router.server)  # Step 2: Create and decorate a class to hold the endpoints
class TestCBV:
   
    db_session : Session = Depends(get_db)
    @test_cbv_router.post("/test")
    def test_url(self,request: Request):
        return  JSONResponse({'status':'ok'})
@test_cbv_router.get("/existing")
def existing_contact(request: Request,db_session: Session = Depends(get_db)):

    result = dict()
    result["result_data"] = ["contact_list"]
    result["tmpl_name"] = "existing.html"
    return  result


image

The @cbv is class base view router of https://github.com/dmontagu/fastapi-utils, it needs APIRouter() instance to add the routes in the app. We want to use EasyAuthAPIRouter with class base views. So with EasyAuthAPIRouter.server we get APIRouter() , but CBV is adding it in base app . Here I have highlighted the URL issues happening because of the same.

aghure avatar Apr 28 '23 06:04 aghure