Files
suixinkan_uikit/suixinkan/Features/Live/API/LiveAPI.swift

195 lines
6.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// LiveAPI.swift
// suixinkan
//
import Foundation
/// manual-live view-album
@MainActor
protocol LiveServing {
///
func liveList(scenicId: String, page: Int, pageSize: Int) async throws -> LiveListResponse
///
func liveDetail(liveId: Int) async throws -> LiveItem
///
func createLive(scenicId: String, title: String, coverImg: String) async throws
///
func startLive(liveId: Int) async throws
///
func stopLive(liveId: Int) async throws
///
func finishLive(liveId: Int) async throws
///
func setPushMode(liveId: Int, mode: Int) async throws
///
func albumList(scenicId: String, startTime: String?, endTime: String?, page: Int, pageSize: Int) async throws -> LiveAlbumFolderListResponse
///
func createAlbumFolder(scenicId: String, name: String, items: [LiveAlbumFile]) async throws
///
func deleteAlbumFolder(folderId: Int) async throws
///
func albumFolderDetail(folderId: Int) async throws -> LiveAlbumFolder
///
func deleteAlbumFile(folderId: Int, fileId: Int) async throws
}
/// API Android `NetworkApi` alive/manual-live view-album
@MainActor
final class LiveAPI: LiveServing {
private let client: APIClient
/// API
init(client: APIClient) {
self.client = client
}
///
func liveList(scenicId: String, page: Int = 1, pageSize: Int = 10) async throws -> LiveListResponse {
try await client.send(
APIRequest(
method: .get,
path: "/api/app/manual-live/list",
queryItems: [
URLQueryItem(name: "scenic_id", value: scenicId),
URLQueryItem(name: "page", value: String(max(page, 1))),
URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
]
)
)
}
///
func liveDetail(liveId: Int) async throws -> LiveItem {
try await client.send(
APIRequest(
method: .get,
path: "/api/app/manual-live/detail",
queryItems: [URLQueryItem(name: "live_id", value: String(liveId))]
)
)
}
///
func createLive(scenicId: String, title: String, coverImg: String) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(
method: .post,
path: "/api/app/manual-live/create",
body: LiveCreateRequest(scenicId: scenicId, title: title, coverImg: coverImg)
)
)
}
///
func startLive(liveId: Int) async throws {
try await controlLive(path: "/api/app/manual-live/start", liveId: liveId)
}
///
func stopLive(liveId: Int) async throws {
try await controlLive(path: "/api/app/manual-live/stop", liveId: liveId)
}
///
func finishLive(liveId: Int) async throws {
try await controlLive(path: "/api/app/manual-live/finish", liveId: liveId)
}
///
func setPushMode(liveId: Int, mode: Int) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(
method: .post,
path: "/api/app/manual-live/set-push-mode",
body: LivePushModeRequest(liveId: liveId, manualPushMode: mode)
)
)
}
///
func albumList(
scenicId: String,
startTime: String? = nil,
endTime: String? = nil,
page: Int = 1,
pageSize: Int = 10
) async throws -> LiveAlbumFolderListResponse {
var items = [
URLQueryItem(name: "scenic_id", value: scenicId),
URLQueryItem(name: "page", value: String(max(page, 1))),
URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
]
if let startTime, !startTime.isEmpty {
items.append(URLQueryItem(name: "start_time", value: startTime))
}
if let endTime, !endTime.isEmpty {
items.append(URLQueryItem(name: "end_time", value: endTime))
}
return try await client.send(
APIRequest(method: .get, path: "/api/app/view-album/folders", queryItems: items)
)
}
///
func createAlbumFolder(scenicId: String, name: String, items: [LiveAlbumFile]) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(
method: .post,
path: "/api/app/view-album/create-folder",
body: LiveAlbumFolderCreateRequest(scenicId: scenicId, name: name, items: items)
)
)
}
///
func deleteAlbumFolder(folderId: Int) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(
method: .post,
path: "/api/app/view-album/delete-folder",
body: LiveAlbumFolderDeleteRequest(folderId: folderId)
)
)
}
///
func albumFolderDetail(folderId: Int) async throws -> LiveAlbumFolder {
try await client.send(
APIRequest(
method: .get,
path: "/api/app/view-album/folder-detail",
queryItems: [URLQueryItem(name: "folder_id", value: String(folderId))]
)
)
}
///
func deleteAlbumFile(folderId: Int, fileId: Int) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(
method: .post,
path: "/api/app/view-album/delete-files",
body: LiveAlbumFileDeleteRequest(folderId: folderId, fileIds: [fileId])
)
)
}
private func controlLive(path: String, liveId: Int) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(method: .post, path: path, body: LiveControlRequest(liveId: liveId))
)
}
}