DRY-up logging and metrics for sending SMS

This avoids duplicating it as we add a new provider and means we
can test it all in one place (although it wasn't tested before).

I'm not sure why the previous code did "super(..)__init__" in a
non-init function - it's a bit late! - so I've just replaced it
with a call to the new "init_app" function in the parent class.
This commit is contained in:
Ben Thorner
2022-03-25 13:05:23 +00:00
parent 84578e8a1d
commit 3b082477f0
6 changed files with 112 additions and 66 deletions

View File

@@ -1,3 +1,5 @@
from time import monotonic
from app.clients import Client, ClientException
@@ -18,6 +20,10 @@ class SmsClient(Client):
Base Sms client for sending smss.
'''
def init_app(self, current_app, statsd_client):
self.current_app = current_app
self.statsd_client = statsd_client
def record_outcome(self, success):
log_message = "Provider request for {} {}".format(
self.name,
@@ -31,7 +37,23 @@ class SmsClient(Client):
self.statsd_client.incr(f"clients.{self.name}.error")
self.current_app.logger.warning(log_message)
def send_sms(self, *args, **kwargs):
def send_sms(self, to, content, reference, international, sender=None):
start_time = monotonic()
try:
response = self.try_send_sms(to, content, reference, international, sender)
self.record_outcome(True)
except SmsClientResponseException as e:
self.record_outcome(False)
raise e
finally:
elapsed_time = monotonic() - start_time
self.statsd_client.timing(f"clients.{self.name}.request-time", elapsed_time)
self.current_app.logger.info("Reach request for {} finished in {}".format(reference, elapsed_time))
return response
def try_send_sms(self, *args, **kwargs):
raise NotImplementedError('TODO Need to implement.')
@property

View File

@@ -1,6 +1,5 @@
import json
import logging
from time import monotonic
from requests import RequestException, request
@@ -62,20 +61,18 @@ class FiretextClient(SmsClient):
FireText sms client.
'''
def init_app(self, current_app, statsd_client, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.current_app = current_app
self.api_key = current_app.config.get('FIRETEXT_API_KEY')
self.international_api_key = current_app.config.get('FIRETEXT_INTERNATIONAL_API_KEY')
self.from_number = current_app.config.get('FROM_NUMBER')
self.url = current_app.config.get('FIRETEXT_URL')
self.statsd_client = statsd_client
def init_app(self, *args, **kwargs):
super().init_app(*args, **kwargs)
self.api_key = self.current_app.config.get('FIRETEXT_API_KEY')
self.international_api_key = self.current_app.config.get('FIRETEXT_INTERNATIONAL_API_KEY')
self.from_number = self.current_app.config.get('FROM_NUMBER')
self.url = self.current_app.config.get('FIRETEXT_URL')
@property
def name(self):
return 'firetext'
def send_sms(self, to, content, reference, international, sender=None):
def try_send_sms(self, to, content, reference, international, sender=None):
data = {
"apiKey": self.international_api_key if international else self.api_key,
"from": self.from_number if sender is None else sender,
@@ -84,7 +81,6 @@ class FiretextClient(SmsClient):
"reference": reference
}
start_time = monotonic()
try:
response = request(
"POST",
@@ -98,14 +94,8 @@ class FiretextClient(SmsClient):
if response.json()['code'] != 0:
raise ValueError()
except (ValueError, AttributeError) as e:
self.record_outcome(False)
raise FiretextClientResponseException(response=response, exception=e)
self.record_outcome(True)
except RequestException as e:
self.record_outcome(False)
raise FiretextClientResponseException(response=e.response, exception=e)
finally:
elapsed_time = monotonic() - start_time
self.current_app.logger.info("Firetext request for {} finished in {}".format(reference, elapsed_time))
self.statsd_client.timing("clients.firetext.request-time", elapsed_time)
return response

View File

@@ -1,5 +1,4 @@
import json
from time import monotonic
from requests import RequestException, request
@@ -69,19 +68,17 @@ class MMGClient(SmsClient):
MMG sms client
'''
def init_app(self, current_app, statsd_client, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.current_app = current_app
self.api_key = current_app.config.get('MMG_API_KEY')
self.from_number = current_app.config.get('FROM_NUMBER')
self.statsd_client = statsd_client
self.mmg_url = current_app.config.get('MMG_URL')
def init_app(self, *args, **kwargs):
super().init_app(*args, **kwargs)
self.api_key = self.current_app.config.get('MMG_API_KEY')
self.from_number = self.current_app.config.get('FROM_NUMBER')
self.mmg_url = self.current_app.config.get('MMG_URL')
@property
def name(self):
return 'mmg'
def send_sms(self, to, content, reference, international, sender=None):
def try_send_sms(self, to, content, reference, international, sender=None):
data = {
"reqType": "BULK",
"MSISDN": to,
@@ -91,7 +88,6 @@ class MMGClient(SmsClient):
"multi": True
}
start_time = monotonic()
try:
response = request(
"POST",
@@ -108,15 +104,8 @@ class MMGClient(SmsClient):
try:
json.loads(response.text)
except (ValueError, AttributeError) as e:
self.record_outcome(False)
raise MMGClientResponseException(response=response, exception=e)
self.record_outcome(True)
except RequestException as e:
self.record_outcome(False)
raise MMGClientResponseException(response=e.response, exception=e)
finally:
elapsed_time = monotonic() - start_time
self.statsd_client.timing("clients.mmg.request-time", elapsed_time)
self.current_app.logger.info("MMG request for {} finished in {}".format(reference, elapsed_time))
return response