Initial set up for mmg client integration

This commit is contained in:
Rebecca Law
2016-04-01 16:42:31 +01:00
parent 9946427eb6
commit 69697388d7
4 changed files with 68 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ from flask_marshmallow import Marshmallow
from werkzeug.local import LocalProxy from werkzeug.local import LocalProxy
from utils import logging from utils import logging
from app.celery.celery import NotifyCelery from app.celery.celery import NotifyCelery
from app.clients.sms.mmg import MMGClient
from app.clients.sms.twilio import TwilioClient from app.clients.sms.twilio import TwilioClient
from app.clients.sms.firetext import FiretextClient from app.clients.sms.firetext import FiretextClient
from app.clients.email.aws_ses import AwsSesClient from app.clients.email.aws_ses import AwsSesClient
@@ -21,6 +22,7 @@ ma = Marshmallow()
notify_celery = NotifyCelery() notify_celery = NotifyCelery()
twilio_client = TwilioClient() twilio_client = TwilioClient()
firetext_client = FiretextClient() firetext_client = FiretextClient()
mmg_client = MMGClient()
aws_ses_client = AwsSesClient() aws_ses_client = AwsSesClient()
encryption = Encryption() encryption = Encryption()
@@ -42,6 +44,7 @@ def create_app(app_name=None):
logging.init_app(application) logging.init_app(application)
twilio_client.init_app(application) twilio_client.init_app(application)
firetext_client.init_app(application) firetext_client.init_app(application)
mmg_client.init_app(application.config)
aws_ses_client.init_app(application.config['AWS_REGION']) aws_ses_client.init_app(application.config['AWS_REGION'])
notify_celery.init_app(application) notify_celery.init_app(application)
encryption.init_app(application) encryption.init_app(application)

View File

@@ -22,7 +22,8 @@ from app import (
notify_celery, notify_celery,
encryption, encryption,
firetext_client, firetext_client,
aws_ses_client aws_ses_client,
mmg_client
) )
from app.aws import s3 from app.aws import s3
@@ -312,12 +313,20 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not
@notify_celery.task(name='send-sms-code') @notify_celery.task(name='send-sms-code')
def send_sms_code(encrypted_verification): def send_sms_code(encrypted_verification):
verification_message = encryption.decrypt(encrypted_verification) verification_message = encryption.decrypt(encrypted_verification)
# send_sms_via_firetext(validate_and_format_phone_number(verification_message['to']),
# verification_message['secret_code'],
# 'send-sms-code')
try: try:
firetext_client.send_sms( mmg_client.send_sms(validate_and_format_phone_number(verification_message['to']),
validate_and_format_phone_number(verification_message['to']), verification_message['secret_code'],
verification_message['secret_code'], 'send-sms-code')
'send-sms-code' except Exception as e:
) current_app.logger.exception(e)
def send_sms_via_firetext(to, content, reference):
try:
firetext_client.send_sms(to=to, content=content, reference=reference)
except FiretextClientException as e: except FiretextClientException as e:
current_app.logger.exception(e) current_app.logger.exception(e)

48
app/clients/sms/mmg.py Normal file
View File

@@ -0,0 +1,48 @@
from flask import current_app
from monotonic import monotonic
from requests import request, RequestException, HTTPError
from app.clients.sms import SmsClient
class MMGClient(SmsClient):
'''
MMG sms client
'''
def init_app(self, config, *args, **kwargs):
super(SmsClient, self).__init__(*args, *kwargs)
self.api_key = config.get('MMG_API_KEY')
self.from_number = config.get('NOTIFY_FROM_NUMBER')
self.name = 'mmg'
def get_name(self):
return self.name
def send_sms(self, to, content, reference):
data = {
"reqType": "BULK",
"MSISDN": to,
"msg": content,
"sender": self.from_number
}
start_time = monotonic()
try:
response = request("POST", "https://www.mmgrp.co.uk/API/json/api.php",
data=data)
except RequestException as e:
api_error = HTTPError.create(e)
current_app.logger.error(
"API {} request on {} failed with {} '{}'".format(
"POST",
"https://www.mmgrp.co.uk/API/json/api.php",
api_error.status_code,
api_error.message
)
)
raise api_error
finally:
elapsed_time = monotonic() - start_time
current_app.logger.info("MMG request finished in {}".format(elapsed_time))
return response

View File

@@ -17,12 +17,14 @@ class Config(object):
NOTIFY_JOB_QUEUE = os.environ['NOTIFY_JOB_QUEUE'] NOTIFY_JOB_QUEUE = os.environ['NOTIFY_JOB_QUEUE']
# Notification Queue names are a combination of a prefx plus a name # Notification Queue names are a combination of a prefx plus a name
NOTIFICATION_QUEUE_PREFIX = os.environ['NOTIFICATION_QUEUE_PREFIX'] NOTIFICATION_QUEUE_PREFIX = os.environ['NOTIFICATION_QUEUE_PREFIX']
MMG_API_KEY = os.environ['MMG_API_KEY']
SECRET_KEY = os.environ['SECRET_KEY'] SECRET_KEY = os.environ['SECRET_KEY']
SQLALCHEMY_COMMIT_ON_TEARDOWN = False SQLALCHEMY_COMMIT_ON_TEARDOWN = False
SQLALCHEMY_DATABASE_URI = os.environ['SQLALCHEMY_DATABASE_URI'] SQLALCHEMY_DATABASE_URI = os.environ['SQLALCHEMY_DATABASE_URI']
SQLALCHEMY_RECORD_QUERIES = True SQLALCHEMY_RECORD_QUERIES = True
VERIFY_CODE_FROM_EMAIL_ADDRESS = os.environ['VERIFY_CODE_FROM_EMAIL_ADDRESS'] VERIFY_CODE_FROM_EMAIL_ADDRESS = os.environ['VERIFY_CODE_FROM_EMAIL_ADDRESS']
NOTIFY_EMAIL_DOMAIN = os.environ['NOTIFY_EMAIL_DOMAIN'] NOTIFY_EMAIL_DOMAIN = os.environ['NOTIFY_EMAIL_DOMAIN']
NOTIFY_FROM_NUMBER= os.environ['NOTIFY_FROM_NUMBER']
PAGE_SIZE = 50 PAGE_SIZE = 50
BROKER_URL = 'sqs://' BROKER_URL = 'sqs://'