feat(chat): support public channel chat
This commit is contained in:
39
app/dependencies/param.py
Normal file
39
app/dependencies/param.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
|
||||
def BodyOrForm[T: BaseModel](model: type[T]):
|
||||
async def dependency(
|
||||
request: Request,
|
||||
) -> T:
|
||||
content_type = request.headers.get("content-type", "")
|
||||
|
||||
data: dict[str, Any] = {}
|
||||
if "application/json" in content_type:
|
||||
try:
|
||||
data = await request.json()
|
||||
except Exception:
|
||||
raise RequestValidationError(
|
||||
[
|
||||
{
|
||||
"loc": ("body",),
|
||||
"msg": "Invalid JSON body",
|
||||
"type": "value_error",
|
||||
}
|
||||
]
|
||||
)
|
||||
else:
|
||||
form = await request.form()
|
||||
data = dict(form)
|
||||
|
||||
try:
|
||||
return model(**data)
|
||||
except ValidationError as e:
|
||||
raise RequestValidationError(e.errors())
|
||||
|
||||
return dependency
|
||||
Reference in New Issue
Block a user