mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-23 22:14:18 +02:00
Consolidate to single httpx.Request construction point
Eliminate early returns by using variables for files/data kwargs.
All httpx body kwargs accept None, so we set exactly one and
pass all to a single Request() call.
🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9af9827c9f
commit
400932d3ac
1 changed files with 9 additions and 19 deletions
|
|
@ -85,39 +85,27 @@ class RequestDirector:
|
|||
{k: str(v) for k, v in cookie_params.items()} if cookie_params else None
|
||||
)
|
||||
|
||||
# Step 6: Handle request body
|
||||
# Step 6: Handle request body — dispatch on declared content type.
|
||||
# httpx body kwargs (json, content, data, files) are mutually exclusive
|
||||
# but all accept None, so we set exactly one and pass all to Request().
|
||||
files: dict[str, Any] | None = None
|
||||
data: dict[str, Any] | None = None
|
||||
if body is not None:
|
||||
if declared_content_type == "multipart/form-data" and isinstance(
|
||||
body, dict
|
||||
):
|
||||
# httpx requires files= for multipart/form-data encoding.
|
||||
# Wrap plain values as (None, str(value)) tuples so httpx
|
||||
# treats them as form fields. Scalars must be stringified
|
||||
# because httpx rejects non-string/bytes values.
|
||||
# because httpx rejects non-string/bytes values in files=.
|
||||
files = {
|
||||
k: v if isinstance(v, tuple) else (None, str(v))
|
||||
for k, v in body.items()
|
||||
}
|
||||
return httpx.Request(
|
||||
method=method,
|
||||
url=url,
|
||||
params=params,
|
||||
headers=headers,
|
||||
files=files,
|
||||
cookies=cookies,
|
||||
)
|
||||
elif (
|
||||
declared_content_type == "application/x-www-form-urlencoded"
|
||||
and isinstance(body, dict)
|
||||
):
|
||||
return httpx.Request(
|
||||
method=method,
|
||||
url=url,
|
||||
params=params,
|
||||
headers=headers,
|
||||
data=body,
|
||||
cookies=cookies,
|
||||
)
|
||||
data = body
|
||||
elif isinstance(body, dict | list):
|
||||
if (
|
||||
declared_content_type is not None
|
||||
|
|
@ -144,6 +132,8 @@ class RequestDirector:
|
|||
headers=headers,
|
||||
json=json_body,
|
||||
content=content,
|
||||
data=data,
|
||||
files=files,
|
||||
cookies=cookies,
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue