Merge branch 'master' into celery-send-sms-code

Conflicts:
	tests/app/celery/test_tasks.py
This commit is contained in:
Rebecca Law
2016-02-17 15:43:57 +00:00
7 changed files with 74 additions and 11 deletions

View File

@@ -8,12 +8,14 @@ from werkzeug.local import LocalProxy
from utils import logging
from app.celery.celery import NotifyCelery
from app.clients.sms.twilio import TwilioClient
from app.clients.sms.firetext import FiretextClient
from app.encryption import Encryption
db = SQLAlchemy()
ma = Marshmallow()
notify_celery = NotifyCelery()
twilio_client = TwilioClient()
firetext_client = FiretextClient()
encryption = Encryption()
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
@@ -30,6 +32,7 @@ def create_app():
init_app(application)
logging.init_app(application)
twilio_client.init_app(application)
firetext_client.init_app(application)
notify_celery.init_app(application)
encryption.init_app(application)

View File

@@ -1,5 +1,5 @@
from app import notify_celery, twilio_client, encryption
from app.clients.sms.twilio import TwilioClientException
from app import notify_celery, encryption, firetext_client
from app.clients.sms.firetext import FiretextClientException
from app.dao.templates_dao import get_model_templates
from app.dao.notifications_dao import save_notification
from app.models import Notification
@@ -23,8 +23,8 @@ def send_sms(service_id, notification_id, encrypted_notification):
save_notification(notification_db_object)
try:
twilio_client.send_sms(notification['to'], template.content)
except TwilioClientException as e:
firetext_client.send_sms(notification['to'], template.content)
except FiretextClientException as e:
current_app.logger.debug(e)
save_notification(notification_db_object, {"status": "failed"})

View File

@@ -0,0 +1,52 @@
import logging
from app.clients.sms import (
SmsClient,
SmsClientException
)
from requests import request, RequestException, HTTPError
logger = logging.getLogger(__name__)
class FiretextClientException(SmsClientException):
pass
class FiretextClient(SmsClient):
'''
FireText sms client.
'''
def init_app(self, config, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.api_key = config.config.get('FIRETEXT_API_KEY')
self.from_number = config.config.get('FIRETEXT_NUMBER')
def send_sms(self, to, content):
data = {
"apiKey": self.api_key,
"from": self.from_number,
"to": to.replace('+', ''),
"message": content
}
try:
response = request(
"POST",
"https://www.firetext.co.uk/api/sendsms",
data=data
)
response.raise_for_status()
except RequestException as e:
api_error = HTTPError.create(e)
print(
"API {} request on {} failed with {} '{}'".format(
"POST",
"https://www.firetext.co.uk/api/sendsms",
api_error.status_code,
api_error.message
)
)
raise api_error
return response