Skip to content

Commit 26e976c

Browse files
committed
Initial Commit
0 parents  commit 26e976c

File tree

20 files changed

+424
-0
lines changed

20 files changed

+424
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/build/*
2+
/dist/*
3+
/swagger_confluence_pub.egg-info/*
4+
/venv/*
5+
**/.DS_Store
6+
**/*.pyc
7+
.vscode/*
8+
/.eggs/*
9+
/docs/build/*
10+
/test-reports/*
11+
/_build/*
12+
/_static/*
13+
/_templates/*
14+
/__pycache__/*
15+
/*.egg-info/

LICENSE.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
MIT License
2+
Copyright (c) 2018 YOUR NAME
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
The above copyright notice and this permission notice shall be included in all
10+
copies or substantial portions of the Software.
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
SOFTWARE.

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include requirements.txt

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# What this repository contains
2+
#### A FastAPI utility to allow Controller Class usage
3+
4+
## Installation:
5+
6+
install the package
7+
```
8+
pip install fastapi-router-controller
9+
```
10+
11+
## How to use
12+
13+
In a Class module
14+
15+
```python
16+
from fastapi import APIRouter
17+
from fastapi_router_controller import Controller
18+
19+
router = APIRouter()
20+
controller = Controller(router)
21+
22+
@controller.resource()
23+
class ExampleController():
24+
@controller.route.get(
25+
'/some_aoi',
26+
summary='A sample description')
27+
def sample_api(_):
28+
return 'A sample response'
29+
```
30+
31+
Load the controller to the main FastAPI app
32+
```python
33+
from fastapi import FastAPI
34+
from fastapi_router_controller import Controller
35+
36+
import ExampleController
37+
38+
app = FastAPI(
39+
title='A sample application using fastapi_router_controller',
40+
version="0.1.0")
41+
42+
example_controller = ExampleController()
43+
app.include_router(example_controller.router())
44+
```
45+
46+
## For some Example use-cases visit the example folder

example/sample_app/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fastapi import FastAPI
2+
from fastapi_router_controller import ControllersTags
3+
from controller.sample_controller import SampleController
4+
from controller.sample_controller_2 import AnotherSampleController
5+
6+
app = FastAPI(
7+
title='A sample application using fastapi_router_controller',
8+
version="0.1.0",
9+
openapi_tags=ControllersTags)
10+
11+
sample_controller = SampleController()
12+
another_sample_controller = AnotherSampleController()
13+
14+
app.include_router(sample_controller.router())
15+
app.include_router(another_sample_controller.router())
16+
17+
print(app.openapi())

example/sample_app/controller/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from fastapi import APIRouter, status, Query, Body
2+
from fastapi_router_controller import Controller
3+
from fastapi.responses import JSONResponse
4+
from pydantic import BaseModel, Field
5+
6+
router = APIRouter(prefix='/sample_controller')
7+
8+
controller = Controller(router, openapi_tag={
9+
'name': 'sample_controller',
10+
})
11+
12+
class SampleObject(BaseModel):
13+
id: str = Field(..., description='sample id')
14+
15+
# With the "resource" decorator define the controller Class linked to the Controller router arguments
16+
@controller.resource()
17+
class SampleController():
18+
@controller.route.get(
19+
'/',
20+
tags=['sample_controller'],
21+
summary='return a sample object')
22+
def sample_get_request(self, id: str = Query(..., title="itemId", description="The id of the sample object")):
23+
return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(id))
24+
25+
@controller.route.post(
26+
'/',
27+
tags=['sample_controller'],
28+
summary='create another sample object',
29+
status_code=201)
30+
def sample_post_request(self, simple_object: SampleObject = Body(None, title="SampleObject", description="A sample object model")):
31+
return JSONResponse(status_code=status.HTTP_201_CREATED, content={})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from fastapi import APIRouter, status, Query, Body
2+
from fastapi_router_controller import Controller
3+
from fastapi.responses import JSONResponse
4+
from pydantic import BaseModel, Field
5+
6+
router = APIRouter(prefix='/sample_controller_2')
7+
8+
controller = Controller(router, openapi_tag={
9+
'name': 'sample_controller_2',
10+
})
11+
12+
class SampleObject(BaseModel):
13+
id: str = Field(..., description='sample id')
14+
15+
# With the "resource" decorator define the controller Class linked to the Controller router arguments
16+
@controller.resource()
17+
class AnotherSampleController():
18+
@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")):
23+
return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(id))
24+
25+
@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")):
31+
return JSONResponse(status_code=status.HTTP_201_CREATED, content={})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from fastapi import FastAPI
2+
from fastapi_router_controller import Controller, ControllersTags
3+
4+
# just import the main package to load all the controllers in
5+
import controller
6+
7+
app = FastAPI(
8+
title='A sample application using fastapi_router_controller with controller auto import',
9+
version="0.1.0",
10+
# all of the router openapi_tags are collected in ControllerTags object
11+
openapi_tags=ControllersTags)
12+
13+
for router in Controller.routers():
14+
app.include_router(router)
15+
16+
print(app.openapi())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
from fastapi_router_controller import ControllerLoader
3+
4+
this_dir = os.path.dirname(__file__)
5+
6+
# load all the module inside the given path
7+
ControllerLoader.load(this_dir, __package__)

0 commit comments

Comments
 (0)