Applying rate limits only when API response is successful #311
ahmad-taha
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone 👋
And thank you, James, and everyone who contributed to this fantastic library. Why did I not hear about this sooner?😄
Although django-ratelimit takes care of many use cases, mine was a bit different. I have internal APIs for our website where I need to apply rate limits only when the API response returns success.
For example, I was adding a rate limit to an internal API handler that sends SMS OTPs to users for phone verification. There can be many cases where API fails to send OTP, and it shouldn't rate limit users based on our errors.
Therefore, I have made a decorator similar to
@ratelimit
, which only applies rate limit when the view response is JSON and contains{"success":True}
.Note: I am no Python expert and have not contributed much online in the past, so please let me know if there are any potential downsides to this or if there is anything that can be done to improve the code base or make it more efficient.
I thought of sharing this in case anyone may have a similar use case. This can be modified based on the website's specific usage and style.
So now whenever I need to use rate limiting on my APIs, I can simply do the following.
Beta Was this translation helpful? Give feedback.
All reactions