Skip to content

Commit ecc8f01

Browse files
committed
Enhance proxy response handling to support file uploads and form data in OIDC API Proxy
1 parent 50e0ae1 commit ecc8f01

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

endpoints/helpers/endpoint.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ def proxy_response(
1111
headers: httpx._types.HeaderTypes | None,
1212
params: httpx._types.QueryParamTypes | None,
1313
json: Any | None,
14+
data: httpx._types.RequestData | None,
15+
files: httpx._types.RequestFiles | None,
1416
) -> werkzeug.Response:
1517
with httpx.Client() as client:
16-
response = client.request(method=method, url=url, headers=headers, params=params, json=json)
18+
response = client.request(
19+
method=method, url=url, headers=headers, params=params, json=json, data=data, files=files
20+
)
1721

1822
return werkzeug.Response(
1923
response=response.content,
@@ -28,8 +32,10 @@ def proxy_stream_response(
2832
headers: httpx._types.HeaderTypes | None,
2933
params: httpx._types.QueryParamTypes | None,
3034
json: Any | None,
35+
data: httpx._types.RequestData | None,
36+
files: httpx._types.RequestFiles | None,
3137
) -> werkzeug.Response:
32-
stream_context = httpx.stream(method=method, url=url, headers=headers, json=json)
38+
stream_context = httpx.stream(method=method, url=url, headers=headers, json=json, data=data, files=files)
3339

3440
# Manually enter the context manager to get the response
3541
stream_response = stream_context.__enter__()

endpoints/oidc_api_proxy.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
6464
# prepare json if request is json
6565
json = r.get_json() if r.is_json else None
6666

67+
# prepare files if request has files
68+
files = [
69+
(file, (r.files[file].filename, r.files[file].stream, r.files[file].content_type)) for file in r.files
70+
] or None
71+
6772
# Forward request to Dify API with Syncronous HTTP Client
6873
try:
69-
return proxy_stream_response(method=r.method, url=url, headers=headers, params=r.args, json=json)
74+
return proxy_stream_response(
75+
method=r.method, url=url, headers=headers, params=r.args, json=json, data=r.form, files=files
76+
)
7077
except Exception as e:
7178
print(str(e))
7279
return OidcApiProxyErrorResponse(str(e), 500)

0 commit comments

Comments
 (0)