"""Utility Functions for Handlers.""" from functools import wraps from typing import Any, Callable from flask import Response, request from owsrequest import flask_request from owsresponse.adaptors.flask import flaskify from owsresponse.response import Response as OwsResponse from carveouts.models import ows_product def verify_grass_headers( function: Callable[..., Any], ) -> Callable[[tuple[Any, ...], dict[str, Any]], Response]: """Verify grass headers as a Decorator. Returns: callable: the wrapped function. """ @wraps(function) def wrapper(*args: Any, **kwargs: Any) -> Response | Any: headers_response = flask_request.verify_grass_headers(request) if headers_response.status != 200: return flaskify( OwsResponse( message=(headers_response.errors or headers_response.message), status=headers_response.status, ), headers=request.headers.__dict__, ) return function(*args, **kwargs) return wrapper def verify_grass_ownership( function: Callable[..., Any], ) -> Callable[[tuple[Any, ...], dict[str, Any]], Response]: """Verify ownership of the product as Decorator. Returns: callable: the wrapped function. """ @wraps(function) def wrapper(*args: Any, **kwargs: Any) -> Response | Any: ownership_response = flask_request.verify_grass_ownership( request, ows_product.check_ownership, *args, **kwargs ) if ownership_response.status != 200: return flaskify( OwsResponse( message=ownership_response.data.decode("utf-8"), status=ownership_response.status, ), headers=request.headers.__dict__, ) return function(*args, **kwargs) return wrapper