Add missing type hints in http component (#50411)
This commit is contained in:
parent
85f758380a
commit
ce15f28642
13 changed files with 245 additions and 160 deletions
|
@ -1,21 +1,25 @@
|
|||
"""Static file handling for HTTP component."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
from aiohttp import hdrs
|
||||
from aiohttp.web import FileResponse
|
||||
from aiohttp.web import FileResponse, Request, StreamResponse
|
||||
from aiohttp.web_exceptions import HTTPForbidden, HTTPNotFound
|
||||
from aiohttp.web_urldispatcher import StaticResource
|
||||
|
||||
# mypy: allow-untyped-defs
|
||||
|
||||
CACHE_TIME = 31 * 86400 # = 1 month
|
||||
CACHE_HEADERS = {hdrs.CACHE_CONTROL: f"public, max-age={CACHE_TIME}"}
|
||||
CACHE_TIME: Final = 31 * 86400 # = 1 month
|
||||
CACHE_HEADERS: Final[Mapping[str, str]] = {
|
||||
hdrs.CACHE_CONTROL: f"public, max-age={CACHE_TIME}"
|
||||
}
|
||||
|
||||
|
||||
class CachingStaticResource(StaticResource):
|
||||
"""Static Resource handler that will add cache headers."""
|
||||
|
||||
async def _handle(self, request):
|
||||
async def _handle(self, request: Request) -> StreamResponse:
|
||||
rel_url = request.match_info["filename"]
|
||||
try:
|
||||
filename = Path(rel_url)
|
||||
|
@ -42,7 +46,6 @@ class CachingStaticResource(StaticResource):
|
|||
return FileResponse(
|
||||
filepath,
|
||||
chunk_size=self._chunk_size,
|
||||
# type ignore: https://github.com/aio-libs/aiohttp/pull/3976
|
||||
headers=CACHE_HEADERS, # type: ignore
|
||||
headers=CACHE_HEADERS,
|
||||
)
|
||||
raise HTTPNotFound
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue