SPB Git

Repository Public

Endpoints.swift 9.1 KB 131 lines swift

1// SP Music iOS2// File: Core/Sources/SPMusicCore/API/Endpoints.swift3// Description: Wire formats and typed calls of the endpoints the app uses.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import Foundation910// Wire formats of the endpoints the app uses. Field names are the server's.1112public struct SyncPage: Decodable, Sendable {13    public struct ArtistRow: Decodable, Sendable { public var id: String; public var name: String }14    public struct AlbumRow: Decodable, Sendable {15        public var id: String; public var title: String; public var year: Int?; public var artworkId: String?16        public var artistId: String; public var genre: String?; public var createdAt: Date?17    }18    public struct TrackRow: Decodable, Sendable {19        public var id: String; public var title: String; public var trackNo: Int?; public var discNo: Int; public var durationMs: Int; public var year: Int?20        public var codec: String; public var container: String; public var sampleRate: Int?; public var bitDepth: Int?; public var channels: Int?; public var bitrate: Int?21        public var compatState: String; public var artistId: String; public var albumId: String; public var genre: String?; public var lyricsState: String?22        public var createdAt: Date?; public var file: AssetInfo?; public var compat: AssetInfo?23    }24    public struct Tombstone: Decodable, Sendable { public var kind: String; public var id: String }25    public struct Counts: Decodable, Sendable { public var artists: Int; public var albums: Int; public var tracks: Int }26    public var since: Int6427    public var rev: Int6428    public var more: Bool29    public var artists: [ArtistRow]30    public var albums: [AlbumRow]31    public var tracks: [TrackRow]32    public var deleted: [Tombstone]33    public var counts: Counts34}3536public struct UserSync: Decodable, Sendable {37    public struct Entry: Decodable, Sendable { public var entryId: String; public var trackId: String; public var position: Int }38    public struct PlaylistRow: Decodable, Sendable { public var id: String; public var name: String; public var updatedAt: Date?; public var entries: [Entry] }39    public struct Ref: Decodable, Sendable { public var id: String; public var createdAt: Date? }40    public struct Playback: Codable, Sendable, Equatable {41        public var queue: [String]; public var index: Int; public var positionMs: Int; public var shuffle: Bool; public var `repeat`: String; public var volume: Double42        public var updatedAt: Date?43        public init(queue: [String], index: Int, positionMs: Int, shuffle: Bool, repeat: String, volume: Double = 1, updatedAt: Date? = nil) {44            self.queue = queue; self.index = index; self.positionMs = positionMs; self.shuffle = shuffle; self.repeat = `repeat`; self.volume = volume; self.updatedAt = updatedAt45        }46    }47    public var playlists: [PlaylistRow]48    public var favoriteTracks: [Ref]49    public var favoriteAlbums: [Ref]50    public var playback: Playback?51}5253public struct PlayEvent: Codable, Sendable, Equatable {54    public var id: String; public var trackId: String; public var playedAt: Date; public var listenedMs: Int; public var completed: Bool; public var source: String55    public init(id: String, trackId: String, playedAt: Date, listenedMs: Int, completed: Bool, source: String) {56        self.id = id; self.trackId = trackId; self.playedAt = playedAt; self.listenedMs = listenedMs; self.completed = completed; self.source = source57    }58}5960public struct PasskeyInfo: Decodable, Sendable, Identifiable, Hashable {61    public var id: String; public var name: String; public var deviceType: String?; public var backedUp: Bool; public var createdAt: Date?; public var lastUsedAt: Date?62}63public struct SessionRow: Decodable, Sendable, Identifiable, Hashable {64    public var id: String; public var method: String; public var userAgent: String; public var createdAt: Date?; public var lastSeenAt: Date?; public var expiresAt: Date?; public var current: Bool65}66public struct DeviceLinkStart: Decodable, Sendable { public var deviceCode: String; public var userCode: String; public var verificationUrl: String; public var expiresIn: Int; public var interval: Int }67public struct DeviceLinkPoll: Decodable, Sendable { public var status: String; public var sessionToken: String?; public var csrfToken: String? }6869public extension APIClient {70    private struct Items<T: Decodable & Sendable>: Decodable, Sendable { var items: [T] }71    private struct TrackState: Decodable, Sendable { var compatState: String }7273    // Catalogue74    func librarySync(since: Int64) async throws -> SyncPage { try await send("GET", "api/v1/library/sync", query: ["since": String(since)]) }75    func userSync() async throws -> UserSync { try await send("GET", "api/v1/library/sync/me") }76    func lyrics(trackId: String) async throws -> Lyrics { try await send("GET", "api/v1/library/tracks/\(trackId)/lyrics") }7778    // Favourites79    func setFavorite(kind: String, id: String, on: Bool) async throws {80        try await send(on ? "PUT" : "DELETE", "api/v1/library/favorites/\(kind)/\(id)", as: Empty.self)81    }8283    // Playlists84    struct Created: Decodable, Sendable { public var id: String }85    private struct NewPlaylist: Encodable, Sendable { var name: String; var trackIds: [String] }86    private struct Rename: Encodable, Sendable { var name: String }87    private struct AddTracks: Encodable, Sendable { var trackIds: [String] }88    private struct Order: Encodable, Sendable { var entryIds: [String] }89    func createPlaylist(name: String, trackIds: [String] = []) async throws -> String {90        try await send("POST", "api/v1/library/playlists", body: NewPlaylist(name: name, trackIds: trackIds), as: Created.self).id91    }92    func renamePlaylist(id: String, name: String) async throws { try await send("PATCH", "api/v1/library/playlists/\(id)", body: Rename(name: name), as: Empty.self) }93    func deletePlaylist(id: String) async throws { try await send("DELETE", "api/v1/library/playlists/\(id)", as: Empty.self) }94    func addToPlaylist(id: String, trackIds: [String]) async throws { try await send("POST", "api/v1/library/playlists/\(id)/tracks", body: AddTracks(trackIds: trackIds), as: Empty.self) }95    /// The whole new order, as entry identifiers; entries left out are removed.96    func orderPlaylist(id: String, entryIds: [String]) async throws { try await send("PUT", "api/v1/library/playlists/\(id)/order", body: Order(entryIds: entryIds), as: Empty.self) }9798    // Playback state and history99    func savePlayback(_ s: UserSync.Playback) async throws {100        struct Body: Encodable, Sendable { var queue: [String]; var index: Int; var positionMs: Int; var shuffle: Bool; var `repeat`: String; var volume: Double }101        try await send("PUT", "api/v1/library/playback", body: Body(queue: Array(s.queue.prefix(2000)), index: min(s.index, 1999), positionMs: max(0, s.positionMs), shuffle: s.shuffle, repeat: s.repeat, volume: s.volume), as: Empty.self)102    }103    func postHistory(_ events: [PlayEvent]) async throws {104        struct Body: Encodable, Sendable { var events: [PlayEvent] }105        try await send("POST", "api/v1/library/history", body: Body(events: events), as: Empty.self)106    }107    func history(limit: Int = 200) async throws -> [PlayEvent] {108        try await send("GET", "api/v1/library/history", query: ["limit": String(limit)], as: Items<PlayEvent>.self).items109    }110111    // AAC rendition112    /// Asks the server to prepare the AAC rendition of a track. Returns its state ("queued", "working", "ready"…).113    func requestRendition(trackId: String) async throws -> String { try await send("POST", "api/v1/library/tracks/\(trackId)/compat", as: TrackState.self).compatState }114    func renditionState(trackId: String) async throws -> String { try await send("GET", "api/v1/library/tracks/\(trackId)", as: TrackState.self).compatState }115116    // Account117    func passkeys() async throws -> [PasskeyInfo] { try await send("GET", "api/v1/me/passkeys", as: Items<PasskeyInfo>.self).items }118    func deletePasskey(id: String) async throws { try await send("DELETE", "api/v1/me/passkeys/\(id)", as: Empty.self) }119    func sessions() async throws -> [SessionRow] { try await send("GET", "api/v1/me/sessions", as: Items<SessionRow>.self).items }120    func revokeSession(id: String) async throws { try await send("DELETE", "api/v1/me/sessions/\(id)", as: Empty.self) }121    func logout() async throws { try await send("POST", "api/v1/auth/logout", as: Empty.self) }122123    // Sign-in without a passkey on this device124    private struct Recovery: Encodable, Sendable { var code: String }125    func loginWithRecoveryCode(_ code: String) async throws { try await send("POST", "api/v1/auth/recovery", body: Recovery(code: code), as: Empty.self) }126    private struct DeviceStart: Encodable, Sendable { var deviceName: String }127    private struct DevicePoll: Encodable, Sendable { var deviceCode: String }128    func startDeviceLink(deviceName: String) async throws -> DeviceLinkStart { try await send("POST", "api/v1/auth/device/start", body: DeviceStart(deviceName: deviceName)) }129    func pollDeviceLink(deviceCode: String) async throws -> DeviceLinkPoll { try await send("POST", "api/v1/auth/device/poll", body: DevicePoll(deviceCode: deviceCode)) }130}131
Files main
Menu

Theme