🎉 init starlette dial
This commit is contained in:
38
handler/error.py
Normal file
38
handler/error.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from starlette.requests import Request
|
||||
|
||||
from pkg.dial import client
|
||||
from pkg.resp.exception import (
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
ServerErrorException,
|
||||
UnauthorizationException,
|
||||
)
|
||||
|
||||
|
||||
async def handle_400(request: Request):
|
||||
raise BadRequestException(
|
||||
msg="参数错误", data={"name": "张三"}, err={"code": 400, "msg": "参数错误"}
|
||||
)
|
||||
|
||||
|
||||
async def handle_500(request: Request):
|
||||
raise ServerErrorException(
|
||||
msg="服务器内部错误", data=None, err={"code": 500, "msg": "服务器内部错误"}
|
||||
)
|
||||
|
||||
|
||||
async def handle_401(request: Request):
|
||||
raise UnauthorizationException(
|
||||
msg="未授权", data=None, err={"code": 401, "msg": "未授权"}
|
||||
)
|
||||
|
||||
|
||||
async def handle_403(request: Request):
|
||||
raise ForbiddenException(
|
||||
msg="禁止访问", data=None, err={"code": 403, "msg": "禁止访问"}
|
||||
)
|
||||
|
||||
|
||||
async def handle_dail_401(request: Request):
|
||||
res = await client.dial("http://127.0.0.1:3000/api/v3/auth/401", "GET")
|
||||
return res
|
||||
5
handler/home.py
Normal file
5
handler/home.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import HTMLResponse
|
||||
|
||||
async def handle_home(request: Request):
|
||||
return HTMLResponse('<h1>Hello World</h1>')
|
||||
22
handler/other.py
Normal file
22
handler/other.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
from pkg.dial import client
|
||||
|
||||
|
||||
async def handle_login(request: Request):
|
||||
response = await client.dial(
|
||||
"http://127.0.0.1:3000/api/v3/auth/login",
|
||||
"POST",
|
||||
body={"username": "admin", "password": "123456"},
|
||||
)
|
||||
|
||||
data = response.json()
|
||||
|
||||
print(f"[D] data = {data}")
|
||||
|
||||
return JSONResponse(data)
|
||||
|
||||
|
||||
async def handle_auth_check(request: Request):
|
||||
pass
|
||||
Reference in New Issue
Block a user