42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import json
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
|
|
@dataclass
|
|
class _FakeRequestState:
|
|
channel_config: dict
|
|
channel_raw_body: dict | None
|
|
|
|
|
|
class FakeRequest:
|
|
"""为 Polling / WebSocket 等传输层消息构造的伪请求对象。
|
|
|
|
提供 ``request.json()`` 与 ``request.body()`` 接口,并携带 ``state.channel_config``、
|
|
``state.channel_raw_body``,供 ``normalize_inbound`` 在异步传输消息场景中复用。
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
config: dict,
|
|
body: dict,
|
|
channel_raw_body: dict | None = None,
|
|
*,
|
|
headers: dict[str, str] | None = None,
|
|
query_params: dict[str, str] | None = None,
|
|
path_params: dict[str, str] | None = None,
|
|
) -> None:
|
|
raw = channel_raw_body if channel_raw_body is not None else body
|
|
self.state = _FakeRequestState(channel_config=config, channel_raw_body=raw)
|
|
self._json = body
|
|
self._body = json.dumps(body).encode("utf-8")
|
|
self.headers: dict[str, str] = headers or {}
|
|
self.query_params: dict[str, str] = query_params or {}
|
|
self.path_params: dict[str, str] = path_params or {}
|
|
|
|
async def json(self) -> dict[str, Any]:
|
|
return self._json
|
|
|
|
async def body(self) -> bytes:
|
|
return self._body
|