from fansifter_common.adapters.sendgrid import SendGridClient from fansifter_common.adapters.sendgrid.exceptions import SendGridClientError from fansifter_common.utils.functional import lazy_proxy from app.config import settings def get_sendgrid_client() -> SendGridClient: client = SendGridClient( sme_api_key=settings.sendgrid_sme_api_key, orchard_api_key=settings.sendgrid_orchard_api_key, awal_api_key=settings.sendgrid_awal_api_key, unsubscribe_host=settings.unsubscribe_host, preference_center_url=settings.preference_center_url, reply_to_sme_domain=settings.reply_to_sme_domain, reply_to_orchard_domain=settings.reply_to_orchard_domain, reply_to_awal_domain=settings.reply_to_awal_domain, reply_to_header_enabled=settings.reply_to_header_enabled, reply_to_secret_key=settings.reply_to_secret_key, ) client.start() return client sendgrid_client = lazy_proxy(get_sendgrid_client) def get_error_log_msg(exc: SendGridClientError) -> str: if exc.response_obj and exc.response_obj.errors: error = exc.response_obj.errors[0] if error.field: return f"SendGridClientError: `{error.field}` {error.message}" return f"SendGridClientError: {error.message}" return exc.log_msg