mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 11:42:29 +08:00
improve the code readability of http_executor node (#4360)
This commit is contained in:
parent
16d47923c3
commit
3271e3e803
|
@ -14,28 +14,18 @@ from core.workflow.entities.variable_pool import ValueType, VariablePool
|
|||
from core.workflow.nodes.http_request.entities import HttpRequestNodeData
|
||||
from core.workflow.utils.variable_template_parser import VariableTemplateParser
|
||||
|
||||
MAX_BINARY_SIZE = int(os.environ.get('HTTP_REQUEST_NODE_MAX_BINARY_SIZE', str(1024 * 1024 * 10))) # 10MB
|
||||
MAX_BINARY_SIZE = int(os.environ.get('HTTP_REQUEST_NODE_MAX_BINARY_SIZE', 1024 * 1024 * 10)) # 10MB
|
||||
READABLE_MAX_BINARY_SIZE = f'{MAX_BINARY_SIZE / 1024 / 1024:.2f}MB'
|
||||
MAX_TEXT_SIZE = int(os.environ.get('HTTP_REQUEST_NODE_MAX_TEXT_SIZE', str(1024 * 1024))) # 10MB # 1MB
|
||||
MAX_TEXT_SIZE = int(os.environ.get('HTTP_REQUEST_NODE_MAX_TEXT_SIZE', 1024 * 1024)) # 1MB
|
||||
READABLE_MAX_TEXT_SIZE = f'{MAX_TEXT_SIZE / 1024 / 1024:.2f}MB'
|
||||
|
||||
|
||||
class HttpExecutorResponse:
|
||||
headers: dict[str, str]
|
||||
response: Union[httpx.Response, requests.Response]
|
||||
|
||||
def __init__(self, response: Union[httpx.Response, requests.Response] = None):
|
||||
"""
|
||||
init
|
||||
"""
|
||||
headers = {}
|
||||
if isinstance(response, httpx.Response):
|
||||
for k, v in response.headers.items():
|
||||
headers[k] = v
|
||||
elif isinstance(response, requests.Response):
|
||||
for k, v in response.headers.items():
|
||||
headers[k] = v
|
||||
|
||||
self.headers = headers
|
||||
self.headers = response.headers
|
||||
self.response = response
|
||||
|
||||
@property
|
||||
|
@ -45,21 +35,11 @@ class HttpExecutorResponse:
|
|||
"""
|
||||
content_type = self.get_content_type()
|
||||
file_content_types = ['image', 'audio', 'video']
|
||||
for v in file_content_types:
|
||||
if v in content_type:
|
||||
return True
|
||||
|
||||
return False
|
||||
return any(v in content_type for v in file_content_types)
|
||||
|
||||
def get_content_type(self) -> str:
|
||||
"""
|
||||
get content type
|
||||
"""
|
||||
for key, val in self.headers.items():
|
||||
if key.lower() == 'content-type':
|
||||
return val
|
||||
|
||||
return ''
|
||||
return self.headers.get('content-type')
|
||||
|
||||
def extract_file(self) -> tuple[str, bytes]:
|
||||
"""
|
||||
|
@ -75,9 +55,7 @@ class HttpExecutorResponse:
|
|||
"""
|
||||
get content
|
||||
"""
|
||||
if isinstance(self.response, httpx.Response):
|
||||
return self.response.text
|
||||
elif isinstance(self.response, requests.Response):
|
||||
if isinstance(self.response, httpx.Response | requests.Response):
|
||||
return self.response.text
|
||||
else:
|
||||
raise ValueError(f'Invalid response type {type(self.response)}')
|
||||
|
@ -87,9 +65,7 @@ class HttpExecutorResponse:
|
|||
"""
|
||||
get body
|
||||
"""
|
||||
if isinstance(self.response, httpx.Response):
|
||||
return self.response.content
|
||||
elif isinstance(self.response, requests.Response):
|
||||
if isinstance(self.response, httpx.Response | requests.Response):
|
||||
return self.response.content
|
||||
else:
|
||||
raise ValueError(f'Invalid response type {type(self.response)}')
|
||||
|
@ -99,9 +75,7 @@ class HttpExecutorResponse:
|
|||
"""
|
||||
get status code
|
||||
"""
|
||||
if isinstance(self.response, httpx.Response):
|
||||
return self.response.status_code
|
||||
elif isinstance(self.response, requests.Response):
|
||||
if isinstance(self.response, httpx.Response | requests.Response):
|
||||
return self.response.status_code
|
||||
else:
|
||||
raise ValueError(f'Invalid response type {type(self.response)}')
|
||||
|
@ -138,10 +112,8 @@ class HttpExecutor:
|
|||
variable_selectors: list[VariableSelector]
|
||||
timeout: HttpRequestNodeData.Timeout
|
||||
|
||||
def __init__(self, node_data: HttpRequestNodeData, timeout: HttpRequestNodeData.Timeout, variable_pool: Optional[VariablePool] = None):
|
||||
"""
|
||||
init
|
||||
"""
|
||||
def __init__(self, node_data: HttpRequestNodeData, timeout: HttpRequestNodeData.Timeout,
|
||||
variable_pool: Optional[VariablePool] = None):
|
||||
self.server_url = node_data.url
|
||||
self.method = node_data.method
|
||||
self.authorization = node_data.authorization
|
||||
|
@ -155,7 +127,8 @@ class HttpExecutor:
|
|||
self.variable_selectors = []
|
||||
self._init_template(node_data, variable_pool)
|
||||
|
||||
def _is_json_body(self, body: HttpRequestNodeData.Body):
|
||||
@staticmethod
|
||||
def _is_json_body(body: HttpRequestNodeData.Body):
|
||||
"""
|
||||
check if body is json
|
||||
"""
|
||||
|
@ -168,52 +141,43 @@ class HttpExecutor:
|
|||
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _to_dict(convert_item: str, convert_text: str, maxsplit: int = -1):
|
||||
"""
|
||||
Convert the string like `aa:bb\n cc:dd` to dict `{aa:bb, cc:dd}`
|
||||
:param convert_item: A label for what item to be converted, params, headers or body.
|
||||
:param convert_text: The string containing key-value pairs separated by '\n'.
|
||||
:param maxsplit: The maximum number of splits allowed for the ':' character in each key-value pair. Default is -1 (no limit).
|
||||
:return: A dictionary containing the key-value pairs from the input string.
|
||||
"""
|
||||
kv_paris = convert_text.split('\n')
|
||||
result = {}
|
||||
for kv in kv_paris:
|
||||
if not kv.strip():
|
||||
continue
|
||||
|
||||
kv = kv.split(':', maxsplit=maxsplit)
|
||||
if len(kv) == 2:
|
||||
k, v = kv
|
||||
elif len(kv) == 1:
|
||||
k, v = kv[0], ''
|
||||
else:
|
||||
raise ValueError(f'Invalid {convert_item} {kv}')
|
||||
result[k.strip()] = v
|
||||
return result
|
||||
|
||||
def _init_template(self, node_data: HttpRequestNodeData, variable_pool: Optional[VariablePool] = None):
|
||||
"""
|
||||
init template
|
||||
"""
|
||||
variable_selectors = []
|
||||
|
||||
# extract all template in url
|
||||
self.server_url, server_url_variable_selectors = self._format_template(node_data.url, variable_pool)
|
||||
|
||||
# extract all template in params
|
||||
params, params_variable_selectors = self._format_template(node_data.params, variable_pool)
|
||||
|
||||
# fill in params
|
||||
kv_paris = params.split('\n')
|
||||
for kv in kv_paris:
|
||||
if not kv.strip():
|
||||
continue
|
||||
|
||||
kv = kv.split(':')
|
||||
if len(kv) == 2:
|
||||
k, v = kv
|
||||
elif len(kv) == 1:
|
||||
k, v = kv[0], ''
|
||||
else:
|
||||
raise ValueError(f'Invalid params {kv}')
|
||||
|
||||
self.params[k.strip()] = v
|
||||
self.params = self._to_dict("params", params)
|
||||
|
||||
# extract all template in headers
|
||||
headers, headers_variable_selectors = self._format_template(node_data.headers, variable_pool)
|
||||
|
||||
# fill in headers
|
||||
kv_paris = headers.split('\n')
|
||||
for kv in kv_paris:
|
||||
if not kv.strip():
|
||||
continue
|
||||
|
||||
kv = kv.split(':')
|
||||
if len(kv) == 2:
|
||||
k, v = kv
|
||||
elif len(kv) == 1:
|
||||
k, v = kv[0], ''
|
||||
else:
|
||||
raise ValueError(f'Invalid headers {kv}')
|
||||
|
||||
self.headers[k.strip()] = v.strip()
|
||||
self.headers = self._to_dict("headers", headers)
|
||||
|
||||
# extract all template in body
|
||||
body_data_variable_selectors = []
|
||||
|
@ -231,18 +195,7 @@ class HttpExecutor:
|
|||
self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
||||
|
||||
if node_data.body.type in ['form-data', 'x-www-form-urlencoded']:
|
||||
body = {}
|
||||
kv_paris = body_data.split('\n')
|
||||
for kv in kv_paris:
|
||||
if not kv.strip():
|
||||
continue
|
||||
kv = kv.split(':', 1)
|
||||
if len(kv) == 2:
|
||||
body[kv[0].strip()] = kv[1]
|
||||
elif len(kv) == 1:
|
||||
body[kv[0].strip()] = ''
|
||||
else:
|
||||
raise ValueError(f'Invalid body {kv}')
|
||||
body = self._to_dict("body", body_data, 1)
|
||||
|
||||
if node_data.body.type == 'form-data':
|
||||
self.files = {
|
||||
|
@ -292,10 +245,12 @@ class HttpExecutor:
|
|||
|
||||
if executor_response.is_file:
|
||||
if executor_response.size > MAX_BINARY_SIZE:
|
||||
raise ValueError(f'File size is too large, max size is {READABLE_MAX_BINARY_SIZE}, but current size is {executor_response.readable_size}.')
|
||||
raise ValueError(
|
||||
f'File size is too large, max size is {READABLE_MAX_BINARY_SIZE}, but current size is {executor_response.readable_size}.')
|
||||
else:
|
||||
if executor_response.size > MAX_TEXT_SIZE:
|
||||
raise ValueError(f'Text size is too large, max size is {READABLE_MAX_TEXT_SIZE}, but current size is {executor_response.readable_size}.')
|
||||
raise ValueError(
|
||||
f'Text size is too large, max size is {READABLE_MAX_TEXT_SIZE}, but current size is {executor_response.readable_size}.')
|
||||
|
||||
return executor_response
|
||||
|
||||
|
@ -303,7 +258,6 @@ class HttpExecutor:
|
|||
"""
|
||||
do http request depending on api bundle
|
||||
"""
|
||||
# do http request
|
||||
kwargs = {
|
||||
'url': self.server_url,
|
||||
'headers': headers,
|
||||
|
@ -312,23 +266,12 @@ class HttpExecutor:
|
|||
'follow_redirects': True
|
||||
}
|
||||
|
||||
if self.method == 'get':
|
||||
response = ssrf_proxy.get(**kwargs)
|
||||
elif self.method == 'post':
|
||||
response = ssrf_proxy.post(data=self.body, files=self.files, **kwargs)
|
||||
elif self.method == 'put':
|
||||
response = ssrf_proxy.put(data=self.body, files=self.files, **kwargs)
|
||||
elif self.method == 'delete':
|
||||
response = ssrf_proxy.delete(data=self.body, files=self.files, **kwargs)
|
||||
elif self.method == 'patch':
|
||||
response = ssrf_proxy.patch(data=self.body, files=self.files, **kwargs)
|
||||
elif self.method == 'head':
|
||||
response = ssrf_proxy.head(**kwargs)
|
||||
elif self.method == 'options':
|
||||
response = ssrf_proxy.options(**kwargs)
|
||||
if self.method in ('get', 'head', 'options'):
|
||||
response = getattr(ssrf_proxy, self.method)(**kwargs)
|
||||
elif self.method in ('post', 'put', 'delete', 'patch'):
|
||||
response = getattr(ssrf_proxy, self.method)(data=self.body, files=self.files, **kwargs)
|
||||
else:
|
||||
raise ValueError(f'Invalid http method {self.method}')
|
||||
|
||||
return response
|
||||
|
||||
def invoke(self) -> HttpExecutorResponse:
|
||||
|
@ -348,9 +291,6 @@ class HttpExecutor:
|
|||
"""
|
||||
convert to raw request
|
||||
"""
|
||||
if mask_authorization_header == None:
|
||||
mask_authorization_header = True
|
||||
|
||||
server_url = self.server_url
|
||||
if self.params:
|
||||
server_url += f'?{urlencode(self.params)}'
|
||||
|
|
Loading…
Reference in New Issue
Block a user