diff --git a/core/routes/civitai_search.py b/core/routes/civitai_search.py index 3aa2367..87e3590 100644 --- a/core/routes/civitai_search.py +++ b/core/routes/civitai_search.py @@ -2,15 +2,14 @@ from ..services.model_service import ModelService from .context import RouteContext +from .helpers import register_service_post_route def register_civitai_search_routes(context: RouteContext): - json_api_endpoint = context.get("json_api_endpoint") - routes = context.get("routes") model_service = ModelService(context) - - @routes.post("/model_resolver/civitai-search") - @json_api_endpoint("civitai-search") - async def civitai_search(request): - """Delegate model operation to the model service.""" - return await model_service.civitai_search(request) + register_service_post_route( + context, + path="/model_resolver/civitai-search", + error_prefix="civitai-search", + operation=model_service.civitai_search, + ) diff --git a/core/routes/custom_url.py b/core/routes/custom_url.py index cc519f8..787c735 100644 --- a/core/routes/custom_url.py +++ b/core/routes/custom_url.py @@ -2,15 +2,14 @@ from ..services.model_service import ModelService from .context import RouteContext +from .helpers import register_service_post_route def register_custom_url_routes(context: RouteContext): - json_api_endpoint = context.get("json_api_endpoint") - routes = context.get("routes") model_service = ModelService(context) - - @routes.post("/model_resolver/custom-url") - @json_api_endpoint("custom-url") - async def custom_url(request): - """Delegate model operation to the model service.""" - return await model_service.custom_url(request) + register_service_post_route( + context, + path="/model_resolver/custom-url", + error_prefix="custom-url", + operation=model_service.custom_url, + ) diff --git a/core/routes/helpers.py b/core/routes/helpers.py index a86a7e4..2f27c47 100644 --- a/core/routes/helpers.py +++ b/core/routes/helpers.py @@ -5,6 +5,19 @@ import threading from functools import wraps +def register_service_post_route(context, *, path, error_prefix, operation): + """Register a JSON POST route for a bound service operation.""" + json_api_endpoint = context.get("json_api_endpoint") + routes = context.get("routes") + + async def service_route(request): + return await operation(request) + + service_route.__name__ = getattr(operation, "__name__", "service_route") + decorated_route = json_api_endpoint(error_prefix)(service_route) + routes.post(path)(decorated_route) + + def create_route_helpers(web, logger, load_settings, hash_calculation_cancelled): """Create route helpers bound to the current ComfyUI server context.""" diff --git a/core/routes/model_details.py b/core/routes/model_details.py index 3fb4524..80239d5 100644 --- a/core/routes/model_details.py +++ b/core/routes/model_details.py @@ -2,15 +2,14 @@ from ..services.model_service import ModelService from .context import RouteContext +from .helpers import register_service_post_route def register_model_details_routes(context: RouteContext): - json_api_endpoint = context.get("json_api_endpoint") - routes = context.get("routes") model_service = ModelService(context) - - @routes.post("/model_resolver/model-details") - @json_api_endpoint("model-details") - async def model_details(request): - """Delegate model operation to the model service.""" - return await model_service.model_details(request) + register_service_post_route( + context, + path="/model_resolver/model-details", + error_prefix="model-details", + operation=model_service.model_details, + )