Open M3te0r opened 2 years ago
This is a much needed feature for DRY. Currently one router can only be attached to one API instance. For context, FastAPI supports this feature:
You can also use
.include_router()
multiple times with the same router using different prefixes.This could be useful, for example, to expose the same API under different prefixes, e.g.
/api/v1
and/api/latest
.
I used the following as a workaround. It is not the cleanest solution but seems to be working (not thoroughly tested):
from copy import copy
api_latest = NinjaAPI(version="v1", urls_namespace="latest")
api_latest.add_router("/bar/", copy(router))
api_v1 = NinjaAPI(version="v1")
api_v1.add_router("/bar/", router)
Make sure that you attach the copy()
of the router
before you attach the original one.
This is a much needed feature for DRY. Currently one router can only be attached to one API instance. For context, FastAPI supports this feature:
You can also use
.include_router()
multiple times with the same router using different prefixes. This could be useful, for example, to expose the same API under different prefixes, e.g./api/v1
and/api/latest
.I used the following as a workaround. It is not the cleanest solution but seems to be working (not thoroughly tested):
from copy import copy api_latest = NinjaAPI(version="v1", urls_namespace="latest") api_latest.add_router("/bar/", copy(router)) api_v1 = NinjaAPI(version="v1") api_v1.add_router("/bar/", router)
Make sure that you attach the
copy()
of therouter
before you attach the original one.
When using copy, the router auth will be overwritten, and it is better to use deepcopy.
This also creates an issue with testing if you'd like to test a specific router without importing the whole API to your tests. This is to prevent cross-dependencies between Django apps:
@pytest.fixture(scope="session")
def api_mock() -> NinjaAPI:
api = NinjaAPI(urls_namespace="tests", version="vt")
def sample_view(request: AuthedRequest):
return {"message": "secret_things"}
# Add a protected view
auth_router.api_operation("GET", "protected/", auth=JWTAuth())(sample_view)
# Add the views to the API (make sure it's the same as the real API)
api.add_router("/auth", auth_router)
# Use the same exception handler as the real API so that errors are formatted the same
api.add_exception_handler(APIError, handler=api_error)
return api
Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Hi Vitaliy, first thank you for this really great project !
I would like to reuse some of my routers with multiple NinjaAPI instances. Sharing some common routers (and paths) with the end user API and the Admin API instances; but having different urls_namespace, docs, auth etc In my case the first attempt was to share the same login router but it resulted in an error
Here is a reproductible minimal example :
Describe the solution you'd like A clear and concise description of what you want to happen.
I don't know if this is something achievable, I saw that the api is attached to the router with
router.set_api_instance(self)