from aiogoogle import Aiogoogle from aiogoogle.auth.creds import ServiceAccountCreds from ... import logger from ...environment_vars import YT_CMS_CREDENTIALS_PATH from . import helpers logger = logger.new_logger(__name__) def requires_init(func): """Decorator that ensures the client is initialized before executing the decorated method. """ async def wrapper(self, *args, **kwargs): cls = self.__class__ async with cls._init_lock: if not cls._api: logger.info("Initializing YouTube Content ID API client...") service_account_creds = ServiceAccountCreds( scopes=cls._scopes, **helpers.load_credentials(YT_CMS_CREDENTIALS_PATH), ) cls._aiogoogle = Aiogoogle(service_account_creds=service_account_creds) logger.info("Discovering YouTube Content ID API schema...") # Persist schema in class for performance. async with cls._aiogoogle: cls._api = await cls._aiogoogle.discover( cls._api_name, cls._api_version ) return await func(self, *args, **kwargs) return wrapper