2016-04-18 09:55:56 +01:00
|
|
|
import json
|
2016-04-01 16:42:31 +01:00
|
|
|
from monotonic import monotonic
|
2016-09-22 17:18:05 +01:00
|
|
|
from requests import (request, RequestException)
|
|
|
|
|
|
2016-04-06 14:31:33 +01:00
|
|
|
from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE)
|
2016-09-22 17:18:05 +01:00
|
|
|
from app.clients.sms import (SmsClient, SmsClientResponseException)
|
2016-04-04 15:02:21 +01:00
|
|
|
|
2016-04-06 14:31:33 +01:00
|
|
|
mmg_response_map = {
|
2016-05-19 11:27:22 +01:00
|
|
|
'2': {
|
2016-09-01 10:23:37 +01:00
|
|
|
"message": ' Permanent failure',
|
2016-05-19 11:27:22 +01:00
|
|
|
"notification_statistics_status": STATISTICS_FAILURE,
|
|
|
|
|
"success": False,
|
2016-09-01 10:23:37 +01:00
|
|
|
"notification_status": 'permanent-failure'
|
2016-05-19 11:27:22 +01:00
|
|
|
},
|
2016-04-20 14:29:31 +01:00
|
|
|
'3': {
|
2016-04-06 14:31:33 +01:00
|
|
|
"message": 'Delivered',
|
|
|
|
|
"notification_statistics_status": STATISTICS_DELIVERED,
|
|
|
|
|
"success": True,
|
|
|
|
|
"notification_status": 'delivered'
|
|
|
|
|
},
|
2016-05-19 11:27:22 +01:00
|
|
|
'4': {
|
|
|
|
|
"message": ' Temporary failure',
|
|
|
|
|
"notification_statistics_status": STATISTICS_FAILURE,
|
|
|
|
|
"success": False,
|
|
|
|
|
"notification_status": 'temporary-failure'
|
|
|
|
|
},
|
|
|
|
|
'5': {
|
|
|
|
|
"message": 'Permanent failure',
|
|
|
|
|
"notification_statistics_status": STATISTICS_FAILURE,
|
|
|
|
|
"success": False,
|
|
|
|
|
"notification_status": 'permanent-failure'
|
|
|
|
|
},
|
2016-04-06 14:31:33 +01:00
|
|
|
'default': {
|
|
|
|
|
"message": 'Declined',
|
|
|
|
|
"success": False,
|
|
|
|
|
"notification_statistics_status": STATISTICS_FAILURE,
|
|
|
|
|
"notification_status": 'failed'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_mmg_responses(status):
|
|
|
|
|
return mmg_response_map.get(status, mmg_response_map.get('default'))
|
2016-04-04 15:02:21 +01:00
|
|
|
|
|
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
class MMGClientResponseException(SmsClientResponseException):
|
|
|
|
|
def __init__(self, response, exception):
|
|
|
|
|
self.status_code = response.status_code
|
|
|
|
|
self.text = response.text
|
|
|
|
|
self.exception = exception
|
2016-04-04 15:02:21 +01:00
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-09-22 17:18:05 +01:00
|
|
|
return "Code {} text {} exception {}".format(self.status_code, self.text, str(self.exception))
|
2016-04-01 16:42:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MMGClient(SmsClient):
|
|
|
|
|
'''
|
|
|
|
|
MMG sms client
|
|
|
|
|
'''
|
|
|
|
|
|
2016-06-01 15:24:19 +01:00
|
|
|
def init_app(self, current_app, statsd_client, *args, **kwargs):
|
2016-04-07 10:53:59 +01:00
|
|
|
super(SmsClient, self).__init__(*args, **kwargs)
|
2016-06-01 15:24:19 +01:00
|
|
|
self.current_app = current_app
|
|
|
|
|
self.api_key = current_app.config.get('MMG_API_KEY')
|
2016-06-06 09:49:51 +01:00
|
|
|
self.from_number = current_app.config.get('FROM_NUMBER')
|
2016-04-21 11:37:38 +01:00
|
|
|
self.name = 'mmg'
|
2016-05-13 17:15:39 +01:00
|
|
|
self.statsd_client = statsd_client
|
2016-07-20 10:40:50 +01:00
|
|
|
self.mmg_url = current_app.config.get('MMG_URL')
|
2016-04-01 16:42:31 +01:00
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
def record_outcome(self, success, response):
|
2016-09-23 10:24:12 +01:00
|
|
|
log_message = "API {} request {} on {} response status_code {}".format(
|
2016-09-22 17:18:05 +01:00
|
|
|
"POST",
|
|
|
|
|
"succeeded" if success else "failed",
|
|
|
|
|
self.mmg_url,
|
2016-09-23 10:24:12 +01:00
|
|
|
response.status_code
|
2016-09-22 17:18:05 +01:00
|
|
|
)
|
|
|
|
|
|
2016-09-23 15:59:23 +01:00
|
|
|
if success:
|
2016-09-22 17:18:05 +01:00
|
|
|
self.current_app.logger.info(log_message)
|
|
|
|
|
self.statsd_client.incr("clients.mmg.success")
|
2016-09-23 15:59:23 +01:00
|
|
|
else:
|
|
|
|
|
self.statsd_client.incr("clients.mmg.error")
|
|
|
|
|
self.current_app.logger.error(log_message)
|
2016-09-22 17:18:05 +01:00
|
|
|
|
2016-04-01 16:42:31 +01:00
|
|
|
def get_name(self):
|
|
|
|
|
return self.name
|
|
|
|
|
|
2016-07-01 14:06:32 +01:00
|
|
|
def send_sms(self, to, content, reference, multi=True, sender=None):
|
2016-04-01 16:42:31 +01:00
|
|
|
data = {
|
|
|
|
|
"reqType": "BULK",
|
|
|
|
|
"MSISDN": to,
|
|
|
|
|
"msg": content,
|
2016-08-23 12:05:47 +01:00
|
|
|
"sender": self.from_number if sender is None else sender,
|
2016-04-18 09:55:56 +01:00
|
|
|
"cid": reference,
|
|
|
|
|
"multi": multi
|
2016-04-01 16:42:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start_time = monotonic()
|
|
|
|
|
try:
|
2016-09-22 17:18:05 +01:00
|
|
|
response = request(
|
|
|
|
|
"POST",
|
|
|
|
|
self.mmg_url,
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers={
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Authorization': 'Basic {}'.format(self.api_key)
|
|
|
|
|
}
|
2016-06-01 15:24:19 +01:00
|
|
|
)
|
2016-09-22 17:18:05 +01:00
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
try:
|
|
|
|
|
json.loads(response.text)
|
|
|
|
|
except (ValueError, AttributeError) as e:
|
|
|
|
|
self.record_outcome(False, response)
|
|
|
|
|
raise MMGClientResponseException(response=response, exception=e)
|
|
|
|
|
self.record_outcome(True, response)
|
2016-04-01 16:42:31 +01:00
|
|
|
except RequestException as e:
|
2016-09-22 17:18:05 +01:00
|
|
|
self.record_outcome(False, e.response)
|
|
|
|
|
raise MMGClientResponseException(response=e.response, exception=e)
|
2016-04-01 16:42:31 +01:00
|
|
|
finally:
|
|
|
|
|
elapsed_time = monotonic() - start_time
|
2016-08-08 11:23:58 +01:00
|
|
|
self.statsd_client.timing("clients.mmg.request-time", elapsed_time)
|
2016-06-01 15:24:19 +01:00
|
|
|
self.current_app.logger.info("MMG request finished in {}".format(elapsed_time))
|
2016-09-22 17:18:05 +01:00
|
|
|
|
2016-04-01 16:42:31 +01:00
|
|
|
return response
|