summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam Englander <adamenglander@yahoo.com>2019-12-01 18:52:15 -0800
committerDavid Lord <davidism@gmail.com>2020-01-04 17:53:20 -0800
commitccfd0750ae69c874594de1d8dd439c6318761b6b (patch)
tree5b921649629cf2c82ace378944b245460fe66156 /src
parent42e29b98185ea388ab4ff1ea957c8f4490ae441a (diff)
downloadwerkzeug-ccfd0750ae69c874594de1d8dd439c6318761b6b.tar.gz
Add ability to set Retry-After header to 429 Too Many Requests response.
Added optional retry_after_secs to TooManyRequests exception init. This brings the output in line with the HTTP spec.
Diffstat (limited to 'src')
-rw-r--r--src/werkzeug/exceptions.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/werkzeug/exceptions.py b/src/werkzeug/exceptions.py
index edc6454a..7b64048f 100644
--- a/src/werkzeug/exceptions.py
+++ b/src/werkzeug/exceptions.py
@@ -600,11 +600,29 @@ class TooManyRequests(HTTPException):
to identify users and their request rates). The server may include a
"Retry-After" header to indicate how long the user should wait before
retrying.
+
+ .. versionchanged:: 0.16.1
+ ``retry_after_secs`` was added as the first argument, ahead of
+ ``description``.
"""
code = 429
description = "This user has exceeded an allotted request count. Try again later."
+ def __init__(self, description=None, retry_after_secs=None):
+ """
+ Use the optional value of retry_after_secs to specify the number of seconds
+ to wait for a retry attempt.
+ """
+ HTTPException.__init__(self, description)
+ self.retry_after_secs = retry_after_secs
+
+ def get_headers(self, environ=None):
+ headers = HTTPException.get_headers(self, environ)
+ if self.retry_after_secs:
+ headers.append(("Retry-After", str(self.retry_after_secs)))
+ return headers
+
class RequestHeaderFieldsTooLarge(HTTPException):
"""*431* `Request Header Fields Too Large`