Skip to content

Commit

Permalink
Indent project to use 2 spaces instead of 4 for copying code into book
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed May 30, 2022
1 parent 7871dbf commit e879846
Show file tree
Hide file tree
Showing 74 changed files with 2,396 additions and 2,426 deletions.
70 changes: 35 additions & 35 deletions Sources/MusadoraKit/Add Resources/MusicAddResourcesRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ import MusicKit
/// A request that your app uses to add one or more catalog resources to a user’s iCloud Music Library.
/// You can add multiple types in the same request.
public struct MusicAddResourcesRequest {
private var types: [LibraryMusicItemType: [MusicItemID]]

/// Creates a request to add multiple resources to the user's iCloud Music Library using their identifiers.
public init(types: [LibraryMusicItemType: [MusicItemID]]) {
self.types = types
}

/// Adds the given types to the user's iCloud Music Library and returns a successful/failure response.
public func response() async throws -> Bool {
let url = try addResourcesEndpointURL
let request = MusicDataPostRequest(url: url)
let response = try await request.response()
return response.urlResponse.statusCode == 202
}
private var types: [LibraryMusicItemType: [MusicItemID]]

/// Creates a request to add multiple resources to the user's iCloud Music Library using their identifiers.
public init(types: [LibraryMusicItemType: [MusicItemID]]) {
self.types = types
}

/// Adds the given types to the user's iCloud Music Library and returns a successful/failure response.
public func response() async throws -> Bool {
let url = try addResourcesEndpointURL
let request = MusicDataPostRequest(url: url)
let response = try await request.response()
return response.urlResponse.statusCode == 202
}
}

extension MusicAddResourcesRequest {
internal var addResourcesEndpointURL: URL {
get throws {
var components = URLComponents()
var queryItems: [URLQueryItem] = []

components.scheme = "https"
components.host = "api.music.apple.com"
components.path = "/v1/me/library"

for (key, value) in types {
let values = value.map { $0.rawValue }.joined(separator: ",")
queryItems.append(URLQueryItem(name: key.type, value: values))
}

components.queryItems = queryItems

guard let url = components.url else {
throw URLError(.badURL)
}
return url
}
var addResourcesEndpointURL: URL {
get throws {
var components = URLComponents()
var queryItems: [URLQueryItem] = []

components.scheme = "https"
components.host = "api.music.apple.com"
components.path = "/v1/me/library"

for (key, value) in types {
let values = value.map { $0.rawValue }.joined(separator: ",")
queryItems.append(URLQueryItem(name: key.type, value: values))
}

components.queryItems = queryItems

guard let url = components.url else {
throw URLError(.badURL)
}
return url
}
}
}
31 changes: 15 additions & 16 deletions Sources/MusadoraKit/Add Resources/MusicDataDeleteRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
import Foundation

public struct MusicDataDeleteRequest {
/// The URL for the data request.
public var url: URL

/// The URL for the data request.
public var url: URL
/// Creates a data request with the given URL.
public init(url: URL) {
self.url = url
}

/// Creates a data request with the given URL.
public init(url: URL) {
self.url = url
}
/// Uploads data the Apple Music API endpoint that
/// the URL request defines.
public func response() async throws -> MusicDataPostResponse {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "DELETE"

/// Uploads data the Apple Music API endpoint that
/// the URL request defines.
public func response() async throws -> MusicDataPostResponse {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "DELETE"

var request = MusicTokenRequest(urlRequest: urlRequest)
let response = try await request.response()
return response
}
var request = MusicTokenRequest(urlRequest: urlRequest)
let response = try await request.response()
return response
}
}
31 changes: 15 additions & 16 deletions Sources/MusadoraKit/Add Resources/MusicDataPostRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ import MusicKit

/// A request for uploading data from an arbitrary Apple Music API endpoint.
public struct MusicDataPostRequest {
/// The URL for the data request.
public var url: URL

/// The URL for the data request.
public var url: URL
/// Creates a data request with the given URL.
public init(url: URL) {
self.url = url
}

/// Creates a data request with the given URL.
public init(url: URL) {
self.url = url
}
/// Uploads data the Apple Music API endpoint that
/// the URL request defines.
public func response() async throws -> MusicDataPostResponse {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"

/// Uploads data the Apple Music API endpoint that
/// the URL request defines.
public func response() async throws -> MusicDataPostResponse {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"

var request = MusicTokenRequest(urlRequest: urlRequest)
let response = try await request.response()
return response
}
var request = MusicTokenRequest(urlRequest: urlRequest)
let response = try await request.response()
return response
}
}
39 changes: 19 additions & 20 deletions Sources/MusadoraKit/Add Resources/MusicDataPutRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@
import Foundation

public struct MusicDataPutRequest {
/// The URL for the data request.
public var url: URL

/// The URL for the data request.
public var url: URL
/// Data to encode for the PUT request.
public var data: Data

/// Data to encode for the PUT request.
public var data: Data
/// Creates a data request with the given URL.
public init(url: URL, data: Data) {
self.url = url
self.data = data
}

/// Creates a data request with the given URL.
public init(url: URL, data: Data) {
self.url = url
self.data = data
}
/// Uploads data the Apple Music API endpoint that
/// the URL request defines.
public func response() async throws -> MusicDataPostResponse {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "PUT"
urlRequest.httpBody = data

/// Uploads data the Apple Music API endpoint that
/// the URL request defines.
public func response() async throws -> MusicDataPostResponse {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "PUT"
urlRequest.httpBody = data

var request = MusicTokenRequest(urlRequest: urlRequest)
let response = try await request.response()
return response
}
var request = MusicTokenRequest(urlRequest: urlRequest)
let response = try await request.response()
return response
}
}
24 changes: 12 additions & 12 deletions Sources/MusadoraKit/Add Resources/MusicTokenRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import Foundation
import MusicKit

public struct MusicTokenRequest {
public var urlRequest: URLRequest
public var urlRequest: URLRequest

public init(urlRequest: URLRequest) {
self.urlRequest = urlRequest
}
public init(urlRequest: URLRequest) {
self.urlRequest = urlRequest
}

public mutating func response() async throws -> MusicDataPostResponse {
let developerToken = try await MusicDataRequest.tokenProvider.developerToken(options: .ignoreCache)
let userToken = try await MusicDataRequest.tokenProvider.userToken(for: developerToken, options: .ignoreCache)
public mutating func response() async throws -> MusicDataPostResponse {
let developerToken = try await MusicDataRequest.tokenProvider.developerToken(options: .ignoreCache)
let userToken = try await MusicDataRequest.tokenProvider.userToken(for: developerToken, options: .ignoreCache)

urlRequest.addValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
urlRequest.addValue(userToken, forHTTPHeaderField: "Music-User-Token")
urlRequest.addValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
urlRequest.addValue(userToken, forHTTPHeaderField: "Music-User-Token")

let (data, response) = try await URLSession.shared.data(for: urlRequest)
return MusicDataPostResponse(data: data, urlResponse: response as! HTTPURLResponse)
}
let (data, response) = try await URLSession.shared.data(for: urlRequest)
return MusicDataPostResponse(data: data, urlResponse: response as! HTTPURLResponse)
}
}
13 changes: 6 additions & 7 deletions Sources/MusadoraKit/Add Resources/MusicTokenResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import Foundation

/// An object containing results for a post data request.
public struct MusicDataPostResponse {
/// The raw data returned by the Apple Music API endpoint
/// for the originating data request.
public let data: Data

/// The raw data returned by the Apple Music API endpoint
/// for the originating data request.
public let data: Data

/// The URL response returned by the Apple Music API endpoint
/// for the originating data request.
public let urlResponse: HTTPURLResponse
/// The URL response returned by the Apple Music API endpoint
/// for the originating data request.
public let urlResponse: HTTPURLResponse
}
112 changes: 58 additions & 54 deletions Sources/MusadoraKit/Catalog/CatalogAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,67 @@ import MusicKit
public typealias Albums = MusicItemCollection<Album>

public extension MusadoraKit {
/// Fetch an album from the Apple Music catalog by using its identifier.
/// - Parameters:
/// - id: The unique identifier for the album.
/// - properties: Additional relationships to fetch with the album.
/// - Returns: `Album` matching the given identifier.
static func catalogAlbum(id: MusicItemID,
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Album {
var request = MusicCatalogResourceRequest<Album>(matching: \.id, equalTo: id)
request.properties = properties
let response = try await request.response()

guard let album = response.items.first else {
throw MusadoraKitError.notFound(for: id.rawValue)
}
return album
}
/// Fetch an album from the Apple Music catalog by using its identifier.
/// - Parameters:
/// - id: The unique identifier for the album.
/// - properties: Additional relationships to fetch with the album.
/// - Returns: `Album` matching the given identifier.
static func catalogAlbum(id: MusicItemID,
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Album
{
var request = MusicCatalogResourceRequest<Album>(matching: \.id, equalTo: id)
request.properties = properties
let response = try await request.response()

/// Fetch one or more albums from the Apple Music catalog by using their identifiers.
/// - Parameters:
/// - ids: The unique identifiers for the albums.
/// - properties: Additional relationships to fetch with the albums.
/// - Returns: `Albums` matching the given identifiers.
static func catalogAlbums(ids: [MusicItemID],
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Albums {
var request = MusicCatalogResourceRequest<Album>(matching: \.id, memberOf: ids)
request.properties = properties
let response = try await request.response()
return response.items
guard let album = response.items.first else {
throw MusadoraKitError.notFound(for: id.rawValue)
}
return album
}

/// Fetch an album from Apple Music catalog by using their UPC value.
/// - Parameters:
/// - upc: The UPC (Universal Product Code) value for the album or single.
/// - properties: Additional relationships to fetch with the album.
/// - Returns: `Album` matching the given UPC value.
static func catalogAlbums(upc: String,
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Album {
var request = MusicCatalogResourceRequest<Album>(matching: \.upc, equalTo: upc)
request.properties = properties
let response = try await request.response()

guard let album = response.items.first else {
throw MusadoraKitError.notFound(for: upc)
}
return album
}
/// Fetch one or more albums from the Apple Music catalog by using their identifiers.
/// - Parameters:
/// - ids: The unique identifiers for the albums.
/// - properties: Additional relationships to fetch with the albums.
/// - Returns: `Albums` matching the given identifiers.
static func catalogAlbums(ids: [MusicItemID],
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Albums
{
var request = MusicCatalogResourceRequest<Album>(matching: \.id, memberOf: ids)
request.properties = properties
let response = try await request.response()
return response.items
}

/// Fetch multiple albums from Apple Music catalog by using their UPC values.
/// - Parameters:
/// - upcs: The UPC (Universal Product Code) values for the albums or singles.
/// - properties: Additional relationships to fetch with the albums.
/// - Returns: `Albums` matching the given UPC values.
static func catalogAlbums(upcs: [String],
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Albums {
var request = MusicCatalogResourceRequest<Album>(matching: \.upc, memberOf: upcs)
request.properties = properties
let response = try await request.response()
return response.items
/// Fetch an album from Apple Music catalog by using their UPC value.
/// - Parameters:
/// - upc: The UPC (Universal Product Code) value for the album or single.
/// - properties: Additional relationships to fetch with the album.
/// - Returns: `Album` matching the given UPC value.
static func catalogAlbums(upc: String,
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Album
{
var request = MusicCatalogResourceRequest<Album>(matching: \.upc, equalTo: upc)
request.properties = properties
let response = try await request.response()

guard let album = response.items.first else {
throw MusadoraKitError.notFound(for: upc)
}
return album
}

/// Fetch multiple albums from Apple Music catalog by using their UPC values.
/// - Parameters:
/// - upcs: The UPC (Universal Product Code) values for the albums or singles.
/// - properties: Additional relationships to fetch with the albums.
/// - Returns: `Albums` matching the given UPC values.
static func catalogAlbums(upcs: [String],
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Albums
{
var request = MusicCatalogResourceRequest<Album>(matching: \.upc, memberOf: upcs)
request.properties = properties
let response = try await request.response()
return response.items
}
}
Loading

0 comments on commit e879846

Please sign in to comment.