128 lines
5.9 KiB
Swift
128 lines
5.9 KiB
Swift
import Foundation
|
|
|
|
public struct MusicAPI: Sendable {
|
|
public let client: APIClient
|
|
public init(client: APIClient = APIClient()) { self.client = client }
|
|
|
|
private func q(_ pairs: [(String, String?)]) -> [URLQueryItem] {
|
|
pairs.compactMap { key, value in value.map { URLQueryItem(name: key, value: $0) } }
|
|
}
|
|
private func page(_ limit: Int?, _ skip: Int?) -> [URLQueryItem] {
|
|
q([("limit", limit.map(String.init)), ("skip", skip.map(String.init))])
|
|
}
|
|
|
|
public func signIn(email: String, password: String) async throws {
|
|
let _: APIClient.EmptyResponse = try await client.request(
|
|
"POST", path: "auth/signin", body: Credentials(email: email, password: password))
|
|
guard try await sessionStatus().authenticated else {
|
|
throw APIError.http(
|
|
status: 401, message: "The server did not establish an authenticated session.")
|
|
}
|
|
}
|
|
public func sessionStatus() async throws -> SessionStatus {
|
|
try await client.request("GET", path: "auth/session")
|
|
}
|
|
public func renewSession() async throws -> Session {
|
|
try await client.request("POST", path: "auth/session")
|
|
}
|
|
public func signOut() async throws -> SignOutResponse {
|
|
try await client.request("POST", path: "auth/signout")
|
|
}
|
|
public func currentUser() async throws -> User { try await client.request("GET", path: "user") }
|
|
|
|
public func search(_ query: String, type: SearchType = .default, limit: Int? = nil) async throws
|
|
-> SearchResults
|
|
{
|
|
try await client.request(
|
|
"GET", path: "search",
|
|
query: q([("q", query), ("type", type.rawValue), ("limit", limit.map(String.init))]))
|
|
}
|
|
public func randomGenres(limit: Int? = nil) async throws -> [Genre] {
|
|
try await client.request(
|
|
"GET", path: "genres/random", query: q([("limit", limit.map(String.init))]))
|
|
}
|
|
public func album(id: String) async throws -> Album? {
|
|
try await client.request("GET", path: "albums/\(id)")
|
|
}
|
|
public func artist(id: String) async throws -> Artist? {
|
|
try await client.request("GET", path: "artists/\(id)")
|
|
}
|
|
public func track(id: String) async throws -> Track? {
|
|
try await client.request("GET", path: "tracks/\(id)")
|
|
}
|
|
public func lyrics(trackID: String) async throws -> Lyrics? {
|
|
try await client.request("GET", path: "tracks/\(trackID)/lyrics")
|
|
}
|
|
public func albumTracks(id: String, limit: Int? = nil, skip: Int? = nil) async throws -> [Track]
|
|
{ try await client.request("GET", path: "albums/\(id)/tracks", query: page(limit, skip)) }
|
|
public func artistTracks(id: String, limit: Int? = nil, skip: Int? = nil) async throws
|
|
-> [Track]
|
|
{ try await client.request("GET", path: "artists/\(id)/tracks", query: page(limit, skip)) }
|
|
public func artistAlbums(id: String, limit: Int? = nil, skip: Int? = nil) async throws
|
|
-> [Album]
|
|
{ try await client.request("GET", path: "artists/\(id)/albums", query: page(limit, skip)) }
|
|
public func genreAlbums(id: String, limit: Int? = nil, skip: Int? = nil) async throws -> [Album]
|
|
{ try await client.request("GET", path: "genres/albums/\(id)", query: page(limit, skip)) }
|
|
public func genreTracks(id: String, limit: Int? = nil, skip: Int? = nil) async throws -> [Track]
|
|
{ try await client.request("GET", path: "genres/tracks/\(id)", query: page(limit, skip)) }
|
|
public func allAlbums() async throws -> [Album] {
|
|
try await client.request("GET", path: "all/albums")
|
|
}
|
|
public func allArtists() async throws -> [Artist] {
|
|
try await client.request("GET", path: "all/artists")
|
|
}
|
|
public func allGenres() async throws -> [Genre] {
|
|
try await client.request("GET", path: "all/genres")
|
|
}
|
|
public func allTracks() async throws -> [Track] {
|
|
try await client.request("GET", path: "all/tracks")
|
|
}
|
|
public func savedTracks() async throws -> [Track] {
|
|
try await client.request("GET", path: "user/tracks")
|
|
}
|
|
public func savedAlbums() async throws -> [Album] {
|
|
try await client.request("GET", path: "user/albums")
|
|
}
|
|
public func savedArtists() async throws -> [Artist] {
|
|
try await client.request("GET", path: "user/artists")
|
|
}
|
|
public func isTrackSaved(id: String) async throws -> Bool {
|
|
let membership: TrackMembership = try await client.request(
|
|
"GET",
|
|
path: "user/tracks/contains",
|
|
query: q([("id", id)])
|
|
)
|
|
return membership.contains
|
|
}
|
|
public func saveTrack(id: String) async throws {
|
|
let _: APIClient.EmptyResponse = try await client.request(
|
|
"PATCH", path: "user/tracks", query: q([("id", id)]))
|
|
}
|
|
public func removeTrack(id: String) async throws {
|
|
let _: APIClient.EmptyResponse = try await client.request(
|
|
"DELETE", path: "user/tracks", query: q([("id", id)]))
|
|
}
|
|
public func removeTracks(ids: [String]) async throws {
|
|
let _: APIClient.EmptyResponse = try await client.request(
|
|
"DELETE", path: "user/tracks", query: ids.map { URLQueryItem(name: "ids", value: $0) })
|
|
}
|
|
public func tracks(ids: [String]) async throws -> [Track] {
|
|
try await client.request(
|
|
"GET", path: "tracks", query: ids.map { URLQueryItem(name: "ids", value: $0) })
|
|
}
|
|
public func albums(ids: [String]) async throws -> [Album] {
|
|
try await client.request(
|
|
"GET", path: "albums", query: ids.map { URLQueryItem(name: "ids", value: $0) })
|
|
}
|
|
public func artists(ids: [String]) async throws -> [Artist] {
|
|
try await client.request(
|
|
"GET", path: "artists", query: ids.map { URLQueryItem(name: "ids", value: $0) })
|
|
}
|
|
}
|
|
|
|
nonisolated private struct Credentials: Encodable, Sendable {
|
|
let email: String
|
|
let password: String
|
|
}
|
|
nonisolated public struct SignOutResponse: Decodable, Sendable { public let success: Bool? }
|