refactor: Update APIRequestComponent to use DataInput for headers and body fields

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-06-20 21:23:47 -03:00
commit 0358166b91

View file

@ -1,14 +1,14 @@
import asyncio import asyncio
import json import json
from typing import Any, List, Optional from typing import Any, List, Optional
from urllib.parse import urlencode, urlparse, parse_qsl, urlunparse from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
import httpx import httpx
from loguru import logger from loguru import logger
from langflow.base.curl.parse import parse_context from langflow.base.curl.parse import parse_context
from langflow.custom import Component from langflow.custom import Component
from langflow.io import DropdownInput, IntInput, NestedDictInput, Output, TextInput, DataInput from langflow.io import DataInput, DropdownInput, IntInput, NestedDictInput, Output, TextInput
from langflow.schema import Data from langflow.schema import Data
from langflow.schema.dotdict import dotdict from langflow.schema.dotdict import dotdict
@ -44,27 +44,17 @@ class APIRequestComponent(Component):
value="GET", value="GET",
info="The HTTP method to use (GET, POST, PATCH, PUT).", info="The HTTP method to use (GET, POST, PATCH, PUT).",
), ),
DataInput( NestedDictInput(
name="headers", name="headers",
display_name="Headers Data", display_name="Headers",
info="The headers to send with the request as a Data object.",
),
NestedDictInput(
name="headers_dict",
display_name="Headers Dictionary",
info="The headers to send with the request as a dictionary. This is populated when using the CURL field.", info="The headers to send with the request as a dictionary. This is populated when using the CURL field.",
advanced=True, input_types=["Data"],
),
DataInput(
name="body",
display_name="Body Data",
info="The body to send with the request as a Data object (for POST, PATCH, PUT).",
), ),
NestedDictInput( NestedDictInput(
name="body_dict", name="body",
display_name="Body Dictionary", display_name="Body",
info="The body to send with the request as a dictionary (for POST, PATCH, PUT). This is populated when using the CURL field.", info="The body to send with the request as a dictionary (for POST, PATCH, PUT). This is populated when using the CURL field.",
advanced=True, input_types=["Data"],
), ),
DataInput( DataInput(
name="query_params", name="query_params",
@ -88,16 +78,16 @@ class APIRequestComponent(Component):
parsed = parse_context(curl) parsed = parse_context(curl)
build_config["urls"]["value"] = [parsed.url] build_config["urls"]["value"] = [parsed.url]
build_config["method"]["value"] = parsed.method.upper() build_config["method"]["value"] = parsed.method.upper()
build_config["headers_dict"]["value"] = dict(parsed.headers) build_config["headers"]["value"] = dict(parsed.headers)
if parsed.data: if parsed.data:
try: try:
json_data = json.loads(parsed.data) json_data = json.loads(parsed.data)
build_config["body_dict"]["value"] = json_data build_config["body"]["value"] = json_data
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logger.error(f"Error decoding JSON data: {e}") logger.error(f"Error decoding JSON data: {e}")
else: else:
build_config["body_dict"]["value"] = None build_config["body"]["value"] = {}
except Exception as exc: except Exception as exc:
logger.error(f"Error parsing curl: {exc}") logger.error(f"Error parsing curl: {exc}")
raise ValueError(f"Error parsing curl: {exc}") raise ValueError(f"Error parsing curl: {exc}")
@ -106,11 +96,6 @@ class APIRequestComponent(Component):
def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None): def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None):
if field_name == "curl" and field_value: if field_name == "curl" and field_value:
build_config = self.parse_curl(field_value, build_config) build_config = self.parse_curl(field_value, build_config)
# Apply the parsed values to the component fields
self.urls = build_config["urls"]["value"]
self.method = build_config["method"]["value"]
self.headers_dict = build_config["headers"]["value"]
self.body_dict = build_config["body"]["value"]
return build_config return build_config
async def make_request( async def make_request(
@ -172,8 +157,8 @@ class APIRequestComponent(Component):
method = self.method method = self.method
urls = [url.strip() for url in self.urls if url.strip()] urls = [url.strip() for url in self.urls if url.strip()]
curl = self.curl curl = self.curl
headers = self.headers if self.headers else (self.headers_dict or {}) headers = self.headers or {}
body = self.body if self.body else (self.body_dict or {}) body = self.body or {}
timeout = self.timeout timeout = self.timeout
query_params = self.query_params.data if self.query_params else {} query_params = self.query_params.data if self.query_params else {}