构建fastapi框架

This commit is contained in:
wsb1224 2025-10-15 17:55:23 +08:00
parent f95e085e7b
commit a50e0a088b
2 changed files with 70 additions and 2 deletions

View File

@ -7,6 +7,13 @@
3. 上传永久图片到微信公众号素材库
4. 上传文章草稿到微信公众号
## 功能特点
- 基于FastAPI框架提供高性能的异步API服务
- 自动生成交互式API文档Swagger UI和ReDoc
- 完整的错误处理和参数验证
- 易于部署和扩展
## 安装依赖
```bash
@ -39,6 +46,14 @@ python main.py
- `appsecret` (string): 公众号的AppSecret
- **返回**: access_token
**示例请求**:
```json
{
"appid": "your_appid",
"appsecret": "your_appsecret"
}
```
### 2. 上传临时图片
- **URL**: `/upload_temp_image`
@ -48,6 +63,14 @@ python main.py
- `image_url` (string): 图片的URL地址
- **返回**: media_id
**示例请求**:
```json
{
"access_token": "your_access_token",
"image_url": "https://example.com/image.jpg"
}
```
### 3. 上传永久图片
- **URL**: `/upload_permanent_image`
@ -57,6 +80,14 @@ python main.py
- `image_url` (string): 图片的URL地址
- **返回**: 永久素材的media_id
**示例请求**:
```json
{
"access_token": "your_access_token",
"image_url": "https://example.com/image.jpg"
}
```
### 4. 上传文章草稿
- **URL**: `/upload_draft`
@ -69,4 +100,41 @@ python main.py
- `author` (string): 作者
- `digest` (string, optional): 文章摘要
- `show_cover_pic` (int, optional): 是否显示封面0不显示1显示默认为1
- **返回**: 草稿的media_id
- **返回**: 草稿的media_id
**示例请求**:
```json
{
"access_token": "your_access_token",
"title": "文章标题",
"content": "<p>文章内容</p>",
"cover_media_id": "media_id",
"author": "作者名",
"digest": "文章摘要",
"show_cover_pic": 1
}
```
## 测试API
项目包含一个测试脚本 `test_api.py`可以用来测试各个API接口的功能。使用前请先修改脚本中的参数为实际值。
```bash
python test_api.py
```
## 部署建议
在生产环境中,建议使用以下方式部署:
1. 使用Gunicorn或Uvicorn作为生产级服务器
2. 配置反向代理如Nginx
3. 使用环境变量管理敏感配置
4. 添加日志记录和监控
## 注意事项
1. 请妥善保管公众号的APPID和AppSecret不要泄露
2. 上传的图片需要符合微信公众号的规范要求
3. 永久素材有数量限制,请合理使用
4. 草稿内容需要符合微信公众号的内容规范

View File

@ -110,4 +110,4 @@ async def upload_draft(request: DraftUploadRequest):
return DraftResponse(success=False, error=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
uvicorn.run(app, host="0.0.0.0", port=5050)