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.1.tar.gz (13.3 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.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_rest_toolkit-0.0.1.tar.gz
  • Upload date:
  • Size: 13.3 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.1.tar.gz
Algorithm Hash digest
SHA256 7c122dbce1de5175edb32866d877f68a35b5e8ae8a8ec87411a1bbe81557b366
MD5 5286fc150e328ca14fabf2ce28b81c20
BLAKE2b-256 73e8e183261b4ea17df9cb12860ab5b173081d437fd27b024dae406696d3531c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_rest_toolkit-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 008a7ccdc965981237304aae76ea112f85e70b9cd301c566e975e91e30657e2f
MD5 472673205be60cf644d1ea60b58f1208
BLAKE2b-256 874cc48b8c0f87670f5767f9d031d23eb743c1a8266600d28cc6ff0b8a96948c

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