Skip to content

Commit 596b37d

Browse files
authored
Merge pull request #138 from python-ellar/openapi_document_renderer_refactor
OpenAPI module Refactor
2 parents 4211a29 + 425a89d commit 596b37d

File tree

15 files changed

+79
-66
lines changed

15 files changed

+79
-66
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ repos:
1414
name: Code Formatting
1515
entry: "make fmt"
1616
types: [python]
17-
language_version: python3.10
17+
language_version: python3
1818
language: python
1919
- id: code_linting
2020
args: [ ]
2121
name: Code Linting
2222
entry: "make lint"
2323
types: [ python ]
24-
language_version: python3.10
24+
language_version: python3
2525
language: python
2626
- repo: https://github.com/pre-commit/pre-commit-hooks
2727
rev: v2.3.0

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ clean: ## Removing cached python compiled files
99
find . -name \*pyo | xargs rm -fv
1010
find . -name \*~ | xargs rm -fv
1111
find . -name __pycache__ | xargs rm -rfv
12+
find . -name .ruff_cache | xargs rm -rfv
1213

1314
install: ## Install dependencies
1415
flit install --deps develop --symlink

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ $(venv) pip install ellar
5050
## **Try This**
5151
```python
5252
import uvicorn
53-
from ellar.common import Body, Controller, ControllerBase, delete, get, post, put, Serializer, Provide
53+
from ellar.common import Body, Controller, ControllerBase, delete, get, post, put, Serializer, Inject
5454
from ellar.core import AppFactory
5555
from ellar.di import injectable, request_scope
56-
from ellar.openapi import OpenAPIDocumentModule, OpenAPIDocumentBuilder, SwaggerDocumentGenerator
56+
from ellar.openapi import OpenAPIDocumentModule, OpenAPIDocumentBuilder, SwaggerUI
5757
from pydantic import Field
5858
from pathlib import Path
5959

@@ -76,20 +76,20 @@ class MotoController(ControllerBase):
7676
self._service = service
7777

7878
@post()
79-
async def create(self, payload: CreateCarSerializer = Body()):
79+
async def create(self, payload: Body[CreateCarSerializer]):
8080
assert self._service.detail == 'a service'
8181
result = payload.dict()
8282
result.update(message='This action adds a new car')
8383
return result
8484

8585
@put('/{car_id:str}')
86-
async def update(self, car_id: str, payload: CreateCarSerializer = Body()):
86+
async def update(self, car_id: str, payload: Body[CreateCarSerializer]):
8787
result = payload.dict()
8888
result.update(message=f'This action updated #{car_id} car resource')
8989
return result
9090

9191
@get('/{car_id:str}')
92-
async def get_one(self, car_id: str, service: CarService = Provide()):
92+
async def get_one(self, car_id: str, service: Inject[CarService]):
9393
assert self._service == service
9494
return f"This action returns a #{car_id} car"
9595

@@ -113,7 +113,7 @@ document_builder.set_title('Ellar API') \
113113

114114
document = document_builder.build_document(app)
115115
module = OpenAPIDocumentModule.setup(
116-
document_generator=SwaggerDocumentGenerator(),
116+
docs_ui=SwaggerUI(),
117117
document=document,
118118
guards=[]
119119
)

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import uvicorn
5959
from ellar.common import Body, Controller, ControllerBase, delete, get, post, put, Serializer, Inject
6060
from ellar.core import AppFactory
6161
from ellar.di import injectable, request_scope
62-
from ellar.openapi import OpenAPIDocumentModule, OpenAPIDocumentBuilder, SwaggerDocumentGenerator
62+
from ellar.openapi import OpenAPIDocumentModule, OpenAPIDocumentBuilder, SwaggerUI
6363
from pydantic import Field
6464
from pathlib import Path
6565

@@ -119,7 +119,7 @@ document_builder.set_title('Ellar API') \
119119

120120
document = document_builder.build_document(app)
121121
module = OpenAPIDocumentModule.setup(
122-
document_generator=SwaggerDocumentGenerator(),
122+
docs_ui=SwaggerUI(),
123123
document=document,
124124
guards=[]
125125
)

docs/overview/step-one.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ In `project_name.server`, we create the `application` instance using the `AppFac
5353
# project_name/server.py
5454
import os
5555

56-
from ellar.constants import ELLAR_CONFIG_MODULE
56+
from ellar.common.constants import ELLAR_CONFIG_MODULE
5757
from ellar.core.factory import AppFactory
5858
from .root_module import ApplicationModule
5959

@@ -178,9 +178,9 @@ then add the below.
178178
# project_name/server.py
179179
180180
import os
181-
from ellar.constants import ELLAR_CONFIG_MODULE
181+
from ellar.common.constants import ELLAR_CONFIG_MODULE
182182
from ellar.core.factory import AppFactory
183-
from ellar.openapi import OpenAPIDocumentModule, OpenAPIDocumentBuilder, SwaggerDocumentGenerator
183+
from ellar.openapi import OpenAPIDocumentModule, OpenAPIDocumentBuilder, SwaggerUI
184184
from .root_module import ApplicationModule
185185
186186
application = AppFactory.create_from_app_module(
@@ -198,7 +198,7 @@ document_builder.set_title('Project Name API') \
198198
199199
document = document_builder.build_document(application)
200200
module_config = OpenAPIDocumentModule.setup(
201-
document_generator=SwaggerDocumentGenerator(),
201+
docs_ui=SwaggerUI(),
202202
document=document,
203203
guards=[]
204204
)

ellar/common/exceptions/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async def catch(
3030
if isinstance(exc.detail, (list, dict)):
3131
data = exc.detail
3232
else:
33-
data = {"detail": exc.detail, "status_code": exc.status_code} # type: ignore[assignment]
33+
data = {"detail": exc.detail, "status_code": exc.status_code}
3434

3535
return config.DEFAULT_JSON_CLASS(
3636
data, status_code=exc.status_code, headers=exc.headers

ellar/di/injector/ellar_injector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def get_module(self, module: t.Type) -> t.Optional["ModuleRefBase"]:
101101
def get_templating_modules(
102102
self,
103103
) -> t.Dict[t.Type["ModuleBase"], "ModuleTemplateRef"]:
104-
return self._modules.get(MODULE_REF_TYPES.TEMPLATE, {})
104+
return self._modules.get( # type:ignore[no-any-return]
105+
MODULE_REF_TYPES.TEMPLATE, {}
106+
)
105107

106108
def add_module(self, module_ref: t.Union["ModuleRefBase", "ModuleSetup"]) -> None:
107109
self._modules[module_ref.ref_type].update({module_ref.module: module_ref})

ellar/openapi/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from .builder import OpenAPIDocumentBuilder
22
from .decorators import ApiTags, openapi_info
3-
from .docs_generators import (
4-
IDocumentationGenerator,
5-
ReDocDocumentGenerator,
6-
SwaggerDocumentGenerator,
3+
from .docs_ui import (
4+
IDocumentationUIContext,
5+
ReDocsUI,
6+
SwaggerUI,
77
)
88
from .module import OpenAPIDocumentModule
99
from .route_doc_models import (
@@ -13,14 +13,14 @@
1313
)
1414

1515
__all__ = [
16-
"IDocumentationGenerator",
16+
"IDocumentationUIContext",
1717
"OpenAPIDocumentBuilder",
1818
"OpenAPIRoute",
1919
"OpenAPIMountDocumentation",
2020
"OpenAPIRouteDocumentation",
2121
"OpenAPIDocumentModule",
22-
"ReDocDocumentGenerator",
23-
"SwaggerDocumentGenerator",
22+
"ReDocsUI",
23+
"SwaggerUI",
2424
"openapi_info",
2525
"ApiTags",
2626
]

ellar/openapi/docs_generators/__init__.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

ellar/openapi/docs_ui/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .base import IDocumentationUIContext
2+
from .redocs import ReDocsUI
3+
from .swagger import SwaggerUI
4+
5+
__all__ = [
6+
"IDocumentationUIContext",
7+
"ReDocsUI",
8+
"SwaggerUI",
9+
]

0 commit comments

Comments
 (0)