import json import logging from typing import Any, Dict, Optional, Union import boto3 from botocore.exceptions import ClientError from tests import config def get_secret( secret_name: str, region_name: str = config.AWS_REGION ) -> Optional[Dict[str, Any]]: """ Retrieve and return the secret as a dictionary. """ session = boto3.session.Session() client = session.client('secretsmanager', region_name=region_name) try: secret = client.get_secret_value(SecretId=secret_name)['SecretString'] secret_data: Dict[str, Any] = json.loads(secret) return secret_data except ClientError as e: logging.error(f'Error retrieving secret: {e}') raise def get_m2m_access_token() -> Optional[str]: secret = get_secret(secret_name=config.M2M_TOKEN_SECRET_NAME) if secret and 'token' in secret: return str(secret['token']) return None