Skip to main content

类DRF风格的FastAPI工具包

Project description

FastAPI REST Toolkit

类 Django REST Framework 风格的 FastAPI 工具包,提供简洁优雅的方式来构建 RESTful API。

特性

  • ViewSet: 类似 DRF 的 ViewSet,支持 CRUD 操作
  • Router: 自动路由注册,简化路由配置
  • 权限系统: 灵活的权限控制(AllowAny、IsAuthenticated、IsAdmin)
  • 过滤器: 支持搜索、排序、CRUD Plus 过滤
  • 节流: 内置限流机制,支持 Redis 存储
  • 分页: LimitOffset 分页支持

安装

pip install fastapi-rest-toolkit

如需使用 Redis 节流功能:

pip install fastapi-rest-toolkit[redis]

或安装所有可选依赖:

pip install fastapi-rest-toolkit[all]

快速开始

基本使用

from fastapi import FastAPI
from fastapi_rest_toolkit import DefaultRouter, ViewSet, CRUDService
from pydantic import BaseModel

app = FastAPI()
router = DefaultRouter()

# 定义模型
class User(BaseModel):
    id: int
    name: str
    email: str

# 定义服务
class UserService(CRUDService):
    pass

# 定义 ViewSet
class UserViewSet(ViewSet):
    service_class = UserService

# 注册路由
router.register("/users", UserViewSet)
app.include_router(router)

权限控制

from fastapi_rest_toolkit import ViewSet, IsAuthenticated

class ProtectedViewSet(ViewSet):
    permission_classes = [IsAuthenticated]

过滤和搜索

from fastapi_rest_toolkit import (
    ViewSet,
    SearchFilterBackend,
    OrderingFilterBackend,
)

class UserViewSet(ViewSet):
    filter_backends = [
        SearchFilterBackend,
        OrderingFilterBackend,
    ]
    search_fields = ["name", "email"]
    ordering_fields = ["id", "name", "created_at"]

节流

from fastapi_rest_toolkit import ViewSet, AnonRateThrottle

class UserViewSet(ViewSet):
    throttle_classes = [AnonRateThrottle]
    throttle_rate = "100/hour"

组件

ViewSet

提供标准的 CRUD 操作接口:

  • list() - 获取列表
  • retrieve() - 获取单个对象
  • create() - 创建对象
  • update() - 更新对象
  • destroy() - 删除对象

权限类

  • AllowAny - 允许所有访问
  • IsAuthenticated - 需要认证
  • IsAdmin - 需要管理员权限
  • BasePermission - 自定义权限基类

过滤器

  • SearchFilterBackend - 搜索过滤
  • OrderingFilterBackend - 排序
  • CRUDPlusFilterBackend - CRUD Plus 过滤

节流类

  • SimpleRateThrottle - 简单限流
  • AnonRateThrottle - 匿名用户限流
  • AsyncRedisSimpleRateThrottle - 基于 Redis 的异步限流

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_rest_toolkit-0.0.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_rest_toolkit-0.0.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_rest_toolkit-0.0.2.tar.gz.

File metadata

  • Download URL: fastapi_rest_toolkit-0.0.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for fastapi_rest_toolkit-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c942ba4f95ed32614e6f78bc07ad80249029db062d22c25f418a3f071e36028a
MD5 954ac155b8cada5a0b74675405e76b21
BLAKE2b-256 c73cefb2d04aa31f27cf92e02de14e1522654751369d857dd0a7564083b69404

See more details on using hashes here.

File details

Details for the file fastapi_rest_toolkit-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_rest_toolkit-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eecd7c96883258c70561796df85ce863344c9469ce7c6d2bc0e34e099b6874dd
MD5 b0e682e385007fa0e8ffd702647fc69a
BLAKE2b-256 6e09a89c4296358ba778c0e3d789b3798eb9ca21647d80f64f58b8102b298070

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page