Skip to content

Commit 08a794f

Browse files
committed
Extract JSONDecoder configuration into shared helper method
Added private helper method decodePlaylists(from:) to avoid duplicating JSONDecoder instantiation and configuration across multiple functions. This improves maintainability by centralizing the decoder configuration (.iso8601 dateDecodingStrategy) in a single location. Updated playlists(limit:) and madeForYouPlaylists() to use the new helper.
1 parent fbd9649 commit 08a794f

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Sources/MusadoraKit/Library/MLibrary+Playlist.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,15 @@ public extension MLibrary {
242242
return LibraryPlaylists([])
243243
}
244244

245-
var playlists: LibraryPlaylists = []
246-
let decoder = JSONDecoder()
247-
decoder.dateDecodingStrategy = .iso8601
248-
245+
let playlists: LibraryPlaylists
249246
if let userToken = MusadoraKit.userToken {
250247
let request = MusicUserRequest(urlRequest: .init(url: url), userToken: userToken)
251248
let data = try await request.response()
252-
playlists = try decoder.decode(LibraryPlaylists.self, from: data)
249+
playlists = try decodePlaylists(from: data)
253250
} else {
254251
let request = MusicDataRequest(urlRequest: .init(url: url))
255252
let response = try await request.response()
256-
257-
playlists = try decoder.decode(LibraryPlaylists.self, from: response.data)
253+
playlists = try decodePlaylists(from: response.data)
258254
}
259255
return try await playlists.collectingAll(upTo: limit)
260256
}
@@ -295,11 +291,7 @@ public extension MLibrary {
295291

296292
let request = MusicDataRequest(urlRequest: .init(url: url))
297293
let response = try await request.response()
298-
299-
let decoder = JSONDecoder()
300-
decoder.dateDecodingStrategy = .iso8601
301-
let playlists = try decoder.decode(LibraryPlaylists.self, from: response.data)
302-
294+
let playlists = try decodePlaylists(from: response.data)
303295
return try await playlists.collectingAll()
304296
}
305297

@@ -314,6 +306,12 @@ public extension MLibrary {
314306

315307
return components.url
316308
}
309+
310+
private static func decodePlaylists(from data: Data) throws -> LibraryPlaylists {
311+
let decoder = JSONDecoder()
312+
decoder.dateDecodingStrategy = .iso8601
313+
return try decoder.decode(LibraryPlaylists.self, from: data)
314+
}
317315
}
318316

319317
public extension Playlist {

0 commit comments

Comments
 (0)