Skip to content

Throttle response message customization #202

@tmorgan497

Description

@tmorgan497

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,
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions