2016-04-18 09:55:56 +01:00
|
|
|
import json
|
2016-04-01 16:42:31 +01:00
|
|
|
from flask import current_app
|
|
|
|
|
from monotonic import monotonic
|
2016-04-04 15:02:21 +01:00
|
|
|
from requests import (request, RequestException, HTTPError)
|
2016-04-06 14:31:33 +01:00
|
|
|
from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE)
|
2016-04-04 15:02:21 +01:00
|
|
|
from app.clients.sms import (SmsClient, SmsClientException)
|
|
|
|
|
|
2016-04-06 14:31:33 +01:00
|
|
|
mmg_response_map = {
|
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'
|
|
|
|
|
},
|
|
|
|
|
'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
|
|
|
|
|
|
|
|
|
|
|
|
|
class MMGClientException(SmsClientException):
|
|
|
|
|
def __init__(self, error_response):
|
|
|
|
|
self.code = error_response['Error']
|
|
|
|
|
self.description = error_response['Description']
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "Code {} description {}".format(self.code, self.description)
|
2016-04-01 16:42:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MMGClient(SmsClient):
|
|
|
|
|
'''
|
|
|
|
|
MMG sms client
|
|
|
|
|
'''
|
|
|
|
|
|
2016-05-13 17:15:39 +01:00
|
|
|
def init_app(self, config, statsd_client, *args, **kwargs):
|
2016-04-07 10:53:59 +01:00
|
|
|
super(SmsClient, self).__init__(*args, **kwargs)
|
2016-04-01 16:42:31 +01:00
|
|
|
self.api_key = config.get('MMG_API_KEY')
|
2016-04-07 10:18:46 +01:00
|
|
|
self.from_number = config.get('MMG_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-04-01 16:42:31 +01:00
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
|
return self.name
|
|
|
|
|
|
2016-04-18 09:55:56 +01:00
|
|
|
def send_sms(self, to, content, reference, multi=True):
|
2016-04-01 16:42:31 +01:00
|
|
|
data = {
|
|
|
|
|
"reqType": "BULK",
|
|
|
|
|
"MSISDN": to,
|
|
|
|
|
"msg": content,
|
2016-04-06 17:35:14 +01:00
|
|
|
"sender": self.from_number,
|
2016-04-18 09:55:56 +01:00
|
|
|
"cid": reference,
|
|
|
|
|
"multi": multi
|
2016-04-01 16:42:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start_time = monotonic()
|
|
|
|
|
try:
|
|
|
|
|
response = request("POST", "https://www.mmgrp.co.uk/API/json/api.php",
|
2016-04-06 17:35:14 +01:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers={'Content-Type': 'application/json',
|
|
|
|
|
'Authorization': 'Basic {}'.format(self.api_key)})
|
2016-04-04 15:02:21 +01:00
|
|
|
if response.status_code != 200:
|
2016-04-06 17:35:14 +01:00
|
|
|
error = response.text
|
|
|
|
|
raise MMGClientException(json.loads(error))
|
2016-04-04 15:02:21 +01:00
|
|
|
response.raise_for_status()
|
2016-04-01 16:42:31 +01:00
|
|
|
except RequestException as e:
|
|
|
|
|
api_error = HTTPError.create(e)
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
"API {} request on {} failed with {} '{}'".format(
|
|
|
|
|
"POST",
|
|
|
|
|
"https://www.mmgrp.co.uk/API/json/api.php",
|
|
|
|
|
api_error.status_code,
|
|
|
|
|
api_error.message
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-05-13 17:15:39 +01:00
|
|
|
self.statsd_client.incr("notifications.clients.mmg.error")
|
2016-04-01 16:42:31 +01:00
|
|
|
raise api_error
|
|
|
|
|
finally:
|
|
|
|
|
elapsed_time = monotonic() - start_time
|
2016-05-13 17:15:39 +01:00
|
|
|
self.statsd_client.timing("notifications.clients.mmg.request-time", elapsed_time)
|
2016-04-01 16:42:31 +01:00
|
|
|
current_app.logger.info("MMG request finished in {}".format(elapsed_time))
|
|
|
|
|
return response
|