From 226459132a6fa3a813213d8149a757bec93169af Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 17 Feb 2016 12:57:51 +0000 Subject: [PATCH 1/4] Basic (Very basic) implementation of the fire text API. [https://www.firetext.co.uk/docs#sendingsms](https://www.firetext.co.uk/docs#sendingsms) Not to be merged. This API has a limit on it at the moment that will need to be removed before it is used in anger. --- app/__init__.py | 3 ++ app/celery/tasks.py | 3 +- app/clients/sms/firetext.py | 55 +++++++++++++++++++++++++++++++++++++ config.py | 2 ++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/clients/sms/firetext.py diff --git a/app/__init__.py b/app/__init__.py index 6b85f33ab..50c1691f4 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 3b095d062..50a99495b 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -1,4 +1,4 @@ -from app import notify_celery, twilio_client, encryption +from app import notify_celery, twilio_client, encryption, firetext_client from app.clients.sms.twilio import TwilioClientException from app.dao.templates_dao import get_model_templates from app.dao.notifications_dao import save_notification @@ -24,6 +24,7 @@ def send_sms(service_id, notification_id, encrypted_notification): try: twilio_client.send_sms(notification['to'], template.content) + firetext_client.send_sms(notification['to'], template.content) except TwilioClientException as e: current_app.logger.debug(e) save_notification(notification_db_object, {"status": "failed"}) diff --git a/app/clients/sms/firetext.py b/app/clients/sms/firetext.py new file mode 100644 index 000000000..37f986232 --- /dev/null +++ b/app/clients/sms/firetext.py @@ -0,0 +1,55 @@ +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) + print(config.config) + 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 + } + + print(data) + + 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 diff --git a/config.py b/config.py index 241791ec3..5115ae7cf 100644 --- a/config.py +++ b/config.py @@ -41,6 +41,8 @@ class Config(object): TWILIO_ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID') TWILIO_AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN') TWILIO_NUMBER = os.getenv('TWILIO_NUMBER') + FIRETEXT_NUMBER = os.getenv('FIRETEXT_NUMBER') + FIRETEXT_API_KEY = os.getenv("FIRETEXT_API_KEY") class Development(Config): From 837c9b7cdbff32a4cfb5cba40ac200f2ff2214b7 Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 17 Feb 2016 13:59:01 +0000 Subject: [PATCH 2/4] Removed logging, and make fire text only client. --- app/celery/tasks.py | 7 +++---- app/clients/sms/firetext.py | 3 --- tests/app/celery/test_tasks.py | 16 ++++++++-------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 50a99495b..0fae1b7c3 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -1,5 +1,5 @@ -from app import notify_celery, twilio_client, encryption, firetext_client -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,9 +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) firetext_client.send_sms(notification['to'], template.content) - except TwilioClientException as e: + except FiretextClientException as e: current_app.logger.debug(e) save_notification(notification_db_object, {"status": "failed"}) diff --git a/app/clients/sms/firetext.py b/app/clients/sms/firetext.py index 37f986232..1c22cb8dd 100644 --- a/app/clients/sms/firetext.py +++ b/app/clients/sms/firetext.py @@ -19,7 +19,6 @@ class FiretextClient(SmsClient): def init_app(self, config, *args, **kwargs): super(SmsClient, self).__init__(*args, **kwargs) - print(config.config) self.api_key = config.config.get('FIRETEXT_API_KEY') self.from_number = config.config.get('FIRETEXT_NUMBER') @@ -32,8 +31,6 @@ class FiretextClient(SmsClient): "message": content } - print(data) - try: response = request( "POST", diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index c53ae2469..bbf71c066 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -1,8 +1,8 @@ import uuid import pytest from app.celery.tasks import send_sms -from app import twilio_client -from app.clients.sms.twilio import TwilioClientException +from app import firetext_client +from app.clients.sms.firetext import FiretextClientException from app.dao import notifications_dao from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm.exc import NoResultFound @@ -14,7 +14,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat "to": "+441234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.twilio_client.send_sms') + mocker.patch('app.firetext_client.send_sms') notification_id = uuid.uuid4() @@ -23,7 +23,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat notification_id, "encrypted-in-reality") - twilio_client.send_sms.assert_called_once_with("+441234123123", sample_template.content) + firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content) persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id) assert persisted_notification.id == notification_id assert persisted_notification.to == '+441234123123' @@ -37,7 +37,7 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa "to": "+441234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.twilio_client.send_sms', side_effect=TwilioClientException()) + mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException()) notification_id = uuid.uuid4() @@ -46,7 +46,7 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa notification_id, "encrypted-in-reality") - twilio_client.send_sms.assert_called_once_with("+441234123123", sample_template.content) + firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content) persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id) assert persisted_notification.id == notification_id assert persisted_notification.to == '+441234123123' @@ -60,7 +60,7 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker): "to": "+441234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.twilio_client.send_sms', side_effect=TwilioClientException()) + mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException()) mocker.patch('app.db.session.add', side_effect=SQLAlchemyError()) notification_id = uuid.uuid4() @@ -70,7 +70,7 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker): notification_id, "encrypted-in-reality") - twilio_client.send_sms.assert_not_called() + firetext_client.send_sms.assert_not_called() with pytest.raises(NoResultFound) as e: notifications_dao.get_notification(sample_template.service_id, notification_id) assert 'No row was found for one' in str(e.value) From 36eee56f17fde0d96e9322b42f2943927e56b78b Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 17 Feb 2016 14:03:40 +0000 Subject: [PATCH 3/4] Adding boto for python2 as Celery seems to need it. --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 184d57249..cca101658 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ itsdangerous==0.24 Flask-Bcrypt==0.6.2 credstash==1.8.0 boto3==1.2.3 +boto==2.39.0 celery==3.1.20 twilio==4.6.0 From 53bdedf8f37ff799eebbde0b998892cc5af4ac4d Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 17 Feb 2016 14:27:28 +0000 Subject: [PATCH 4/4] Test properties for firetext --- environment_test.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/environment_test.sh b/environment_test.sh index 2be312597..bbc44a855 100644 --- a/environment_test.sh +++ b/environment_test.sh @@ -12,4 +12,6 @@ export SQLALCHEMY_DATABASE_URI='postgresql://localhost/test_notification_api' export VERIFY_CODE_FROM_EMAIL_ADDRESS='no-reply@notify.works' export TWILIO_ACCOUNT_SID="test" export TWILIO_AUTH_TOKEN="test" -export TWILIO_NUMBER="test" \ No newline at end of file +export TWILIO_NUMBER="test" +export FIRETEXT_API_KEY="Firetext" +export FIRETEXT_NUMBER="Firetext" \ No newline at end of file