diff --git a/endpoints/helpers.py b/endpoints/helpers.py index c9628b2..579fcc2 100644 --- a/endpoints/helpers.py +++ b/endpoints/helpers.py @@ -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. @@ -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. @@ -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: @@ -63,7 +65,13 @@ def validate_api_key(r: Request, settings: Mapping) -> Optional[Response]: return None -EndpointRoute = Literal["/workflow/", "/chatflow/", "/single-workflow", "/single-chatflow"] + +EndpointRoute = Literal[ + "/workflow/", + "/chatflow/", + "/single-workflow", + "/single-chatflow" +] def determine_route(path: str) -> Optional[EndpointRoute]: """ @@ -83,4 +91,4 @@ def determine_route(path: str) -> Optional[EndpointRoute]: return "/single-workflow" elif path.startswith("/single-chatflow"): return "/single-chatflow" - return None \ No newline at end of file + return None diff --git a/endpoints/invoke_endpoint.py b/endpoints/invoke_endpoint.py index e6b174c..5b2cb27 100644 --- a/endpoints/invoke_endpoint.py +++ b/endpoints/invoke_endpoint.py @@ -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")