Merge branch 'philip/notify-errors' into 'master'
Philip/notify errors Closes cic-notify#4 See merge request grassrootseconomics/cic-internal-integration!161
This commit is contained in:
		
						commit
						db2ec0dcfa
					
				@ -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."""
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								apps/cic-notify/cic_notify/ext/enums.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								apps/cic-notify/cic_notify/ext/enums.py
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
 | 
			
		||||
@ -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):
 | 
			
		||||
 | 
			
		||||
@ -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],
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user