From 372074edbab946155bed34def356be38df27f2b4 Mon Sep 17 00:00:00 2001 From: Guangdong Liu Date: Wed, 27 Aug 2025 13:23:34 +0800 Subject: [PATCH] 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> --- .../workflow/nodes/http_request/executor.py | 30 ++++++++----------- api/services/external_knowledge_service.py | 17 +++++++++-- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/api/core/workflow/nodes/http_request/executor.py b/api/core/workflow/nodes/http_request/executor.py index a5a578a6f..b6f938361 100644 --- a/api/core/workflow/nodes/http_request/executor.py +++ b/api/core/workflow/nodes/http_request/executor.py @@ -329,22 +329,16 @@ class Executor: """ do http request depending on api bundle """ - if self.method not in { - "get", - "head", - "post", - "put", - "delete", - "patch", - "options", - "GET", - "POST", - "PUT", - "PATCH", - "DELETE", - "HEAD", - "OPTIONS", - }: + _METHOD_MAP = { + "get": ssrf_proxy.get, + "head": ssrf_proxy.head, + "post": ssrf_proxy.post, + "put": ssrf_proxy.put, + "delete": ssrf_proxy.delete, + "patch": ssrf_proxy.patch, + } + method_lc = self.method.lower() + if method_lc not in _METHOD_MAP: raise InvalidHttpMethodError(f"Invalid http method {self.method}") request_args = { @@ -362,11 +356,11 @@ class Executor: } # request_args = {k: v for k, v in request_args.items() if v is not None} try: - response = getattr(ssrf_proxy, self.method.lower())(**request_args) + response: httpx.Response = _METHOD_MAP[method_lc](**request_args) except (ssrf_proxy.MaxRetriesExceededError, httpx.RequestError) as e: raise HttpRequestNodeError(str(e)) from e # FIXME: fix type ignore, this maybe httpx type issue - return response # type: ignore + return response def invoke(self) -> Response: # assemble headers diff --git a/api/services/external_knowledge_service.py b/api/services/external_knowledge_service.py index 2f1babba6..fcf57070e 100644 --- a/api/services/external_knowledge_service.py +++ b/api/services/external_knowledge_service.py @@ -9,6 +9,7 @@ from sqlalchemy import select from constants import HIDDEN_VALUE from core.helper import ssrf_proxy from core.rag.entities.metadata_entities import MetadataCondition +from core.workflow.nodes.http_request.exc import InvalidHttpMethodError from extensions.ext_database import db from libs.datetime_utils import naive_utc_now from models.dataset import ( @@ -185,9 +186,19 @@ class ExternalDatasetService: "follow_redirects": True, } - response: httpx.Response = getattr(ssrf_proxy, settings.request_method)( - data=json.dumps(settings.params), files=files, **kwargs - ) + _METHOD_MAP = { + "get": ssrf_proxy.get, + "head": ssrf_proxy.head, + "post": ssrf_proxy.post, + "put": ssrf_proxy.put, + "delete": ssrf_proxy.delete, + "patch": ssrf_proxy.patch, + } + method_lc = settings.request_method.lower() + if method_lc not in _METHOD_MAP: + raise InvalidHttpMethodError(f"Invalid http method {settings.request_method}") + + response: httpx.Response = _METHOD_MAP[method_lc](data=json.dumps(settings.params), files=files, **kwargs) return response @staticmethod