FastAPI CRUD Router

https://github.com/awtkns/fastapi-crudrouter

fastapi提供基础的制作API能力

对于简单的业务来说,对于表仅仅需要 CRUD 接口,不需要其他额外的数据逻辑,

对于这种情况,我们希望能够快速提供对指定表格的 CRUD 能力。

此库应运而生。

 

懒人专享

快速制作业务原型

Tired of rewriting generic CRUD routes? Need to rapidly prototype a feature for a presentation or a hackathon? Thankfully, fastapi-crudrouter has your back. As an extension to the APIRouter included with FastAPI, the FastAPI CRUDRouter will automatically generate and document your CRUD routes for you, all you have to do is pass your model and maybe your database connection.

FastAPI-CRUDRouter is lighting fast, well tested, and production ready.

 

Basic Code

非常神奇便利

只需要提供Pydantic schema生成一个CRUD router即可。

 

Below is a simple example of what the CRUDRouter can do. In just ten lines of code, you can generate all the crud routes you need for any model. A full list of the routes generated can be found here.

from pydantic import BaseModel
from fastapi import FastAPI
from fastapi_crudrouter import MemoryCRUDRouter as CRUDRouter

class Potato(BaseModel):
    id: int
    color: str
    mass: float

app = FastAPI()
app.include_router(CRUDRouter(schema=Potato))

 

默认生成的API

https://fastapi-crudrouter.awtkns.com/routing

 增删改查面面俱到。

Default Routes

By default, the CRUDRouter will generate the six routes below for you.

Route Method Description
/ GET Get all the resources
/ POST Create a new resource
/ DELETE Delete all the resources
/{item_id} GET Get an existing resource matching the given item_id
/{item_id} PUT Update an existing resource matching the given item_id
/{item_id} DELETE Delete an existing resource matching the given item_id

 

选择默认支持的API

如果有的接口不需要,可以关闭。

Disabling Routes

Routes can be disabled from generating with a key word argument (kwarg) when creating your CRUDRouter. The valid kwargs are shown below.

Argument Default Description
get_all_route True Setting this to false will prevent the get all route from generating
get_one_route True Setting this to false will prevent the get one route from generating
delete_all_route True Setting this to false will prevent the delete all route from generating
delete_one_route True Setting this to false will prevent the delete one route from generating
create_route True Setting this to false will prevent the create route from generating
update_route True Setting this to false will prevent the update route from generating

 

router = MemoryCRUDRouter(schema=MyModel, delete_all_route=False)

 

定制接口

如果有的接口不满足要求,例如需要在创建条目后, 触发后台业务,

可以重载接口,定制业务逻辑。

 

Below is an example where we are overriding the routes /potato/{item_id} and /potato while using the MemoryCRUDRouter.

 

from pydantic import BaseModel
from fastapi import FastAPI
from fastapi_crudrouter import MemoryCRUDRouter as CRUDRouter

class Potato(BaseModel):
    id: int
    color: str
    mass: float

app = FastAPI()
router = CRUDRouter(schema=Potato)

@router.get('')
def overloaded_get_all():
    return 'My overloaded route that returns all the items'

@router.get('/{item_id}')
def overloaded_get_one():
    return 'My overloaded route that returns one item'

app.include_router(router)

 

Dependencies

https://fastapi-crudrouter.awtkns.com/dependencies

接口需要鉴权,则定义依赖可以实现。

 

All the CRUDRouters included with fastapi_crudrouter support FastAPI dependency injection.

 

from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from fastapi_crudrouter import MemoryCRUDRouter

app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")

def token_auth(token: str=Depends(oauth2_scheme)):
    if not token:
        raise HTTPException(401, "Invalid token")

router = MemoryCRUDRouter(schema=MySchema, dependencies=[Depends(token_auth)])
app.include_router(router)

 

Custom Dependencies Per Route

也可以对具体接口定义依赖。

CRUDRouter(
    # ...
    get_all_route=[Depends(get_all_route_dep), ...],
    get_one_route=[Depends(get_one_route_dep), ...],
    create_route=[Depends(create_route_dep), ...],
    update_route=[Depends(update_route_dep), ...],
    delete_one_route=[Depends(user), ...],
    delete_all_route=[Depends(user), ...],
)

 

原文地址:http://www.cnblogs.com/lightsong/p/16852304.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性