INNER CODE UNIT · Python

_inject_asgi_headers

laurentS/slowapi · slowapi/extension.py:431

    def _inject_asgi_headers(
        self, headers: MutableHeaders, current_limit: Tuple[RateLimitItem, List[str]]
    ) -> MutableHeaders:
        """
        Injects 'X-RateLimit-Reset', 'X-RateLimit-Remaining', 'X-RateLimit-Limit'
        and 'Retry-After' headers into :headers parameter if needed.

        Basically the same as _inject_headers, but without access to the Response object.
        -> supports ASGI Middlewares.
        """
        if self.enabled and self._headers_enabled and current_limit is not None:
            try:
                window_stats: Tuple[int, int] = self.limiter.get_window_stats(
                    current_limit[0], *current_limit[1]
                )
                reset_in = 1 + window_stats[0]
                headers[self._header_mapping[HEADERS.LIMIT]] = str(
                    current_limit[0].amount

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…