2018-02-20 11:22:17 +00:00
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user
|
2016-03-02 13:52:05 +00:00
|
|
|
|
|
|
|
|
|
2016-11-30 17:01:44 +00:00
|
|
|
class NotificationApiClient(NotifyAdminAPIClient):
|
2016-09-21 09:28:06 +01:00
|
|
|
def get_notifications_for_service(
|
|
|
|
|
self,
|
|
|
|
|
service_id,
|
|
|
|
|
job_id=None,
|
|
|
|
|
template_type=None,
|
|
|
|
|
status=None,
|
|
|
|
|
page=None,
|
|
|
|
|
page_size=None,
|
2019-01-08 15:35:44 +00:00
|
|
|
count_pages=None,
|
2016-09-21 09:32:20 +01:00
|
|
|
limit_days=None,
|
|
|
|
|
include_jobs=None,
|
2017-04-20 14:57:31 +01:00
|
|
|
include_from_test_key=None,
|
2017-05-30 12:55:13 +01:00
|
|
|
format_for_csv=None,
|
|
|
|
|
to=None,
|
2018-07-17 17:02:59 +01:00
|
|
|
include_one_off=None,
|
2016-09-21 09:28:06 +01:00
|
|
|
):
|
2019-01-08 15:35:44 +00:00
|
|
|
params = {
|
2023-08-25 09:12:23 -07:00
|
|
|
"page": page,
|
|
|
|
|
"page_size": page_size,
|
|
|
|
|
"template_type": template_type,
|
|
|
|
|
"status": status,
|
|
|
|
|
"include_jobs": include_jobs,
|
|
|
|
|
"include_from_test_key": include_from_test_key,
|
|
|
|
|
"format_for_csv": format_for_csv,
|
|
|
|
|
"to": to,
|
|
|
|
|
"include_one_off": include_one_off,
|
|
|
|
|
"count_pages": count_pages,
|
2019-01-08 15:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params = {k: v for k, v in params.items() if v is not None}
|
|
|
|
|
|
2021-12-14 15:40:12 +00:00
|
|
|
# if `to` is set it is likely PII like an email address or mobile which
|
|
|
|
|
# we do not want in our logs, so we do a POST request instead of a GET
|
|
|
|
|
method = self.post if to else self.get
|
2023-08-25 09:12:23 -07:00
|
|
|
kwargs = {"data": params} if to else {"params": params}
|
2021-12-14 15:40:12 +00:00
|
|
|
|
2016-03-02 13:52:05 +00:00
|
|
|
if job_id:
|
2021-12-14 15:40:12 +00:00
|
|
|
return method(
|
2023-08-25 09:12:23 -07:00
|
|
|
url="/service/{}/job/{}/notifications".format(service_id, job_id),
|
2021-12-14 15:40:12 +00:00
|
|
|
**kwargs
|
2016-03-02 13:52:05 +00:00
|
|
|
)
|
|
|
|
|
else:
|
2016-04-28 15:50:05 +01:00
|
|
|
if limit_days is not None:
|
2023-08-25 09:12:23 -07:00
|
|
|
params["limit_days"] = limit_days
|
|
|
|
|
return method(url="/service/{}/notifications".format(service_id), **kwargs)
|
2017-05-24 13:19:31 +01:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
def send_notification(
|
|
|
|
|
self, service_id, *, template_id, recipient, personalisation, sender_id
|
|
|
|
|
):
|
2017-06-22 15:56:05 +01:00
|
|
|
data = {
|
2023-08-25 09:12:23 -07:00
|
|
|
"template_id": template_id,
|
|
|
|
|
"to": recipient,
|
|
|
|
|
"personalisation": personalisation,
|
2017-06-22 15:56:05 +01:00
|
|
|
}
|
2017-10-16 16:35:35 +01:00
|
|
|
if sender_id:
|
2023-08-25 09:12:23 -07:00
|
|
|
data["sender_id"] = sender_id
|
2017-06-22 15:56:05 +01:00
|
|
|
data = _attach_current_user(data)
|
2023-08-25 09:12:23 -07:00
|
|
|
return self.post(
|
|
|
|
|
url="/service/{}/send-notification".format(service_id), data=data
|
|
|
|
|
)
|
2017-06-22 15:56:05 +01:00
|
|
|
|
2017-06-19 12:31:14 +01:00
|
|
|
def get_notification(self, service_id, notification_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
return self.get(
|
|
|
|
|
url="/service/{}/notifications/{}".format(service_id, notification_id)
|
|
|
|
|
)
|
2017-09-20 16:02:15 +01:00
|
|
|
|
|
|
|
|
def get_api_notifications_for_service(self, service_id):
|
2018-07-17 17:02:59 +01:00
|
|
|
ret = self.get_notifications_for_service(
|
|
|
|
|
service_id,
|
|
|
|
|
include_jobs=False,
|
|
|
|
|
include_from_test_key=True,
|
2019-01-08 15:35:44 +00:00
|
|
|
include_one_off=False,
|
2023-08-25 09:12:23 -07:00
|
|
|
count_pages=False,
|
2018-07-17 17:02:59 +01:00
|
|
|
)
|
2022-12-05 15:33:44 -05:00
|
|
|
return ret
|
2018-10-26 15:39:32 +01:00
|
|
|
|
2018-11-15 16:40:55 +00:00
|
|
|
def update_notification_to_cancelled(self, service_id, notification_id):
|
|
|
|
|
return self.post(
|
2023-08-25 09:12:23 -07:00
|
|
|
url="/service/{}/notifications/{}/cancel".format(
|
|
|
|
|
service_id, notification_id
|
|
|
|
|
),
|
|
|
|
|
data={},
|
|
|
|
|
)
|
2018-11-15 16:40:55 +00:00
|
|
|
|
2019-07-22 13:09:03 +01:00
|
|
|
def get_notification_status_by_service(self, start_date, end_date):
|
|
|
|
|
return self.get(
|
2023-08-25 09:12:23 -07:00
|
|
|
url="service/monthly-data-by-service",
|
2019-07-22 13:09:03 +01:00
|
|
|
params={
|
2023-08-25 09:12:23 -07:00
|
|
|
"start_date": str(start_date),
|
|
|
|
|
"end_date": str(end_date),
|
|
|
|
|
},
|
2019-07-22 13:09:03 +01:00
|
|
|
)
|
|
|
|
|
|
2019-09-24 17:27:31 +01:00
|
|
|
def get_notification_count_for_job_id(self, *, service_id, job_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
return self.get(
|
|
|
|
|
url="/service/{}/job/{}/notification_count".format(service_id, job_id)
|
|
|
|
|
)["count"]
|
2019-09-24 17:27:31 +01:00
|
|
|
|
2018-10-26 15:39:32 +01:00
|
|
|
|
|
|
|
|
notification_api_client = NotificationApiClient()
|