From 164326ce76831ea2e332a3de4c02b857ba664e15 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 7 May 2026 08:35:44 +0800 Subject: [PATCH] docs: add comprehensive documentation - Add docstrings to models and main.py - Create API.md with detailed endpoint documentation - Create CHANGELOG.md for version tracking - Enhance FastAPI app metadata (description, version, tags) Co-Authored-By: Claude Opus 4.7 --- CHANGELOG.md | 26 ++++++++++ app/main.py | 29 +++++++++-- app/models.py | 6 +++ docs/API.md | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 5 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 docs/API.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cc42c9d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,26 @@ +# 更新日志 + +本项目遵循[语义化版本](https://semver.org/lang/zh-CN/)。 + +## [1.0.0] - 2026-05-07 + +### 新增 +- 初始版本发布 +- 健康检查端点 `GET /health` +- 项目 CRUD 端点 + - `GET /items` 列出所有项目 + - `POST /items` 创建新项目 + - `GET /items/{id}` 获取单个项目 +- 完整测试套件(5 个测试用例) +- Swagger UI 和 ReDoc 文档支持 + +### 文档 +- README.md 项目说明 +- API 设计文档 +- 实现计划文档 + +### 技术栈 +- FastAPI 0.100.0+ +- Pydantic 数据验证 +- pytest 测试框架 +- Uvicorn ASGI 服务器 diff --git a/app/main.py b/app/main.py index 686b2ed..8d8111a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,21 +1,31 @@ +"""FastAPI 应用入口。 + +提供健康检查和项目 CRUD 端点。 +""" from fastapi import FastAPI, HTTPException from typing import Dict from app.models import Item, ItemCreate -app = FastAPI(title="FastAPI Example") +app = FastAPI( + title="FastAPI Example", + description="最小化 CRUD API 示例", + version="1.0.0" +) # 内存存储 _items: Dict[int, Item] = {} _item_id_counter = 0 -@app.get("/health") +@app.get("/health", tags=["系统"]) async def health_check(): + """健康检查端点,返回服务状态。""" return {"status": "ok"} -@app.post("/items", response_model=Item) +@app.post("/items", response_model=Item, tags=["项目"]) async def create_item(item: ItemCreate): + """创建新项目。""" global _item_id_counter _item_id_counter += 1 new_item = Item(id=_item_id_counter, **item.model_dump()) @@ -23,13 +33,22 @@ async def create_item(item: ItemCreate): return new_item -@app.get("/items", response_model=list[Item]) +@app.get("/items", response_model=list[Item], tags=["项目"]) async def list_items(): + """列出所有项目。""" return list(_items.values()) -@app.get("/items/{item_id}", response_model=Item) +@app.get("/items/{item_id}", response_model=Item, tags=["项目"]) async def get_item(item_id: int): + """获取单个项目。 + + Args: + item_id: 项目 ID + + Raises: + HTTPException: 项目不存在时返回 404 + """ if item_id not in _items: raise HTTPException(status_code=404, detail="Item not found") return _items[item_id] \ No newline at end of file diff --git a/app/models.py b/app/models.py index d86d21f..8576798 100644 --- a/app/models.py +++ b/app/models.py @@ -1,13 +1,19 @@ +"""数据模型定义。 + +定义 API 请求和响应使用的 Pydantic 模型。 +""" from pydantic import BaseModel from typing import Optional class Item(BaseModel): + """项目完整数据模型,包含 ID。""" id: Optional[int] = None name: str description: Optional[str] = None class ItemCreate(BaseModel): + """创建项目的请求模型,不包含 ID。""" name: str description: Optional[str] = None \ No newline at end of file diff --git a/docs/API.md b/docs/API.md new file mode 100644 index 0000000..7a584fa --- /dev/null +++ b/docs/API.md @@ -0,0 +1,132 @@ +# API 设计文档 + +版本:1.0.0 +更新日期:2026-05-07 + +## 概述 + +本 API 提供简单的 CRUD 操作,用于管理项目(Item)。 + +## 基础信息 + +- 基础 URL: `http://localhost:8000` +- 内容类型: `application/json` +- 认证: 无 + +## 端点 + +### 健康检查 + +``` +GET /health +``` + +**响应示例:** +```json +{ + "status": "ok" +} +``` + +--- + +### 创建项目 + +``` +POST /items +``` + +**请求体:** +```json +{ + "name": "string (必填)", + "description": "string (可选)" +} +``` + +**响应示例:** +```json +{ + "id": 1, + "name": "My Item", + "description": "A test item" +} +``` + +**状态码:** +- 200: 创建成功 + +--- + +### 列出项目 + +``` +GET /items +``` + +**响应示例:** +```json +[ + { + "id": 1, + "name": "My Item", + "description": "A test item" + } +] +``` + +**状态码:** +- 200: 成功 + +--- + +### 获取单个项目 + +``` +GET /items/{item_id} +``` + +**路径参数:** +- `item_id`: 项目 ID (integer) + +**响应示例:** +```json +{ + "id": 1, + "name": "My Item", + "description": "A test item" +} +``` + +**状态码:** +- 200: 成功 +- 404: 项目不存在 + +--- + +## 错误响应 + +```json +{ + "detail": "错误描述" +} +``` + +## 数据模型 + +### Item +```json +{ + "id": "integer", + "name": "string", + "description": "string | null" +} +``` + +### ItemCreate +```json +{ + "name": "string", + "description": "string | null" +} +```