"""Shared helpers for API routers.""" import yaml from ninja.errors import HttpError from privatestudio.studio.models import Studio def get_studio_for_user(user, bucket: str) -> Studio: """Resolve a bucket name to a Studio the user may access (404/403 otherwise).""" studio = Studio.objects.filter(bucket_name=bucket).first() if studio is None: raise HttpError(404, f"Studio '{bucket}' not found") if not studio.user_has_access(user): raise HttpError(403, f"No access to studio '{bucket}'") return studio def parse_yaml_body(request) -> dict: """Parse the raw request body as a YAML mapping (400 on failure).""" try: data = yaml.safe_load(request.body.decode("utf-8") or "") except (yaml.YAMLError, UnicodeDecodeError) as e: raise HttpError(400, f"Invalid YAML: {e}") if not isinstance(data, dict): raise HttpError(400, "Request body must be a YAML mapping") return data