mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 15:15:38 -05:00
Adds in call to new rate limit method in the redis client
- both V1 and V2 APIs - Rate limiting wrapped into a new method - check_rate_limiting - delegates to the previous daily limit and the new though put limit - Rate limiting done on key type. Each key has it's own limit (number of requests) and interval (time period of requests) - Configured in the config. Not done on a per-env basis though could be in the future.
This commit is contained in:
@@ -21,9 +21,9 @@ from app.notifications.process_client_response import (
|
||||
from app.notifications.process_notifications import (persist_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
from app.notifications.validators import (check_service_message_limit,
|
||||
from app.notifications.validators import (check_service_over_daily_message_limit,
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active)
|
||||
check_template_is_active, check_rate_limiting)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
@@ -107,7 +107,7 @@ def send_notification(notification_type):
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
check_rate_limiting(service, api_user)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification_form['template'],
|
||||
service_id=service.id)
|
||||
|
||||
@@ -4,12 +4,26 @@ from notifications_utils.recipients import validate_and_format_phone_number, val
|
||||
from app.dao import services_dao
|
||||
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.v2.errors import TooManyRequestsError, BadRequestError
|
||||
from app.v2.errors import TooManyRequestsError, BadRequestError, RateLimitError
|
||||
from app import redis_store
|
||||
from notifications_utils.clients import redis
|
||||
|
||||
|
||||
def check_service_message_limit(key_type, service):
|
||||
def check_service_over_api_rate_limit(service, api_key):
|
||||
cache_key = redis.rate_limit_cache_key(service.id, api_key.key_type)
|
||||
rate_limit = current_app.config['API_KEY_LIMITS'][api_key.key_type]['limit']
|
||||
interval = current_app.config['API_KEY_LIMITS'][api_key.key_type]['interval']
|
||||
if redis_store.exceeded_rate_limit(
|
||||
cache_key,
|
||||
rate_limit,
|
||||
interval):
|
||||
raise RateLimitError(
|
||||
rate_limit,
|
||||
interval,
|
||||
api_key.key_type)
|
||||
|
||||
|
||||
def check_service_over_daily_message_limit(key_type, service):
|
||||
if key_type != KEY_TYPE_TEST:
|
||||
cache_key = redis.daily_limit_cache_key(service.id)
|
||||
service_stats = redis_store.get(cache_key)
|
||||
@@ -20,6 +34,11 @@ def check_service_message_limit(key_type, service):
|
||||
raise TooManyRequestsError(service.message_limit)
|
||||
|
||||
|
||||
def check_rate_limiting(service, api_key):
|
||||
check_service_over_api_rate_limit(service, api_key)
|
||||
check_service_over_daily_message_limit(api_key.key_type, service)
|
||||
|
||||
|
||||
def check_template_is_for_notification_type(notification_type, template_type):
|
||||
if notification_type != template_type:
|
||||
message = "{0} template is not suitable for {1} notification".format(template_type,
|
||||
@@ -55,8 +74,6 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type)
|
||||
|
||||
def check_sms_content_char_count(content_count):
|
||||
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||
if (
|
||||
content_count > char_count_limit
|
||||
):
|
||||
if content_count > char_count_limit:
|
||||
message = 'Content for template has a character count greater than the limit of {}'.format(char_count_limit)
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
Reference in New Issue
Block a user