MCPcopy Create free account
hub / github.com/OpenRouterTeam/python-sdk / serialize_request_body

Function serialize_request_body

src/openrouter/utils/requestbodies.py:32–67  ·  view source on GitHub ↗
(
    request_body: Any,
    nullable: bool,
    optional: bool,
    serialization_method: str,
    request_body_type,
)

Source from the content-addressed store, hash-verified

30
31
32def serialize_request_body(
33 request_body: Any,
34 nullable: bool,
35 optional: bool,
36 serialization_method: str,
37 request_body_type,
38) -> Optional[SerializedRequestBody]:
39 if request_body is None:
40 if not nullable and optional:
41 return None
42
43 media_type = SERIALIZATION_METHOD_TO_CONTENT_TYPE[serialization_method]
44
45 serialized_request_body = SerializedRequestBody(media_type)
46
47 if re.match(r"^(application|text)\/([^+]+\+)*json.*", media_type) is not None:
48 serialized_request_body.content = marshal_json(request_body, request_body_type)
49
50 elif re.match(r"^multipart\/.*", media_type) is not None:
51 (
52 serialized_request_body.media_type,
53 serialized_request_body.data,
54 serialized_request_body.files,
55 ) = serialize_multipart_form(media_type, request_body)
56 elif re.match(r"^application\/x-www-form-urlencoded.*", media_type) is not None:
57 serialized_request_body.data = serialize_form_data(request_body)
58 elif isinstance(request_body, (bytes, bytearray, io.BytesIO, io.BufferedReader)):
59 serialized_request_body.content = request_body
60 elif isinstance(request_body, str):
61 serialized_request_body.content = request_body
62 else:
63 raise TypeError(
64 f"invalid request body type {type(request_body)} for mediaType {media_type}"
65 )
66
67 return serialized_request_body

Callers

nothing calls this directly

Calls 4

marshal_jsonFunction · 0.85
serialize_multipart_formFunction · 0.85
serialize_form_dataFunction · 0.85

Tested by

no test coverage detected