From 5148e6428b50578005bb4caa991937f44c1bfc11 Mon Sep 17 00:00:00 2001 From: Philip Wafula Date: Wed, 19 May 2021 16:13:06 +0000 Subject: [PATCH] Philip/notify errors --- apps/cic-notify/cic_notify/error.py | 4 ++++ apps/cic-notify/cic_notify/ext/enums.py | 19 ++++++++++++++++++ .../cic_notify/tasks/sms/africastalking.py | 20 ++++++++++++++++++- apps/cic-notify/cic_notify/version.py | 2 +- docker-compose.yml | 6 +++--- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 apps/cic-notify/cic_notify/ext/enums.py diff --git a/apps/cic-notify/cic_notify/error.py b/apps/cic-notify/cic_notify/error.py index a2a68f6..a383512 100644 --- a/apps/cic-notify/cic_notify/error.py +++ b/apps/cic-notify/cic_notify/error.py @@ -9,3 +9,7 @@ class AlreadyInitializedError(Exception): class PleaseCommitFirstError(Exception): """Raised when there exists uncommitted changes in the code while trying to build out the package.""" pass + + +class NotificationSendError(Exception): + """Raised when a notification failed to due to some error as per the service responsible for dispatching the notification.""" diff --git a/apps/cic-notify/cic_notify/ext/enums.py b/apps/cic-notify/cic_notify/ext/enums.py new file mode 100644 index 0000000..e088818 --- /dev/null +++ b/apps/cic-notify/cic_notify/ext/enums.py @@ -0,0 +1,19 @@ +# standard imports +from enum import IntEnum + + +class AfricasTalkingStatusCodes(IntEnum): + PROCESSED = 100 + SENT = 101 + QUEUED = 102 + RISK_HOLD = 401 + INVALID_SENDER_ID = 402 + INVALID_PHONE_NUMBER = 403 + UNSUPPORTED_NUMBER_TYPE = 404 + INSUFFICIENT_BALANCE = 405 + USER_IN_BLACKLIST = 406 + COULD_NOT_ROUTE = 407 + INTERNAL_SERVER_ERROR = 500 + GATEWAY_ERROR = 501 + REJECTED_BY_GATEWAY = 502 + diff --git a/apps/cic-notify/cic_notify/tasks/sms/africastalking.py b/apps/cic-notify/cic_notify/tasks/sms/africastalking.py index 4ee9d47..af48ac5 100644 --- a/apps/cic-notify/cic_notify/tasks/sms/africastalking.py +++ b/apps/cic-notify/cic_notify/tasks/sms/africastalking.py @@ -6,7 +6,8 @@ import celery import africastalking # local imports -from cic_notify.error import NotInitializedError, AlreadyInitializedError +from cic_notify.error import NotInitializedError, AlreadyInitializedError, NotificationSendError +from cic_notify.ext.enums import AfricasTalkingStatusCodes logg = logging.getLogger() celery_app = celery.current_app @@ -50,10 +51,27 @@ class AfricasTalkingNotifier: if self.sender_id: response = self.api_client.send(message=message, recipients=[recipient], sender_id=self.sender_id) logg.debug(f'Africastalking response sender-id {response}') + else: response = self.api_client.send(message=message, recipients=[recipient]) logg.debug(f'africastalking response no-sender-id {response}') + recipients = response.get('Recipients') + + if len(recipients) != 1: + status = response.get('SMSMessageData').get('Message') + raise NotificationSendError(f'Unexpected number of recipients: {len(recipients)}. Status: {status}') + + status_code = recipients[0].get('statusCode') + status = recipients[0].get('status') + + if status_code not in [ + AfricasTalkingStatusCodes.PROCESSED.value, + AfricasTalkingStatusCodes.SENT.value, + AfricasTalkingStatusCodes.QUEUED.value + ]: + raise NotificationSendError(f'Sending notification failed due to: {status}') + @celery_app.task def send(message, recipient): diff --git a/apps/cic-notify/cic_notify/version.py b/apps/cic-notify/cic_notify/version.py index a79e614..e795aad 100644 --- a/apps/cic-notify/cic_notify/version.py +++ b/apps/cic-notify/cic_notify/version.py @@ -9,7 +9,7 @@ import semver logg = logging.getLogger() -version = (0, 4, 0, 'alpha.4') +version = (0, 4, 0, 'alpha.5') version_object = semver.VersionInfo( major=version[0], diff --git a/docker-compose.yml b/docker-compose.yml index 783ff74..1a1eae3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -446,9 +446,9 @@ services: PGPASSWORD: ${DATABASE_PASSWORD:-tralala} CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://redis} CELERY_RESULT_URL: ${CELERY_BROKER_URL:-redis://redis} - TASKS_AFRICASTALKING: $TASKS_AFRICASTALKING - TASKS_SMS_DB: $TASKS_SMS_DB - TASKS_LOG: $TASKS_LOG + AFRICASTALKING_API_USERNAME: $AFRICASTALKING_API_USERNAME + AFRICASTALKING_API_KEY: $AFRICASTALKING_API_KEY + AFRICASTALKING_API_SENDER_ID: $AFRICASTALKING_API_SENDER_ID depends_on: - postgres - redis