Skip to content

Commit

Permalink
Update all genres and storefronts
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Mar 11, 2023
1 parent 635a2a8 commit 1bc0298
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Sources/MusadoraKit/Catalog/CatalogGenre.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ extension MCatalog {
}
}

extension MCatalog {
public extension MCatalog {
/// Fetch all genres from the Apple Music catalog across all Apple Music storefronts.
///
/// - Returns: Unique `Genres` from all storefronts.
static func allGenres() async throws -> Genres {
try await withThrowingTaskGroup(of: Genres.self) { group in
let storefronts = try await MCatalog.storefronts().map { $0.id }
Expand Down
13 changes: 9 additions & 4 deletions Sources/MusadoraKit/Storefronts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//

import MusicKit
import StoreKit
import Foundation

public struct Storefronts: Codable {
public typealias Storefronts = [StorefrontsData.Storefront]

public struct StorefrontsData: Codable {
let data: [Storefront]

public struct Storefront: Codable {
Expand Down Expand Up @@ -36,11 +38,14 @@ public struct Storefronts: Codable {
}

public extension MCatalog {
static func storefronts() async throws -> [Storefronts.Storefront] {
/// Fetch all storefronts of Apple Music.
///
/// - Returns: `Storefronts` as array of storefronts.
static func storefronts() async throws -> Storefronts {
let url = URL(string: "https://api.music.apple.com/v1/storefronts")!
let request = MusicDataRequest(urlRequest: .init(url: url))
let response = try await request.response()
let storefronts = try JSONDecoder().decode(Storefronts.self, from: response.data)
let storefronts = try JSONDecoder().decode(StorefrontsData.self, from: response.data)
return storefronts.data
}
}

0 comments on commit 1bc0298

Please sign in to comment.