• FastAPI CRUD Router


    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.

    RouteMethodDescription
    / 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.

    ArgumentDefaultDescription
    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), ...],
    )
  • 相关阅读:
    WinCE 与通讯模块
    6174问题
    阶乘因式分解(一)
    三个数从小到大排序
    公约数和公倍数
    水仙花数
    韩信点兵
    5个数求最值
    求转置矩阵问题
    孪生素数问题
  • 原文地址:https://www.cnblogs.com/lightsong/p/16852304.html
Copyright © 2020-2023  润新知