| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 | # (c) Nelen & Schuurmansfrom abc import ABCfrom datetime import datetimefrom typing import Callablefrom typing import Listfrom typing import Optionalfrom .exceptions import DoesNotExistfrom .filter import Filterfrom .pagination import PageOptionsfrom .types import Idfrom .types import Json__all__ = ["Gateway"]class Gateway(ABC):    async def filter(        self, filters: List[Filter], params: Optional[PageOptions] = None    ) -> List[Json]:        raise NotImplementedError()    async def count(self, filters: List[Filter]) -> int:        return len(await self.filter(filters, params=None))    async def exists(self, filters: List[Filter]) -> bool:        return len(await self.filter(filters, params=PageOptions(limit=1))) > 0    async def get(self, id: Id) -> Optional[Json]:        result = await self.filter([Filter(field="id", values=[id])], params=None)        return result[0] if result else None    async def add(self, item: Json) -> Json:        raise NotImplementedError()    async def update(        self, item: Json, if_unmodified_since: Optional[datetime] = None    ) -> Json:        raise NotImplementedError()    async def update_transactional(self, id: Id, func: Callable[[Json], Json]) -> Json:        existing = await self.get(id)        if existing is None:            raise DoesNotExist("record", id)        return await self.update(            func(existing), if_unmodified_since=existing["updated_at"]        )    async def upsert(self, item: Json) -> Json:        try:            return await self.update(item)        except DoesNotExist:            return await self.add(item)    async def remove(self, id: Id) -> bool:        raise NotImplementedError()
 |