Skip to content

Commit bcb454a

Browse files
authored
Make lowercasing album preserialization a hook (#327)
1 parent 6443626 commit bcb454a

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/spotifyaio/models.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@
1212
from mashumaro.types import Discriminator, SerializationStrategy
1313

1414

15-
class LowercaseAlbumTypeSerializationStrategy(SerializationStrategy):
16-
"""Serialization strategy for objects encapsulated in items."""
17-
18-
def serialize(self, value: AlbumType) -> str:
19-
"""Serialize optional string."""
20-
return value
21-
22-
def deserialize(self, value: str) -> AlbumType:
23-
"""Deserialize optional string."""
24-
return AlbumType(value.lower())
25-
26-
2715
class DeviceType(StrEnum):
2816
"""Device type."""
2917

@@ -132,11 +120,7 @@ def deserialize(self, value: dict[str, Any]) -> list[Any]:
132120
class SimplifiedAlbum(DataClassORJSONMixin):
133121
"""Album model."""
134122

135-
album_type: AlbumType = field(
136-
metadata=field_options(
137-
serialization_strategy=LowercaseAlbumTypeSerializationStrategy()
138-
)
139-
)
123+
album_type: AlbumType
140124
total_tracks: int
141125
album_id: str = field(metadata=field_options(alias="id"))
142126
images: list[Image]
@@ -146,6 +130,14 @@ class SimplifiedAlbum(DataClassORJSONMixin):
146130
uri: str
147131
artists: list[SimplifiedArtist]
148132

133+
@classmethod
134+
def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
135+
"""Pre deserialize hook."""
136+
return {
137+
**d,
138+
"album_type": d["album_type"].lower(),
139+
}
140+
149141

150142
@dataclass
151143
class Album(SimplifiedAlbum):

src/spotifyaio/spotify.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ async def _put(
143143

144144
async def get_album(self, album_id: str) -> Album:
145145
"""Get album."""
146-
response = await self._get(f"v1/albums/{album_id}")
146+
identifier = album_id.split(":")[-1]
147+
response = await self._get(f"v1/albums/{identifier}")
147148
return Album.from_json(response)
148149

149150
async def get_artist(self, artist_id: str) -> Artist:

0 commit comments

Comments
 (0)