11"""A simple FastAPI app that serves a REST API for proselint."""
22
3- from typing import TYPE_CHECKING , cast
4-
53from fastapi import FastAPI , HTTPException , Request , Response , status
64from fastapi .middleware .cors import CORSMiddleware
75from slowapi import Limiter
1311from proselint .registry import CheckRegistry
1412from proselint .tools import LintFile , LintResult
1513
16- if TYPE_CHECKING :
17- from slowapi import ViewRateLimit
18-
1914
2015def _lint (input_text : str ) -> list [LintResult ]:
2116 return LintFile (content = input_text , source = "<api>" ).lint ()
@@ -35,21 +30,20 @@ def _lint(input_text: str) -> list[LintResult]:
3530CheckRegistry ().register_many (__register__ )
3631
3732
38- # NOTE: We don't use slowapi's handler due to the error key name.
3933@app .exception_handler (RateLimitExceeded )
4034def rate_limit_exceeded_handler (
41- request : Request ,
35+ _ : Request ,
4236 exc : RateLimitExceeded ,
4337) -> Response :
4438 """Middleware to handle exceeded ratelimits."""
45- response = JSONResponse (
46- {"detail" : f"rate limit exceeded: { exc .detail } " },
39+ return JSONResponse (
40+ {
41+ "status" : "error" ,
42+ "message" : "rate limit exceeded" ,
43+ "limit" : str (exc .detail ),
44+ },
4745 status_code = status .HTTP_429_TOO_MANY_REQUESTS ,
48- )
49-
50- rate_limit = cast ("ViewRateLimit" , request .state .view_rate_limit )
51- return limiter ._inject_headers ( # noqa: SLF001
52- response , rate_limit
46+ headers = getattr (exc , "headers" , None ),
5347 )
5448
5549
0 commit comments