Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Union
- class SpotifyObjectTypeEnum:
- ALBUM = 'album'
- TRACK = 'track'
- class SpotifyObjectCollectionDTO(list):
- pass
- class AlbumCollectionDTO(SpotifyObjectCollectionDTO):
- pass
- class TrackCollectionDTO(SpotifyObjectCollectionDTO):
- pass
- class SpotifyResultDTO:
- def __init__(self, albums: AlbumCollectionDTO, tracks: TrackCollectionDTO):
- self.__albums = albums
- self.__tracks = tracks
- class SpotifyAlbumDTO:
- def __init__(self, title: str, year: int):
- self.__title = title
- self.__year = year
- class SpotifyTrackDTO:
- def __init__(self, title: str, duration: int):
- self.__title = title
- self.__duration = duration
- class SpotifyAlbumFactory:
- @staticmethod
- def build(data: dict) -> SpotifyAlbumDTO:
- return SpotifyAlbumDTO(
- title=data.get('title'),
- year=data.get('year'),
- )
- class SpotifyTrackFactory:
- @staticmethod
- def build(data: dict) -> SpotifyTrackDTO:
- return SpotifyTrackDTO(
- title=data.get('title'),
- duration=data.get('duration'),
- )
- class SpotifyObjectCollectionFactory:
- @staticmethod
- def build(data: dict, object_type: str) -> Union[AlbumCollectionDTO, TrackCollectionDTO]:
- collection_map = {
- SpotifyObjectTypeEnum.ALBUM: AlbumCollectionDTO,
- SpotifyObjectTypeEnum.TRACK: TrackCollectionDTO,
- }
- factory_map = {
- SpotifyObjectTypeEnum.ALBUM: SpotifyAlbumFactory,
- SpotifyObjectTypeEnum.TRACK: SpotifyTrackFactory,
- }
- collection = collection_map.get(object_type)()
- item_factory = factory_map.get(object_type)
- for key, item_data in data:
- collection.append(item_factory.build(item_data))
- return collection
- class SpotifyResultFactory:
- @staticmethod
- def build(data: dict) -> SpotifyResultDTO:
- return SpotifyResultDTO(
- albums=SpotifyObjectCollectionFactory.build(data.get('albums'), 'album'),
- tracks=SpotifyObjectCollectionFactory.build(data.get('tracks'), 'track'),
- )
Add Comment
Please, Sign In to add comment