Skip to content

Commit

Permalink
Add MusadoraLabs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed May 30, 2022
1 parent e879846 commit 4a18b18
Show file tree
Hide file tree
Showing 26 changed files with 833 additions and 544 deletions.
26 changes: 13 additions & 13 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import PackageDescription

let package = Package(
name: "MusadoraKit",
platforms: [.iOS(.v15), .macOS(.v12), .watchOS(.v8), .tvOS(.v15)],
products: [
.library(name: "MusadoraKit", type: .static, targets: ["MusadoraKit"]),
.library(name: "MusadoraLabsKit", type: .static, targets: ["MusadoraLabsKit"]),
],
dependencies: [],
targets: [
.target(name: "MusadoraKit", dependencies: []),
.testTarget(name: "MusadoraKitTests", dependencies: ["MusadoraKit"]),
.target(name: "MusadoraLabsKit", dependencies: []),
.testTarget(name: "MusadoraLabsKitTests", dependencies: ["MusadoraLabsKit"]),
]
name: "MusadoraKit",
platforms: [.iOS(.v15), .macOS(.v12), .watchOS(.v8), .tvOS(.v15)],
products: [
.library(name: "MusadoraKit", type: .static, targets: ["MusadoraKit"]),
.library(name: "MusadoraLabsKit", type: .static, targets: ["MusadoraLabsKit"]),
],
dependencies: [],
targets: [
.target(name: "MusadoraKit", dependencies: []),
.testTarget(name: "MusadoraKitTests", dependencies: ["MusadoraKit"]),
.target(name: "MusadoraLabsKit", dependencies: []),
.testTarget(name: "MusadoraLabsKitTests", dependencies: ["MusadoraLabsKit"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// AlbumCatalogEndpoint.swift
// AlbumCatalogEndpoint
//
// Created by Rudrank Riyam on 19/04/22.
//

import Foundation

// MARK: - Requesting Catalog Albums

public enum AlbumCatalogEndpoint {
case id(id: String)
case ids(ids: [String])
case upcs(upcs: [String])
}

// MARK: - Endpoint

extension AlbumCatalogEndpoint: Endpoint {
public var name: String {
switch self {
case .id:
return "Get a Catalog Album"
case .ids:
return "Get Multiple Catalog Albums"
case .upcs:
return "Get Multiple Catalog Albums by UPC"
}
}

public var description: String {
switch self {
case .id:
return "Fetch an album by using its identifier."
case .ids:
return "Fetch one or more albums by using their identifiers."
case .upcs:
return "Fetch one or more albums by using their UPC values."
}
}

var previewURL: String {
switch self {
case .id:
return "https://api.music.apple.com/v1/catalog/{storefront}/albums/{id}"
case .ids:
return "https://api.music.apple.com/v1/catalog/{storefront}/albums?ids={ids}"
case .upcs:
return "https://api.music.apple.com/v1/catalog/{storefront}/albums?filter[upc]={upcs}"
}
}

var url: URL {
get async throws {
switch self {
case let .id(id):
return try await MusadoraLabsKit.catalogAlbum(id: id)
case let .ids(ids):
return try await MusadoraLabsKit.catalogAlbums(ids: ids)
case let .upcs(upcs):
return try await MusadoraLabsKit.catalogAlbums(upcs: upcs)
}
}
}
}

// MARK: - Identifiable

extension AlbumCatalogEndpoint: Hashable, Identifiable {
public var id: Self { self }
}

// MARK: - CaseIterable

extension AlbumCatalogEndpoint: CaseIterable {
public static var allCases: [AlbumCatalogEndpoint] = [.id(id: ""), .ids(ids: []), .upcs(upcs: [])]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// AlbumsEndpoint.swift
// AlbumsEndpoint
//
// Created by Rudrank Riyam on 18/04/22.
//

import Foundation

public extension MusadoraLabsKit {
// MARK: - Catalog Albums

static func catalogAlbum(id: String, storeFront: String? = nil) async throws -> URL {
try await MusadoraLabsKit(library: .catalog, path: MusicItemPath.albums.id(id), storeFront: storeFront).url
}

static func catalogAlbums(ids: [String], storeFront: String? = nil) async throws -> URL {
let query = URLQueryItem(name: "ids", value: ids.joined(separator: ","))
return try await MusadoraLabsKit(library: .catalog, path: .albums, storeFront: storeFront, queryItems: [query]).url
}

static func catalogAlbums(upcs: [String], storeFront: String? = nil) async throws -> URL {
let query = URLQueryItem(name: "filter[upc]", value: upcs.joined(separator: ","))
return try await MusadoraLabsKit(library: .catalog, path: .albums, storeFront: storeFront, queryItems: [query]).url
}

// MARK: - Library Albums

static func libraryAlbum(id: String) async throws -> URL {
try await MusadoraLabsKit(library: .library, path: MusicItemPath.albums.id(id)).url
}

static func libraryAlbums(ids: [String]) async throws -> URL {
let query = URLQueryItem(name: "ids", value: ids.joined(separator: ","))
return try await MusadoraLabsKit(library: .library, path: .albums, queryItems: [query]).url
}

static func libraryAlbums() async throws -> URL {
try await MusadoraLabsKit(library: .library, path: .albums).url
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// AlbumLibraryEndpoint.swift
// AlbumLibraryEndpoint
//
// Created by Rudrank Riyam on 20/04/22.
//

import Foundation

// MARK: - Requesting User Library Albums

enum AlbumLibraryEndpoint {
case all
case id(id: String)
case ids(ids: [String])
}

// MARK: - Endpoint

extension AlbumLibraryEndpoint: Endpoint {
var name: String {
switch self {
case .all:
return "Get All Library Albums"
case .id:
return "Get a Library Album"
case .ids:
return "Get Multiple Library Albums"
}
}

var description: String {
switch self {
case .all:
return "Fetch all the library albums in alphabetical order."
case .id:
return "Fetch a library album by using its identifier."
case .ids:
return "Fetch one or more library albums by using their identifiers."
}
}

var previewURL: String {
switch self {
case .all:
return "https://api.music.apple.com/v1/me/library/albums/{id}"
case .id:
return "https://api.music.apple.com/v1/me/library/albums?ids={ids}"
case .ids:
return "https://api.music.apple.com/v1/me/library/albums"
}
}

var url: URL {
get async throws {
switch self {
case .all:
return try await MusadoraLabsKit.libraryAlbums()
case let .id(id):
return try await MusadoraLabsKit.libraryAlbum(id: id)
case let .ids(ids):
return try await MusadoraLabsKit.libraryAlbums(ids: ids)
}
}
}
}

// MARK: - Identifiable

extension AlbumLibraryEndpoint: Hashable, Identifiable {
public var id: Self { self }
}

// MARK: - CaseIterable

// extension AlbumLibraryEndpoint: CaseIterable {
// static public var allCases: [AlbumCatalogEndpoint] = [.all, .id(id: ""), .ids(ids: [])]
// }
39 changes: 0 additions & 39 deletions Sources/MusadoraLabsKit/Apple Music Endpoint/AlbumsEndpoint.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ import Foundation
import MusicKit

extension MusadoraLabsKit {
var url: URL {
get async throws {
var components = URLComponents()
components.scheme = "https"
components.host = "api.music.apple.com"
components.path = library.path
var url: URL {
get async throws {
var components = URLComponents()
components.scheme = "https"
components.host = "api.music.apple.com"
components.path = library.path

if let queryItems = queryItems {
components.queryItems = queryItems
}
if let queryItems = queryItems {
components.queryItems = queryItems
}

switch library {
case .catalog:
if let storeFront = storeFront {
components.path += storeFront + "/" + path
} else {
let storeFront = try await MusicDataRequest.currentCountryCode
components.path += storeFront + "/" + path
}
case .user, .library: components.path += path
}

guard let url = components.url else {
preconditionFailure("Invalid URL components: \(components)")
}
return url
switch library {
case .catalog:
if let storeFront = storeFront {
components.path += storeFront + "/" + path
} else {
let storeFront = try await MusicDataRequest.currentCountryCode
components.path += storeFront + "/" + path
}
case .user, .library: components.path += path
}

guard let url = components.url else {
preconditionFailure("Invalid URL components: \(components)")
}
return url
}
}
}
48 changes: 25 additions & 23 deletions Sources/MusadoraLabsKit/Apple Music Endpoint/ArtistsEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@
import Foundation

extension MusadoraLabsKit {
// MARK: - Catalog Artists
static func catalogArtist(id: String, storeFront: String? = nil) async throws -> URL {
try await MusadoraLabsKit(library: .catalog, path: MusicItemPath.artists.id(id), storeFront: storeFront).url
}

static func catalogArtists(ids: [String], storeFront: String? = nil) async throws -> URL {
let query = URLQueryItem(name: "ids", value: ids.joined(separator: ","))
return try await MusadoraLabsKit(library: .catalog, path: .artists, storeFront: storeFront, queryItems: [query]).url
}

// MARK: - Library Artists
static func libraryArtist(id: String) async throws -> URL {
try await MusadoraLabsKit(library: .library, path: MusicItemPath.artists.id(id)).url
}

static func libraryArtists(ids: [String]) async throws -> URL {
let query = URLQueryItem(name: "ids", value: ids.joined(separator: ","))
return try await MusadoraLabsKit(library: .library, path: .artists, queryItems: [query]).url
}

static func libraryArtists() async throws -> URL {
try await MusadoraLabsKit(library: .library, path: .artists).url
}
// MARK: - Catalog Artists

static func catalogArtist(id: String, storeFront: String? = nil) async throws -> URL {
try await MusadoraLabsKit(library: .catalog, path: MusicItemPath.artists.id(id), storeFront: storeFront).url
}

static func catalogArtists(ids: [String], storeFront: String? = nil) async throws -> URL {
let query = URLQueryItem(name: "ids", value: ids.joined(separator: ","))
return try await MusadoraLabsKit(library: .catalog, path: .artists, storeFront: storeFront, queryItems: [query]).url
}

// MARK: - Library Artists

static func libraryArtist(id: String) async throws -> URL {
try await MusadoraLabsKit(library: .library, path: MusicItemPath.artists.id(id)).url
}

static func libraryArtists(ids: [String]) async throws -> URL {
let query = URLQueryItem(name: "ids", value: ids.joined(separator: ","))
return try await MusadoraLabsKit(library: .library, path: .artists, queryItems: [query]).url
}

static func libraryArtists() async throws -> URL {
try await MusadoraLabsKit(library: .library, path: .artists).url
}
}
15 changes: 15 additions & 0 deletions Sources/MusadoraLabsKit/Apple Music Endpoint/Endpoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Endpoint.swift
// Endpoint
//
// Created by Rudrank Riyam on 19/04/22.
//

import Foundation

protocol Endpoint {
var name: String { get }
var description: String { get }
var previewURL: String { get }
var url: URL { get async throws }
}
Loading

0 comments on commit 4a18b18

Please sign in to comment.