Skip to content

Commit b3c2d93

Browse files
authored
Merge pull request #21 from KiraPC/master
Dev alignment
2 parents 5985248 + c0833aa commit b3c2d93

File tree

22 files changed

+272
-144
lines changed

22 files changed

+272
-144
lines changed

.flake8

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[flake8]
2+
max-line-length = 88
3+
max-complexity = 10
4+
exclude =
5+
.git,
6+
__pycache__,
7+
docs/source/conf.py,
8+
old,
9+
build,
10+
dist
11+
src/gunicorn_conf.py
12+
src/catalog/migration.py

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Run Tests
22

3-
on: [push, pull_request]
3+
on: [ pull_request ]
44

55
jobs:
66
build:

.github/workflows/lint.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Lint
2+
on: [ pull_request ]
3+
4+
jobs:
5+
lint:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
python-version: [ 3.9 ]
10+
os: [ ubuntu-18.04 ]
11+
runs-on: ${{ matrix.os }}
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: actions/setup-python@v2
15+
with:
16+
python-version: ${{ matrix.python-version }}
17+
- name: flake8 install
18+
run: pip3 install flake8
19+
- name: lint
20+
run: flake8 fastapi_router_controller/

.pre-commit-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 21.5b1
4+
hooks:
5+
- id: black
6+
args: [
7+
"fastapi_router_controller"
8+
]
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v2.0.0
11+
hooks:
12+
- id: flake8

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ExampleController:
6161
self.x = x
6262

6363
@controller.route.get(
64-
"/some_aoi", summary="A sample description", response_model=Foo
64+
"/some_api", summary="A sample description", response_model=Foo
6565
)
6666
def sample_api(self):
6767
print(self.x.bar) # -> amazing_variable

example/sample_app/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from fastapi import FastAPI
2-
from fastapi.testclient import TestClient
32
from fastapi_router_controller import ControllersTags
43
from controller.sample_controller import SampleController
54
from controller.sample_controller_2 import AnotherSampleController
65

76
app = FastAPI(
8-
title='A sample application using fastapi_router_controller',
7+
title="A sample application using fastapi_router_controller",
98
version="0.1.0",
10-
openapi_tags=ControllersTags)
9+
openapi_tags=ControllersTags,
10+
)
1111

1212
app.include_router(SampleController.router())
1313
app.include_router(AnotherSampleController.router())

example/sample_app/controller/sample_controller.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,61 @@
33
from fastapi.responses import JSONResponse
44
from pydantic import BaseModel, Field
55

6-
router = APIRouter(prefix='/sample_controller')
6+
router = APIRouter(prefix="/sample_controller")
7+
8+
controller = Controller(
9+
router,
10+
openapi_tag={
11+
"name": "sample_controller",
12+
},
13+
)
714

8-
controller = Controller(router, openapi_tag={
9-
'name': 'sample_controller',
10-
})
1115

1216
class SampleObject(BaseModel):
13-
id: str = Field(..., description='sample id')
17+
id: str = Field(..., description="sample id")
1418

1519
def to_json(self):
16-
return {'id': self.id}
20+
return {"id": self.id}
21+
1722

18-
class Foo():
23+
class Foo:
1924
def create_foo(_, item):
20-
print('Created Foo', str(item))
25+
print("Created Foo", str(item))
26+
2127

2228
def get_foo():
2329
return Foo()
2430

25-
# With the "resource" decorator define the controller Class linked to the Controller router arguments
31+
32+
# With the "resource" decorator define the controller Class
33+
# linked to the Controller router arguments
2634
@controller.resource()
27-
class SampleController():
35+
class SampleController:
2836
def __init__(self, foo: Foo = Depends(get_foo)):
2937
self.foo = foo
3038

3139
@controller.route.get(
32-
'/',
33-
tags=['sample_controller'],
34-
summary='return a sample object')
35-
def sample_get_request(self, id: str = Query(..., title="itemId", description="The id of the sample object")):
36-
return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(**{'id': id}).to_json())
40+
"/", tags=["sample_controller"], summary="return a sample object"
41+
)
42+
def sample_get_request(
43+
self,
44+
id: str = Query(..., title="itemId", description="The id of the sample object"),
45+
):
46+
return JSONResponse(
47+
status_code=status.HTTP_200_OK, content=SampleObject(**{"id": id}).to_json()
48+
)
3749

3850
@controller.route.post(
39-
'/',
40-
tags=['sample_controller'],
41-
summary='create another sample object',
42-
status_code=201)
43-
def sample_post_request(self, simple_object: SampleObject = Body({}, title="SampleObject", description="A sample object model")):
51+
"/",
52+
tags=["sample_controller"],
53+
summary="create another sample object",
54+
status_code=201,
55+
)
56+
def sample_post_request(
57+
self,
58+
simple_object: SampleObject = Body(
59+
{}, title="SampleObject", description="A sample object model"
60+
),
61+
):
4462
self.foo.create_foo(simple_object)
4563
return JSONResponse(status_code=status.HTTP_201_CREATED, content={})

example/sample_app/controller/sample_controller_2.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,43 @@
33
from fastapi.responses import JSONResponse
44
from pydantic import BaseModel, Field
55

6-
router = APIRouter(prefix='/sample_controller_2')
6+
router = APIRouter(prefix="/sample_controller_2")
7+
8+
controller = Controller(
9+
router,
10+
openapi_tag={
11+
"name": "sample_controller_2",
12+
},
13+
)
714

8-
controller = Controller(router, openapi_tag={
9-
'name': 'sample_controller_2',
10-
})
1115

1216
class SampleObject(BaseModel):
13-
id: str = Field(..., description='sample id')
17+
id: str = Field(..., description="sample id")
18+
1419

15-
# With the "resource" decorator define the controller Class linked to the Controller router arguments
20+
# With the "resource" decorator define the controller Class
21+
# linked to the Controller router arguments
1622
@controller.resource()
17-
class AnotherSampleController():
23+
class AnotherSampleController:
1824
@controller.route.get(
19-
'/',
20-
tags=['sample_controller_2'],
21-
summary='return another sample object')
22-
def sample_get_request(_, id: str = Query(..., title="itemId", description="The id of the sample object")):
25+
"/", tags=["sample_controller_2"], summary="return another sample object"
26+
)
27+
def sample_get_request(
28+
_,
29+
id: str = Query(..., title="itemId", description="The id of the sample object"),
30+
):
2331
return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(id))
2432

2533
@controller.route.post(
26-
'/',
27-
tags=['sample_controller_2'],
28-
summary='create a sample object',
29-
status_code=201)
30-
def sample_post_request(_, simple_object: SampleObject = Body(None, title="SampleObject", description="A sample object model")):
34+
"/",
35+
tags=["sample_controller_2"],
36+
summary="create a sample object",
37+
status_code=201,
38+
)
39+
def sample_post_request(
40+
_,
41+
simple_object: SampleObject = Body(
42+
None, title="SampleObject", description="A sample object model"
43+
),
44+
):
3145
return JSONResponse(status_code=status.HTTP_201_CREATED, content={})

example/sample_app_with_auto_import/app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
from fastapi_router_controller import Controller, ControllersTags
33

44
# just import the main package to load all the controllers in
5-
import controller
5+
import controller # noqa
66

77
app = FastAPI(
8-
title='A sample application using fastapi_router_controller with controller auto import',
8+
title="A sample application using fastapi_router_controller"
9+
+ "with controller auto import",
910
version="0.1.0",
1011
# all of the router openapi_tags are collected in ControllerTags object
11-
openapi_tags=ControllersTags)
12+
openapi_tags=ControllersTags,
13+
)
1214

1315
for router in Controller.routers():
1416
app.include_router(router)

example/sample_app_with_auto_import/controller/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
this_dir = os.path.dirname(__file__)
55

66
# load all the module inside the given path
7-
ControllerLoader.load(this_dir, __package__)
7+
ControllerLoader.load(this_dir, __package__)

0 commit comments

Comments
 (0)