| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 | # (c) Nelen & Schuurmansfrom abc import ABCfrom datetime import datetimefrom typing import Callablefrom typing import Listfrom typing import Optionalfrom .exceptions import DoesNotExistfrom .filter import Filterfrom .json import Jsonfrom .pagination import PageOptions__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: int) -> 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: int, 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: int) -> bool:        raise NotImplementedError()
 |