212 lines
6.2 KiB
Python
Executable File
212 lines
6.2 KiB
Python
Executable File
"""
|
|
Helper functions for route building
|
|
Provides convenience methods for common routing patterns
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
|
|
def build_base_route(service: str, resource: str, tenant_id: Optional[str] = None) -> str:
|
|
"""
|
|
Build a base CRUD route
|
|
|
|
Args:
|
|
service: Service name
|
|
resource: Resource name
|
|
tenant_id: Optional tenant ID (if None, uses {tenant_id} placeholder)
|
|
|
|
Returns:
|
|
Complete route path
|
|
|
|
Example:
|
|
build_base_route('inventory', 'ingredients')
|
|
# Returns: '/api/v1/tenants/{tenant_id}/inventory/ingredients'
|
|
|
|
build_base_route('inventory', 'ingredients', 'uuid-123')
|
|
# Returns: '/api/v1/tenants/uuid-123/inventory/ingredients'
|
|
"""
|
|
tenant_part = tenant_id if tenant_id else "{tenant_id}"
|
|
return f"/api/v1/tenants/{tenant_part}/{service}/{resource}"
|
|
|
|
|
|
def build_dashboard_route(service: str, operation: str, tenant_id: Optional[str] = None) -> str:
|
|
"""
|
|
Build a dashboard route
|
|
|
|
Args:
|
|
service: Service name
|
|
operation: Dashboard operation
|
|
tenant_id: Optional tenant ID
|
|
|
|
Returns:
|
|
Complete route path
|
|
|
|
Example:
|
|
build_dashboard_route('production', 'summary')
|
|
# Returns: '/api/v1/tenants/{tenant_id}/production/dashboard/summary'
|
|
"""
|
|
tenant_part = tenant_id if tenant_id else "{tenant_id}"
|
|
return f"/api/v1/tenants/{tenant_part}/{service}/dashboard/{operation}"
|
|
|
|
|
|
def build_analytics_route(service: str, operation: str, tenant_id: Optional[str] = None) -> str:
|
|
"""
|
|
Build an analytics route
|
|
|
|
Args:
|
|
service: Service name
|
|
operation: Analytics operation
|
|
tenant_id: Optional tenant ID
|
|
|
|
Returns:
|
|
Complete route path
|
|
|
|
Example:
|
|
build_analytics_route('production', 'equipment-efficiency')
|
|
# Returns: '/api/v1/tenants/{tenant_id}/production/analytics/equipment-efficiency'
|
|
"""
|
|
tenant_part = tenant_id if tenant_id else "{tenant_id}"
|
|
return f"/api/v1/tenants/{tenant_part}/{service}/analytics/{operation}"
|
|
|
|
|
|
def build_operations_route(service: str, operation: str, tenant_id: Optional[str] = None) -> str:
|
|
"""
|
|
Build a service operations route
|
|
|
|
Args:
|
|
service: Service name
|
|
operation: Operation name
|
|
tenant_id: Optional tenant ID
|
|
|
|
Returns:
|
|
Complete route path
|
|
|
|
Example:
|
|
build_operations_route('production', 'schedule-batch')
|
|
# Returns: '/api/v1/tenants/{tenant_id}/production/operations/schedule-batch'
|
|
"""
|
|
tenant_part = tenant_id if tenant_id else "{tenant_id}"
|
|
return f"/api/v1/tenants/{tenant_part}/{service}/operations/{operation}"
|
|
|
|
|
|
def build_resource_detail_route(
|
|
service: str,
|
|
resource: str,
|
|
resource_id: Optional[str] = None,
|
|
tenant_id: Optional[str] = None,
|
|
id_param_name: str = "id"
|
|
) -> str:
|
|
"""
|
|
Build a route for individual resource details
|
|
|
|
Args:
|
|
service: Service name
|
|
resource: Resource name
|
|
resource_id: Optional resource ID (if None, uses parameter name)
|
|
tenant_id: Optional tenant ID
|
|
id_param_name: Name of ID parameter when resource_id is None
|
|
|
|
Returns:
|
|
Complete route path
|
|
|
|
Example:
|
|
build_resource_detail_route('inventory', 'ingredients', id_param_name='ingredient_id')
|
|
# Returns: '/api/v1/tenants/{tenant_id}/inventory/ingredients/{ingredient_id}'
|
|
|
|
build_resource_detail_route('inventory', 'ingredients', 'uuid-456', 'uuid-123')
|
|
# Returns: '/api/v1/tenants/uuid-123/inventory/ingredients/uuid-456'
|
|
"""
|
|
base = build_base_route(service, resource, tenant_id)
|
|
id_part = resource_id if resource_id else f"{{{id_param_name}}}"
|
|
return f"{base}/{id_part}"
|
|
|
|
|
|
def build_nested_route(
|
|
service: str,
|
|
parent_resource: str,
|
|
child_resource: str,
|
|
parent_id: Optional[str] = None,
|
|
tenant_id: Optional[str] = None,
|
|
parent_id_param: str = "parent_id"
|
|
) -> str:
|
|
"""
|
|
Build a route for nested resources
|
|
|
|
Args:
|
|
service: Service name
|
|
parent_resource: Parent resource name
|
|
child_resource: Child resource name
|
|
parent_id: Optional parent resource ID
|
|
tenant_id: Optional tenant ID
|
|
parent_id_param: Parent ID parameter name when parent_id is None
|
|
|
|
Returns:
|
|
Complete route path
|
|
|
|
Example:
|
|
build_nested_route('inventory', 'ingredients', 'stock', parent_id_param='ingredient_id')
|
|
# Returns: '/api/v1/tenants/{tenant_id}/inventory/ingredients/{ingredient_id}/stock'
|
|
"""
|
|
parent = build_resource_detail_route(service, parent_resource, parent_id, tenant_id, parent_id_param)
|
|
return f"{parent}/{child_resource}"
|
|
|
|
|
|
def extract_tenant_id_from_route(route_path: str) -> Optional[str]:
|
|
"""
|
|
Extract tenant ID from a route path
|
|
|
|
Args:
|
|
route_path: Route path containing tenant ID
|
|
|
|
Returns:
|
|
Tenant ID if found, None otherwise
|
|
|
|
Example:
|
|
extract_tenant_id_from_route('/api/v1/tenants/uuid-123/inventory/ingredients')
|
|
# Returns: 'uuid-123'
|
|
"""
|
|
import re
|
|
match = re.search(r'/api/v1/tenants/([^/]+)/', route_path)
|
|
return match.group(1) if match else None
|
|
|
|
|
|
def extract_service_from_route(route_path: str) -> Optional[str]:
|
|
"""
|
|
Extract service name from a route path
|
|
|
|
Args:
|
|
route_path: Route path containing service name
|
|
|
|
Returns:
|
|
Service name if found, None otherwise
|
|
|
|
Example:
|
|
extract_service_from_route('/api/v1/tenants/uuid-123/inventory/ingredients')
|
|
# Returns: 'inventory'
|
|
"""
|
|
import re
|
|
match = re.search(r'/api/v1/tenants/[^/]+/([^/]+)/', route_path)
|
|
return match.group(1) if match else None
|
|
|
|
|
|
def is_analytics_route(route_path: str) -> bool:
|
|
"""Check if route is an analytics route"""
|
|
return '/analytics/' in route_path
|
|
|
|
|
|
def is_dashboard_route(route_path: str) -> bool:
|
|
"""Check if route is a dashboard route"""
|
|
return '/dashboard/' in route_path
|
|
|
|
|
|
def is_operations_route(route_path: str) -> bool:
|
|
"""Check if route is an operations route"""
|
|
return '/operations/' in route_path
|
|
|
|
|
|
def is_base_crud_route(route_path: str) -> bool:
|
|
"""Check if route is a base CRUD route"""
|
|
return not (is_analytics_route(route_path) or
|
|
is_dashboard_route(route_path) or
|
|
is_operations_route(route_path))
|