40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
import datetime
|
|
|
|
from aiocache import cached
|
|
|
|
from gallery.sketch.cached import DEFAULT_CACHE_PRESET, CachedApi
|
|
|
|
from .api import WeatherApi
|
|
from .model import Location, WeatherResponse
|
|
|
|
CACHE_PRESET = DEFAULT_CACHE_PRESET
|
|
|
|
|
|
class CachedWeatherApi(WeatherApi, CachedApi[WeatherApi]):
|
|
CACHE_KEY = "weather"
|
|
|
|
@cached(
|
|
key_builder=lambda fun, self, query: f"api.{self.CACHE_KEY}.{self.provider}.locations.{query}",
|
|
**CACHE_PRESET._asdict(),
|
|
)
|
|
async def find_locations(self, query: str) -> list[Location]:
|
|
return await self._api.find_locations(query)
|
|
|
|
@cached(
|
|
key_builder=lambda fun, self, location_id, date: (
|
|
f"api.{self.CACHE_KEY}.{self.provider}.day.{location_id}.{date}"
|
|
),
|
|
**CACHE_PRESET._asdict(),
|
|
)
|
|
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
|
|
return await self._api.get_day(location_id, date)
|
|
|
|
@cached(
|
|
key_builder=lambda fun, self, location_id, date: (
|
|
f"api.{self.CACHE_KEY}.{self.provider}.day.{location_id}.{date}"
|
|
),
|
|
**CACHE_PRESET._asdict(),
|
|
)
|
|
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
|
|
return await self._api.get_days(location_id, days)
|