mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* fix internal error when adjusting rate limits downwards were the new limit would result in negative available requests for already tracked clients (#1126) * remove debug output and unused variable * expose CACHE_MAX_ENTRIES to be set via env, also implement lower limit sanity check for it (#1151) * auth-id changes * fix test data failure Co-authored-by: Stefan Pratter <stefan@20c.com>
36 lines
938 B
Python
36 lines
938 B
Python
from rest_framework.exceptions import Throttled
|
|
from rest_framework.views import exception_handler
|
|
|
|
|
|
def format_wait_time(wait_time):
|
|
"""
|
|
Format wait time in seconds to human readable format
|
|
"""
|
|
|
|
if wait_time < 60:
|
|
return f"{wait_time} seconds"
|
|
elif wait_time < 3600 and wait_time > 60:
|
|
return f"{wait_time // 60} minutes"
|
|
else:
|
|
return f"{wait_time // 3600} hours"
|
|
|
|
|
|
def rest_exception_handler(exc, context):
|
|
|
|
response = exception_handler(exc, context)
|
|
request = context.get("request")
|
|
|
|
if isinstance(exc, Throttled):
|
|
message = getattr(
|
|
request,
|
|
"throttle_response_message",
|
|
"Request was throttled. Expected available in {time}.",
|
|
)
|
|
custom_response_data = {
|
|
"message": f"{message}".replace("{time}", format_wait_time(exc.wait)),
|
|
}
|
|
|
|
response.data = custom_response_data
|
|
|
|
return response
|