| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | # -*- coding: utf-8 -*-# (c) Nelen & Schuurmansfrom abc import abstractmethodfrom abc import abstractpropertyfrom typing import Genericfrom typing import Listfrom typing import Optionalfrom typing import TypeVarfrom clean_python.base.application.manage import Managefrom clean_python.base.domain.exceptions import BadRequestfrom clean_python.base.domain.exceptions import DoesNotExistfrom clean_python.base.domain.pagination import PageOptionsfrom clean_python.base.domain.root_entity import RootEntityfrom clean_python.base.domain.value_object import ValueObjectfrom clean_python.base.infrastructure.gateway import FilterE = TypeVar("E", bound=RootEntity)  # ExternalT = TypeVar("T", bound=ValueObject)  # Internal# don't subclass Gateway; Gateway makes Json objectsclass InternalGateway(Generic[E, T]):    @abstractproperty    def manage(self) -> Manage[E]:        raise NotImplementedError()    @abstractmethod    def _map(self, obj: E) -> T:        raise NotImplementedError()    async def get(self, id: int) -> Optional[T]:        try:            result = await self.manage.retrieve(id)        except DoesNotExist:            return None        else:            return self._map(result)    async def filter(        self, filters: List[Filter], params: Optional[PageOptions] = None    ) -> List[T]:        page = await self.manage.filter(filters, params)        return [self._map(x) for x in page.items]    async def add(self, item: T) -> T:        try:            created = await self.manage.create(item.dict())        except BadRequest as e:            raise ValueError(e)        return self._map(created)    async def remove(self, id) -> bool:        return await self.manage.destroy(id)    async def count(self, filters: List[Filter]) -> int:        return await self.manage.count(filters)    async def exists(self, filters: List[Filter]) -> bool:        return await self.manage.exists(filters)
 |