refactor: Update WebhookComponent to use langflow.custom.Component and langflow.inputs.TextInput

The code changes in `Webhook.py` update the `WebhookComponent` class to inherit from `langflow.custom.Component` instead of `langflow.custom.CustomComponent`. It also replaces the `langflow.schema.dotdict` import with `langflow.template.Output` and updates the `build_data` method to use the `self.data` attribute instead of the `data` parameter. Additionally, it adds a new input `TextInput` for providing a JSON payload. This refactor improves the consistency and readability of the code. The commit message follows the established convention of using a prefix to indicate the type of change.
This commit is contained in:
ogabrielluiz 2024-06-18 22:11:21 -03:00
commit 92bc06be00

View file

@ -1,39 +1,39 @@
import json import json
import uuid
from typing import Any, Optional
from langflow.custom import CustomComponent from langflow.custom import Component
from langflow.inputs import TextInput
from langflow.schema import Data from langflow.schema import Data
from langflow.schema.dotdict import dotdict from langflow.template import Output
class WebhookComponent(CustomComponent): class WebhookComponent(Component):
display_name = "Webhook Input" display_name = "Webhook Input"
description = "Defines a webhook input for the flow." description = "Defines a webhook input for the flow."
def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None): inputs = [
if field_name == "webhook_id": TextInput(
build_config["webhook_id"]["value"] = uuid.uuid4().hex name="data",
return build_config display_name="Data",
info="Use this field to quickly test the webhook component by providing a JSON payload.",
multiline=True,
)
]
outputs = [
Output(display_name="Data", name="output_data", method="build_data"),
]
def build_config(self): def build_data(self) -> Data:
return {
"data": {
"display_name": "Data",
"info": "Use this field to quickly test the webhook component by providing a JSON payload.",
"multiline": True,
}
}
def build(self, data: Optional[str] = "") -> Data:
message = "" message = ""
if not self.data:
self.status = "No data provided."
return Data(data={})
try: try:
body = json.loads(data or "{}") body = json.loads(self.data or "{}")
except json.JSONDecodeError: except json.JSONDecodeError:
body = {"payload": data} body = {"payload": self.data}
message = f"Invalid JSON payload. Please check the format.\n\n{data}" message = f"Invalid JSON payload. Please check the format.\n\n{self.data}"
record = Data(data=body) data = Data(data=body)
if not message: if not message:
message = record message = data
self.status = message self.status = message
return record return data