2024-01-04 09:00:41 -05:00
|
|
|
from abc import abstractmethod
|
2023-12-29 18:11:55 -05:00
|
|
|
from typing import Protocol
|
|
|
|
|
|
2023-08-10 18:02:45 -04:00
|
|
|
from botocore.config import Config
|
|
|
|
|
|
2024-01-10 12:32:25 -05:00
|
|
|
from app.enums import NotificationType
|
|
|
|
|
|
2023-08-10 18:02:45 -04:00
|
|
|
AWS_CLIENT_CONFIG = Config(
|
|
|
|
|
# This config is required to enable S3 to connect to FIPS-enabled
|
|
|
|
|
# endpoints. See https://aws.amazon.com/compliance/fips/ for more
|
|
|
|
|
# information.
|
|
|
|
|
s3={
|
2023-08-29 14:54:30 -07:00
|
|
|
"addressing_style": "virtual",
|
2023-08-10 18:02:45 -04:00
|
|
|
},
|
2023-08-29 14:54:30 -07:00
|
|
|
use_fips_endpoint=True,
|
2025-01-09 10:53:33 -08:00
|
|
|
max_pool_connections=50, # This should be equal or greater than our celery concurrency
|
2023-08-10 18:02:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2016-02-15 16:01:14 +00:00
|
|
|
class ClientException(Exception):
|
2023-08-29 14:54:30 -07:00
|
|
|
"""
|
2016-02-15 16:01:14 +00:00
|
|
|
Base Exceptions for sending notifications that fail
|
2023-08-29 14:54:30 -07:00
|
|
|
"""
|
|
|
|
|
|
2016-02-15 16:01:14 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2023-12-29 18:11:55 -05:00
|
|
|
class Client(Protocol):
|
2023-08-29 14:54:30 -07:00
|
|
|
"""
|
2016-02-15 16:01:14 +00:00
|
|
|
Base client for sending notifications.
|
2023-08-29 14:54:30 -07:00
|
|
|
"""
|
|
|
|
|
|
2024-01-04 09:00:41 -05:00
|
|
|
@abstractmethod
|
|
|
|
|
def init_app(self, current_app, *args, **kwargs):
|
|
|
|
|
raise NotImplementedError("TODO: Need to implement.")
|
2016-03-21 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
2020-11-17 12:35:18 +00:00
|
|
|
class NotificationProviderClients(object):
|
2016-05-06 09:47:06 +01:00
|
|
|
sms_clients = {}
|
|
|
|
|
email_clients = {}
|
|
|
|
|
|
|
|
|
|
def init_app(self, sms_clients, email_clients):
|
|
|
|
|
for client in sms_clients:
|
|
|
|
|
self.sms_clients[client.name] = client
|
|
|
|
|
|
|
|
|
|
for client in email_clients:
|
|
|
|
|
self.email_clients[client.name] = client
|
|
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
def get_sms_client(self, name):
|
2016-05-06 09:47:06 +01:00
|
|
|
return self.sms_clients.get(name)
|
|
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
def get_email_client(self, name):
|
2016-05-06 09:47:06 +01:00
|
|
|
return self.email_clients.get(name)
|
2016-05-10 09:04:22 +01:00
|
|
|
|
|
|
|
|
def get_client_by_name_and_type(self, name, notification_type):
|
2024-01-10 12:32:25 -05:00
|
|
|
assert notification_type in {
|
|
|
|
|
NotificationType.EMAIL,
|
|
|
|
|
NotificationType.SMS,
|
|
|
|
|
} # nosec B101
|
2022-08-19 14:32:11 +00:00
|
|
|
|
2024-01-10 11:18:33 -05:00
|
|
|
if notification_type == NotificationType.EMAIL:
|
2016-05-10 09:04:22 +01:00
|
|
|
return self.get_email_client(name)
|
|
|
|
|
|
2024-01-10 11:18:33 -05:00
|
|
|
if notification_type == NotificationType.SMS:
|
2016-05-10 09:04:22 +01:00
|
|
|
return self.get_sms_client(name)
|