mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Stub SES email client to avoid hitting SES during load testing
If we set an environment variable, we can stub out calls to SES and send them to our own stub app. If the environment variable is not set, things work as normal. To be used alongside https://github.com/alphagov/notifications-email-provider-stub
This commit is contained in:
49
app/clients/email/aws_ses_stub.py
Normal file
49
app/clients/email/aws_ses_stub.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import json
|
||||
|
||||
from flask import current_app
|
||||
from requests import request
|
||||
from time import monotonic
|
||||
|
||||
from app.clients.email import (EmailClientException, EmailClient)
|
||||
|
||||
|
||||
class AwsSesStubClientException(EmailClientException):
|
||||
pass
|
||||
|
||||
|
||||
class AwsSesStubClient(EmailClient):
|
||||
def init_app(self, region, statsd_client, stub_url):
|
||||
self.name = 'ses'
|
||||
self.statsd_client = statsd_client
|
||||
self.url = stub_url
|
||||
|
||||
def get_name(self):
|
||||
return self.name
|
||||
|
||||
def send_email(self,
|
||||
source,
|
||||
to_addresses,
|
||||
subject,
|
||||
body,
|
||||
html_body='',
|
||||
reply_to_address=None):
|
||||
try:
|
||||
start_time = monotonic()
|
||||
response = request(
|
||||
"POST",
|
||||
self.url,
|
||||
data={"id": "dummy-data"},
|
||||
timeout=60
|
||||
)
|
||||
response.raise_for_status()
|
||||
response_json = json.loads(response.text)
|
||||
|
||||
except Exception as e:
|
||||
self.statsd_client.incr("clients.ses_stub.error")
|
||||
raise AwsSesStubClientException(str(e))
|
||||
else:
|
||||
elapsed_time = monotonic() - start_time
|
||||
current_app.logger.info("AWS SES stub request finished in {}".format(elapsed_time))
|
||||
self.statsd_client.timing("clients.ses_stub.request-time", elapsed_time)
|
||||
self.statsd_client.incr("clients.ses_stub.success")
|
||||
return response_json['MessageId']
|
||||
Reference in New Issue
Block a user