Skip to content

Commit 372074e

Browse files
refactor(http-request): Remove the reflective calls to ssrf_proxy and replace them with explicitly defined dictionary retrievals. (#24596)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 826f19e commit 372074e

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

api/core/workflow/nodes/http_request/executor.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,16 @@ def _do_http_request(self, headers: dict[str, Any]) -> httpx.Response:
329329
"""
330330
do http request depending on api bundle
331331
"""
332-
if self.method not in {
333-
"get",
334-
"head",
335-
"post",
336-
"put",
337-
"delete",
338-
"patch",
339-
"options",
340-
"GET",
341-
"POST",
342-
"PUT",
343-
"PATCH",
344-
"DELETE",
345-
"HEAD",
346-
"OPTIONS",
347-
}:
332+
_METHOD_MAP = {
333+
"get": ssrf_proxy.get,
334+
"head": ssrf_proxy.head,
335+
"post": ssrf_proxy.post,
336+
"put": ssrf_proxy.put,
337+
"delete": ssrf_proxy.delete,
338+
"patch": ssrf_proxy.patch,
339+
}
340+
method_lc = self.method.lower()
341+
if method_lc not in _METHOD_MAP:
348342
raise InvalidHttpMethodError(f"Invalid http method {self.method}")
349343

350344
request_args = {
@@ -362,11 +356,11 @@ def _do_http_request(self, headers: dict[str, Any]) -> httpx.Response:
362356
}
363357
# request_args = {k: v for k, v in request_args.items() if v is not None}
364358
try:
365-
response = getattr(ssrf_proxy, self.method.lower())(**request_args)
359+
response: httpx.Response = _METHOD_MAP[method_lc](**request_args)
366360
except (ssrf_proxy.MaxRetriesExceededError, httpx.RequestError) as e:
367361
raise HttpRequestNodeError(str(e)) from e
368362
# FIXME: fix type ignore, this maybe httpx type issue
369-
return response # type: ignore
363+
return response
370364

371365
def invoke(self) -> Response:
372366
# assemble headers

api/services/external_knowledge_service.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from constants import HIDDEN_VALUE
1010
from core.helper import ssrf_proxy
1111
from core.rag.entities.metadata_entities import MetadataCondition
12+
from core.workflow.nodes.http_request.exc import InvalidHttpMethodError
1213
from extensions.ext_database import db
1314
from libs.datetime_utils import naive_utc_now
1415
from models.dataset import (
@@ -185,9 +186,19 @@ def process_external_api(
185186
"follow_redirects": True,
186187
}
187188

188-
response: httpx.Response = getattr(ssrf_proxy, settings.request_method)(
189-
data=json.dumps(settings.params), files=files, **kwargs
190-
)
189+
_METHOD_MAP = {
190+
"get": ssrf_proxy.get,
191+
"head": ssrf_proxy.head,
192+
"post": ssrf_proxy.post,
193+
"put": ssrf_proxy.put,
194+
"delete": ssrf_proxy.delete,
195+
"patch": ssrf_proxy.patch,
196+
}
197+
method_lc = settings.request_method.lower()
198+
if method_lc not in _METHOD_MAP:
199+
raise InvalidHttpMethodError(f"Invalid http method {settings.request_method}")
200+
201+
response: httpx.Response = _METHOD_MAP[method_lc](data=json.dumps(settings.params), files=files, **kwargs)
191202
return response
192203

193204
@staticmethod

0 commit comments

Comments
 (0)