async def get_collection_by_address(self, address: str, connection: Optional[DatabaseConnection] = None) -> Collection: query = TokenCollectionsTable.select() \ .where(TokenCollectionsTable.c.address == address) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException(message=f'Collection with registry:{address} not found') collection = collection_from_row(row) return collection
async def get_block_by_number(self, blockNumber: int, connection: Optional[DatabaseConnection] = None) -> TokenMetadata: query = BlocksTable.select() \ .where(BlocksTable.c.blockNumber == blockNumber) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException(message=f'Block with blockNumber:{blockNumber} not found') block = block_from_row(row) return block
async def get_token_ownership_by_registry_address_token_id(self, registryAddress: str, tokenId: str, connection: Optional[DatabaseConnection] = None) -> TokenOwnership: query = TokenOwnershipsTable.select() \ .where(TokenOwnershipsTable.c.registryAddress == registryAddress) \ .where(TokenOwnershipsTable.c.tokenId == tokenId) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException(message=f'TokenOwnership with registry:{registryAddress} tokenId:{tokenId} not found') tokenOwnership = token_ownership_from_row(row) return tokenOwnership
async def get_latest_base_image_url(self, network: str) -> BaseImage: baseImages = await self.retriever.list_base_images( fieldFilters=[ StringFieldFilter(fieldName=BaseImagesTable.c.network.key, eq=network) ], orders=[ Order(fieldName=BaseImagesTable.c.updatedDate.key, direction=Direction.DESCENDING) ], limit=1) if len(baseImages) == 0: raise NotFoundException() return baseImages[0]
async def get_grid_item( self, gridItemId: int, connection: Optional[DatabaseConnection] = None) -> GridItem: query = GridItemsTable.select() \ .where(GridItemsTable.c.gridItemId == gridItemId) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException( message=f'GridItem with gridItemId {gridItemId} not found') gridItem = grid_item_from_row(row) return gridItem
async def get_network_update_by_network( self, network: str, connection: Optional[DatabaseConnection] = None) -> NetworkUpdate: query = NetworkUpdatesTable.select() \ .where(NetworkUpdatesTable.c.network == network) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException( message=f'NetworkUpdate with network {network} not found') networkUpdate = network_update_from_row(row) return networkUpdate
async def get_token_content(self, network: str, tokenId: str) -> TokenMetadata: if network in { 'rinkeby', 'mumbai', 'rinkeby2', 'rinkeby3', 'rinkeby4' }: try: tokenIdValue = int(tokenId) except ValueError: raise NotFoundException() if tokenIdValue <= 0 or tokenIdValue > 10000: raise NotFoundException() tokenIndex = tokenIdValue - 1 return TokenMetadata( tokenId=tokenId, tokenIndex=tokenIndex, name=f'MDTP Token {tokenId}', description=None, image= f'https://mdtp-images.s3-eu-west-1.amazonaws.com/uploads/b88762dd-7605-4447-949b-d8ba99e6f44d/{tokenIndex}.png', url=None, groupId=None, ) contentUrl = await self.contractStore.get_token_content_url( network=network, tokenId=tokenId) contentJson = await self._get_json_content(url=contentUrl) return TokenMetadata( tokenId=contentJson['tokenId'], tokenIndex=contentJson.get('tokenIndex') or contentJson['tokenId'] - 1, name=contentJson.get('name') or contentJson.get('title') or '', description=contentJson.get('description'), image=contentJson.get('image') or contentJson.get('imageUrl') or '', url=contentJson.get('url'), groupId=contentJson.get('groupId'), )
async def get_token_metadata(self, network: str, tokenId: str) -> TokenMetadata: if network in { 'rinkeby', 'mumbai', 'rinkeby2', 'rinkeby3', 'rinkeby4' }: try: tokenIdValue = int(tokenId) except ValueError: # pylint: disable=raise-missing-from raise NotFoundException() if tokenIdValue <= 0 or tokenIdValue > 10000: # pylint: disable=raise-missing-from raise NotFoundException() return TokenMetadata( tokenId=tokenId, tokenIndex=tokenIdValue - 1, name=f'MDTP #{tokenId}', description=None, image='', url=None, groupId=None, ) metadataUrl = await self.contractStore.get_token_metadata_url( network=network, tokenId=tokenId) metadataJson = await self._get_json_content(url=metadataUrl) return TokenMetadata( tokenId=metadataJson['tokenId'], tokenIndex=metadataJson.get('tokenIndex') or metadataJson['tokenId'] - 1, name=metadataJson.get('name') or metadataJson.get('title') or '', description=metadataJson.get('description'), image=metadataJson.get('image') or metadataJson.get('imageUrl') or '', url=metadataJson.get('url'), groupId=metadataJson.get('groupId'), )
def update_object(target, params): entity = target.__class__ with DatabaseSession() as session: data = session.query(entity). \ filter(entity.id == target.id). \ first() if not data: # object not found raise NotFoundException() for k, v in params.items(): if not hasattr(data, k): raise ValueError("Entity {} has not property `{}`.".format(entity.__class__.__name__, k)) setattr(data, k, v) session.commit() return data
async def get_grid_item_by_token_id_network( self, tokenId: str, network: str, connection: Optional[DatabaseConnection] = None) -> GridItem: query = GridItemsTable.select() \ .where(GridItemsTable.c.tokenId == tokenId) \ .where(GridItemsTable.c.network == network) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException( message= f'GridItem with tokenId {tokenId}, network {network} not found' ) gridItem = grid_item_from_row(row) return gridItem
async def get_grid_item_group_image_by_network_owner_id_group_id( self, network: str, ownerId: str, groupId: str, connection: Optional[DatabaseConnection] = None ) -> GridItemGroupImage: # pylint: disable=invalid-name query = GridItemGroupImagesTable.select() \ .where(GridItemGroupImagesTable.c.network == network) \ .where(GridItemGroupImagesTable.c.ownerId == ownerId) \ .where(GridItemGroupImagesTable.c.groupId == groupId) result = await self.database.execute(query=query, connection=connection) row = result.first() if not row: raise NotFoundException( message= f'GridItemGroupImage with network {network}, ownerId {ownerId}, groupId {groupId} not found' ) gridItemGroupImage = grid_item_group_image_from_row(row) return gridItemGroupImage
def get_contract_by_address(self, address: str) -> Contract: for contract in self.contracts: if contract.address == address: return contract raise NotFoundException()
def get_contract(self, network: str) -> Contract: for contract in self.contracts: if contract.network == network: return contract raise NotFoundException()