11from dataclasses import dataclass
22import json
3- from typing import Any , Literal
3+ from typing import Any , TypeAlias , TypedDict
44
55from nonebot .compat import PYDANTIC_V2
6+ from nonebot .internal .driver import FileTypes
67from pydantic import BaseModel
78
89if PYDANTIC_V2 :
1617if PYDANTIC_V2 :
1718 _JSON_ADAPTER = TypeAdapter (Any )
1819
20+ MultipartFormData : TypeAlias = dict [str , FileTypes ]
21+
22+
23+ class JsonTransportRequest (TypedDict ):
24+ json : dict [str , Any ]
25+
26+
27+ class MultipartTransportRequest (TypedDict ):
28+ files : MultipartFormData
29+
30+
31+ EncodedPreparedRequest : TypeAlias = JsonTransportRequest | MultipartTransportRequest
32+
1933
2034@dataclass (frozen = True , slots = True )
2135class PreparedRequest :
@@ -61,7 +75,11 @@ def prepare_request( # noqa: PLR0913
6175def encode_json_text (value : object ) -> str :
6276 if PYDANTIC_V2 :
6377 return _JSON_ADAPTER .dump_json (value ).decode ()
64- return json .dumps (value , default = pydantic_encoder )
78+ return json .dumps (
79+ value ,
80+ default = pydantic_encoder ,
81+ separators = ("," , ":" ),
82+ )
6583
6684
6785def encode_model_json_text ( # noqa: PLR0913
@@ -133,8 +151,8 @@ def _build_multipart_payload(
133151 files : list [File ],
134152 * ,
135153 attachment_owner_path : tuple [str , ...] = (),
136- ) -> dict [ str , Any ] :
137- multipart : dict [ str , Any ] = {}
154+ ) -> MultipartFormData :
155+ multipart : MultipartFormData = {}
138156 container = _resolve_attachment_owner (payload , attachment_owner_path )
139157 has_attachments = "attachments" in container
140158 attachments = container .get ("attachments" , [])
@@ -151,7 +169,11 @@ def _build_multipart_payload(
151169
152170 if isinstance (attachments , list ) and has_attachments :
153171 container ["attachments" ] = attachments
154- multipart ["payload_json" ] = (None , encode_json_text (payload ), "application/json" )
172+ multipart ["payload_json" ] = (
173+ None ,
174+ encode_json_text (payload ).encode (),
175+ "application/json" ,
176+ )
155177 return multipart
156178
157179
0 commit comments