chore: 更新模块结构
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
from starlette.requests import Request
|
||||
|
||||
from pkg.dial import client
|
||||
from pkg.resp.exception import (
|
||||
from pkg.dial.exception import (
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
ServerErrorException,
|
||||
|
2
main.py
2
main.py
@ -14,7 +14,7 @@ from handler.home import handle_home
|
||||
from handler.other import handle_auth_check, handle_login
|
||||
from pkg.dial import client
|
||||
from pkg.dial.req import ModifiableRequest
|
||||
from pkg.resp.exception import (
|
||||
from pkg.dial.exception import (
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
|
@ -9,6 +9,7 @@
|
||||
- 🔀 **代理功能**: 可以创建代理处理器,支持请求和响应重写
|
||||
- ⚡ **异步支持**: 完全异步实现,与 Starlette 框架完美集成
|
||||
- 🛡️ **类型安全**: 完整的类型注解支持
|
||||
- 🚨 **异常处理**: 内置 HTTP 状态码异常类,自动处理错误响应
|
||||
|
||||
## 安装依赖
|
||||
|
||||
@ -47,7 +48,22 @@ response = await client.dial(
|
||||
)
|
||||
```
|
||||
|
||||
### 2. 自定义客户端
|
||||
### 2. 异常处理
|
||||
|
||||
```python
|
||||
from pkg.dial import client, BadRequestException, ServerErrorException
|
||||
|
||||
try:
|
||||
response = await client.dial("https://api.example.com/users")
|
||||
except BadRequestException as e:
|
||||
print(f"请求参数错误: {e.msg}")
|
||||
print(f"错误详情: {e.err}")
|
||||
print(f"返回数据: {e.data}")
|
||||
except ServerErrorException as e:
|
||||
print(f"服务器错误: {e.msg}")
|
||||
```
|
||||
|
||||
### 3. 自定义客户端
|
||||
|
||||
```python
|
||||
from pkg.dial import Dial
|
||||
@ -65,7 +81,7 @@ async with dial as client:
|
||||
print(response.text)
|
||||
```
|
||||
|
||||
### 3. 代理功能
|
||||
### 4. 代理功能
|
||||
|
||||
```python
|
||||
from pkg.dial import client
|
||||
@ -131,7 +147,7 @@ async def dial(
|
||||
) -> httpx.Response
|
||||
```
|
||||
|
||||
直接发送 HTTP 请求。
|
||||
直接发送 HTTP 请求。当响应状态码 >= 300 时,会自动抛出相应的异常。
|
||||
|
||||
##### `proxy()`
|
||||
|
||||
@ -156,6 +172,94 @@ async def close()
|
||||
|
||||
关闭客户端连接。
|
||||
|
||||
### 异常类
|
||||
|
||||
#### BadRequestException
|
||||
|
||||
400 错误异常,用于处理请求参数错误。
|
||||
|
||||
```python
|
||||
class BadRequestException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 400
|
||||
self.msg = msg or "参数错误"
|
||||
self.data = data
|
||||
self.err = err
|
||||
```
|
||||
|
||||
#### UnauthorizationException
|
||||
|
||||
401 错误异常,用于处理认证失败。
|
||||
|
||||
```python
|
||||
class UnauthorizationException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 401
|
||||
self.msg = msg or "登录信息不存在或已过期, 请重新登录"
|
||||
self.data = data
|
||||
self.err = err
|
||||
```
|
||||
|
||||
#### ForbiddenException
|
||||
|
||||
403 错误异常,用于处理权限不足。
|
||||
|
||||
```python
|
||||
class ForbiddenException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 403
|
||||
self.msg = msg or "权限不足"
|
||||
self.data = data
|
||||
self.err = err
|
||||
```
|
||||
|
||||
#### NotFoundException
|
||||
|
||||
404 错误异常,用于处理资源不存在。
|
||||
|
||||
```python
|
||||
class NotFoundException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 404
|
||||
self.msg = msg or "资源不存在"
|
||||
self.data = data
|
||||
self.err = err
|
||||
```
|
||||
|
||||
#### ServerErrorException
|
||||
|
||||
500 错误异常,用于处理服务器内部错误。
|
||||
|
||||
```python
|
||||
class ServerErrorException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 500
|
||||
self.msg = msg or "服务器开小差了"
|
||||
self.data = data
|
||||
self.err = err
|
||||
```
|
||||
|
||||
### 异常处理器
|
||||
|
||||
为了方便在 Starlette 应用中使用,还提供了对应的异常处理器:
|
||||
|
||||
```python
|
||||
from pkg.dial.exception import (
|
||||
bad_request_exception_handler,
|
||||
unauthorization_exception_handler,
|
||||
forbidden_exception_handler,
|
||||
not_found_exception_handler,
|
||||
server_error_exception_handler,
|
||||
)
|
||||
|
||||
# 在 Starlette 应用中注册异常处理器
|
||||
app.add_exception_handler(BadRequestException, bad_request_exception_handler)
|
||||
app.add_exception_handler(UnauthorizationException, unauthorization_exception_handler)
|
||||
app.add_exception_handler(ForbiddenException, forbidden_exception_handler)
|
||||
app.add_exception_handler(NotFoundException, not_found_exception_handler)
|
||||
app.add_exception_handler(ServerErrorException, server_error_exception_handler)
|
||||
```
|
||||
|
||||
## 示例
|
||||
|
||||
查看 `examples/dial_example.py` 文件获取完整的使用示例。
|
||||
|
@ -1,3 +1,10 @@
|
||||
from pkg.dial.dial import Dial
|
||||
from pkg.dial.exception import (
|
||||
BadRequestException,
|
||||
ServerErrorException,
|
||||
UnauthorizationException,
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
)
|
||||
|
||||
client = Dial()
|
||||
|
@ -7,7 +7,7 @@ from starlette.responses import StreamingResponse
|
||||
import json
|
||||
|
||||
from pkg.dial.req import ModifiableRequest
|
||||
from pkg.resp.exception import (
|
||||
from pkg.dial.exception import (
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
|
Reference in New Issue
Block a user