| 1 |
from functools import partial |
| 2 |
from typing import TYPE_CHECKING, Any, List |
| 3 |
|
| 4 |
from django.urls import path |
| 5 |
|
| 6 |
from .views import default_home, openapi_json, openapi_view |
| 7 |
|
| 8 |
if TYPE_CHECKING: |
| 9 |
from ninja import NinjaAPI |
| 10 |
|
| 11 |
__all__ = ["get_openapi_urls", "get_root_url"] |
| 12 |
|
| 13 |
|
| 14 |
def get_openapi_urls(api: "NinjaAPI") -> List[Any]: |
| 15 |
result = [] |
| 16 |
|
| 17 |
if api.openapi_url: |
| 18 |
view = partial(openapi_json, api=api) |
| 19 |
if api.docs_decorator: |
| 20 |
view = api.docs_decorator(view) |
| 21 |
result.append( |
| 22 |
path(api.openapi_url.lstrip("/"), view, name="openapi-json"), |
| 23 |
) |
| 24 |
|
| 25 |
assert ( |
| 26 |
api.openapi_url != api.docs_url |
| 27 |
), "Please use different urls for openapi_url and docs_url" |
| 28 |
|
| 29 |
if api.docs_url: |
| 30 |
view = partial(openapi_view, api=api) |
| 31 |
if api.docs_decorator: |
| 32 |
view = api.docs_decorator(view) |
| 33 |
result.append( |
| 34 |
path(api.docs_url.lstrip("/"), view, name="openapi-view"), |
| 35 |
) |
| 36 |
|
| 37 |
return result |
| 38 |
|
| 39 |
|
| 40 |
def get_root_url(api: "NinjaAPI") -> Any: |
| 41 |
return path("", partial(default_home, api=api), name="api-root") |