2017-03-16 18:15:49 +00:00
|
|
|
from flask import Blueprint
|
2017-07-12 14:19:39 +01:00
|
|
|
from flask import current_app
|
2017-03-16 18:15:49 +00:00
|
|
|
from flask import json
|
|
|
|
|
from flask import request, jsonify
|
|
|
|
|
|
2020-03-17 15:15:43 +00:00
|
|
|
from app.celery.process_sms_client_response_tasks import process_sms_client_response
|
|
|
|
|
from app.config import QueueNames
|
2017-03-16 18:15:49 +00:00
|
|
|
from app.errors import InvalidRequest, register_errors
|
|
|
|
|
|
2017-03-17 16:21:41 +00:00
|
|
|
sms_callback_blueprint = Blueprint("sms_callback", __name__, url_prefix="/notifications/sms")
|
2017-03-16 18:15:49 +00:00
|
|
|
register_errors(sms_callback_blueprint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@sms_callback_blueprint.route('/mmg', methods=['POST'])
|
|
|
|
|
def process_mmg_response():
|
|
|
|
|
client_name = 'MMG'
|
|
|
|
|
data = json.loads(request.data)
|
|
|
|
|
errors = validate_callback_data(data=data,
|
|
|
|
|
fields=['status', 'CID'],
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
if errors:
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
2020-03-17 15:15:43 +00:00
|
|
|
status = str(data.get('status'))
|
2020-06-01 11:45:35 +01:00
|
|
|
detailed_status_code = str(data.get('substatus'))
|
2020-05-27 18:03:55 +01:00
|
|
|
|
2020-03-17 15:15:43 +00:00
|
|
|
provider_reference = data.get('CID')
|
|
|
|
|
|
|
|
|
|
process_sms_client_response.apply_async(
|
2020-06-01 11:45:35 +01:00
|
|
|
[status, provider_reference, client_name, detailed_status_code],
|
2020-03-17 15:15:43 +00:00
|
|
|
queue=QueueNames.SMS_CALLBACKS,
|
|
|
|
|
)
|
2017-07-12 14:19:39 +01:00
|
|
|
|
2017-07-12 15:32:59 +01:00
|
|
|
safe_to_log = data.copy()
|
|
|
|
|
safe_to_log.pop("MSISDN")
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
current_app.logger.debug(
|
2020-03-17 15:15:43 +00:00
|
|
|
f"Full delivery response from {client_name} for notification: {provider_reference}\n{safe_to_log}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return jsonify(result='success'), 200
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@sms_callback_blueprint.route('/firetext', methods=['POST'])
|
|
|
|
|
def process_firetext_response():
|
|
|
|
|
client_name = 'Firetext'
|
|
|
|
|
errors = validate_callback_data(data=request.form,
|
|
|
|
|
fields=['status', 'reference'],
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
if errors:
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2020-03-17 15:15:43 +00:00
|
|
|
|
|
|
|
|
status = request.form.get('status')
|
2020-06-01 11:45:35 +01:00
|
|
|
detailed_status_code = request.form.get('code')
|
2020-03-17 15:15:43 +00:00
|
|
|
provider_reference = request.form.get('reference')
|
|
|
|
|
|
2017-07-12 15:32:59 +01:00
|
|
|
safe_to_log = dict(request.form).copy()
|
|
|
|
|
safe_to_log.pop('mobile')
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
current_app.logger.debug(
|
2020-03-17 15:15:43 +00:00
|
|
|
f"Full delivery response from {client_name} for notification: {provider_reference}\n{safe_to_log}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
process_sms_client_response.apply_async(
|
2020-06-01 11:45:35 +01:00
|
|
|
[status, provider_reference, client_name, detailed_status_code],
|
2020-03-17 15:15:43 +00:00
|
|
|
queue=QueueNames.SMS_CALLBACKS,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return jsonify(result='success'), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_callback_data(data, fields, client_name):
|
|
|
|
|
errors = []
|
|
|
|
|
for f in fields:
|
|
|
|
|
if not str(data.get(f, '')):
|
|
|
|
|
error = "{} callback failed: {} missing".format(client_name, f)
|
|
|
|
|
errors.append(error)
|
|
|
|
|
return errors if len(errors) > 0 else None
|