Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions endpoints/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from middlewares.discord_middleware import DiscordMiddleware
from middlewares.default_middleware import DefaultMiddleware


def apply_middleware(r: Request, settings: Mapping) -> Optional[Response]:
"""
Applies middleware based on the settings provided.
Expand Down Expand Up @@ -34,6 +35,7 @@ def apply_middleware(r: Request, settings: Mapping) -> Optional[Response]:

return None


def validate_api_key(r: Request, settings: Mapping) -> Optional[Response]:
"""
Validates the API key based on the location specified in the settings.
Expand All @@ -54,7 +56,7 @@ def validate_api_key(r: Request, settings: Mapping) -> Optional[Response]:
if request_api_key != expected_api_key:
return Response(json.dumps({"error": "Invalid API key"}),
status=403, content_type="application/json")

elif api_key_location == "token_query_param":
request_api_key = r.args.get("difyToken")
if request_api_key != expected_api_key:
Expand All @@ -63,7 +65,13 @@ def validate_api_key(r: Request, settings: Mapping) -> Optional[Response]:

return None

EndpointRoute = Literal["/workflow/<app_id>", "/chatflow/<app_id>", "/single-workflow", "/single-chatflow"]

EndpointRoute = Literal[
"/workflow/<app_id>",
"/chatflow/<app_id>",
"/single-workflow",
"/single-chatflow"
]

def determine_route(path: str) -> Optional[EndpointRoute]:
"""
Expand All @@ -83,4 +91,4 @@ def determine_route(path: str) -> Optional[EndpointRoute]:
return "/single-workflow"
elif path.startswith("/single-chatflow"):
return "/single-chatflow"
return None
return None
4 changes: 2 additions & 2 deletions endpoints/invoke_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
response = self._invoke_workflow(
static_app_id, inputs, settings.get('raw_data_output', False))

if not response:
return Response(json.dumps({"error": "Failed to get response"}), status=500, content_type="application/json")
else:
# Return response
logger.debug("%s response: %s", route, response)
return Response(json.dumps(response), status=200, content_type="application/json")

if not response:
return Response(json.dumps({"error": "Failed to get response"}), status=500, content_type="application/json")
except (json.JSONDecodeError, KeyError, TypeError) as e:
logger.error("Error during request processing: %s", str(e))
return Response(json.dumps({"error": str(e)}), status=500, content_type="application/json")
Expand Down
Loading