Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle response message customization #202

Open
tmorgan497 opened this issue Nov 4, 2024 · 1 comment
Open

Throttle response message customization #202

tmorgan497 opened this issue Nov 4, 2024 · 1 comment

Comments

@tmorgan497
Copy link

Hi, is there a way to change the default message in the throttle response to remove the time remaining message?

For example, instead of this:

{
  "detail": "Request was throttled. Expected available in 53 seconds."
}

A way to customize it to this:

{
  "detail": "Too many requests. Please try again later."
}

Potential implementation:

class User6MinRateThrottle(UserRateThrottle):
    """User 6 per min. rate limit."""

    rate = "6/min"
    scope = "minutes"

    def throttle_response(self, request, response):
        if response.status_code = 429:
            return JsonResponse({"detail": "Too many requests. Please try again later."}, status=429)
        return response


@api_controller("shop/", throttle=[User6MinRateThrottle()])
class ProductController:
    """Product API controller."""

    @route.post("/product")
    def create_product(
        self,
        payload: ProductCreateSchema,
    ) -> dict:
        """Create product API endpoint."""
        product = Product.objects.create(
            name=payload.name,
            description=payload.description,
            price=payload.price,
        )
        return {
            "id": product.id,
            "name": product.name,
            "description": product.description,
            "price": product.price,
        }
@eadwinCode
Copy link
Owner

eadwinCode commented Nov 11, 2024

@tmorgan497 You may have to create this ticket on django-ninja repo. because the base class for Throttling have been moved to django-ninja repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants