From 92bc06be002a8898dbed343bb82a82415942d82a Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Tue, 18 Jun 2024 22:11:21 -0300 Subject: [PATCH] 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. --- .../base/langflow/components/data/Webhook.py | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/backend/base/langflow/components/data/Webhook.py b/src/backend/base/langflow/components/data/Webhook.py index c7d9f2cf3..3b88b4734 100644 --- a/src/backend/base/langflow/components/data/Webhook.py +++ b/src/backend/base/langflow/components/data/Webhook.py @@ -1,39 +1,39 @@ 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.dotdict import dotdict +from langflow.template import Output -class WebhookComponent(CustomComponent): +class WebhookComponent(Component): display_name = "Webhook Input" description = "Defines a webhook input for the flow." - def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None): - if field_name == "webhook_id": - build_config["webhook_id"]["value"] = uuid.uuid4().hex - return build_config + inputs = [ + TextInput( + name="data", + 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): - 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: + def build_data(self) -> Data: message = "" + if not self.data: + self.status = "No data provided." + return Data(data={}) try: - body = json.loads(data or "{}") + body = json.loads(self.data or "{}") except json.JSONDecodeError: - body = {"payload": data} - message = f"Invalid JSON payload. Please check the format.\n\n{data}" - record = Data(data=body) + body = {"payload": self.data} + message = f"Invalid JSON payload. Please check the format.\n\n{self.data}" + data = Data(data=body) if not message: - message = record + message = data self.status = message - return record + return data