2020-06-01 17:08:30 +01:00
|
|
|
import json
|
2021-03-10 13:55:06 +00:00
|
|
|
from time import monotonic
|
2020-06-01 17:08:30 +01:00
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
from requests import request
|
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.clients.email import EmailClient, EmailClientException
|
2020-06-01 17:08:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class AwsSesStubClientException(EmailClientException):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AwsSesStubClient(EmailClient):
|
2023-04-25 07:50:56 -07:00
|
|
|
def init_app(self, stub_url):
|
2020-06-01 17:08:30 +01:00
|
|
|
self.url = stub_url
|
|
|
|
|
|
2022-03-24 17:31:53 +00:00
|
|
|
@property
|
|
|
|
|
def name(self):
|
2023-08-29 14:54:30 -07:00
|
|
|
return "ses"
|
|
|
|
|
|
|
|
|
|
def send_email(
|
|
|
|
|
self, source, to_addresses, subject, body, html_body="", reply_to_address=None
|
|
|
|
|
):
|
2020-06-01 17:08:30 +01:00
|
|
|
try:
|
|
|
|
|
start_time = monotonic()
|
2023-08-29 14:54:30 -07:00
|
|
|
response = request("POST", self.url, data={"id": "dummy-data"}, timeout=60)
|
2020-06-01 17:08:30 +01:00
|
|
|
response.raise_for_status()
|
|
|
|
|
response_json = json.loads(response.text)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise AwsSesStubClientException(str(e))
|
|
|
|
|
else:
|
|
|
|
|
elapsed_time = monotonic() - start_time
|
2023-08-29 14:54:30 -07:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"AWS SES stub request finished in {}".format(elapsed_time)
|
|
|
|
|
)
|
|
|
|
|
return response_json["MessageId"]
|