120 lines
3.4 KiB
Swift
120 lines
3.4 KiB
Swift
//
|
|
// SDLAPIClient+Network.swift
|
|
// punchnet
|
|
//
|
|
// Created by 安礼成 on 2026/3/24.
|
|
//
|
|
import Foundation
|
|
|
|
extension SDLAPIClient {
|
|
|
|
// 用来做临时的数据解析
|
|
struct NetworkContext: Codable {
|
|
let ip: String
|
|
let maskLen: UInt8
|
|
// 主机名称
|
|
let hostname: String
|
|
let identityId: UInt32
|
|
let resourceList: [Resource]
|
|
let nodeList: [Node]
|
|
|
|
// 资源列表
|
|
struct Resource: Codable {
|
|
var uuid = UUID().uuidString
|
|
var id: Int
|
|
var name: String
|
|
var url: String
|
|
var connectionStatus: String
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case name
|
|
case url
|
|
case connectionStatus = "connection_status"
|
|
}
|
|
}
|
|
|
|
// 设备列表
|
|
struct Node: Codable {
|
|
var id: Int
|
|
var name: String
|
|
var ip: String
|
|
var system: String?
|
|
var connectionStatus: String
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(id)
|
|
}
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case name
|
|
case ip
|
|
case system
|
|
case connectionStatus = "connection_status"
|
|
}
|
|
|
|
static func == (lhs: Self, rhs: Self) -> Bool {
|
|
return lhs.id == rhs.id
|
|
}
|
|
}
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case ip
|
|
case maskLen = "mask_len"
|
|
case hostname
|
|
case identityId = "identity_id"
|
|
case resourceList = "resource_list"
|
|
case nodeList = "node_list"
|
|
}
|
|
|
|
static func `default`() -> Self {
|
|
return .init(ip: "", maskLen: 24, hostname: "", identityId: 0, resourceList: [], nodeList: [])
|
|
}
|
|
|
|
// 节点详情
|
|
struct NodeDetail: Codable {
|
|
let id: Int
|
|
let name: String
|
|
let ip: String
|
|
let system: String?
|
|
let connectionStatus: String
|
|
let resourceList: [Resource]
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case name
|
|
case ip
|
|
case system
|
|
case connectionStatus = "connection_status"
|
|
case resourceList = "resource_list"
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
static func connectNetwork(accesToken: String) async throws -> NetworkContext {
|
|
let params: [String: Any] = [
|
|
"client_id": SystemConfig.getClientId(),
|
|
"access_token": accesToken
|
|
]
|
|
|
|
return try await SDLAPIClient.doPost(path: "/connect", params: params, as: NetworkContext.self)
|
|
}
|
|
|
|
static func loadNodeResources(accesToken: String, id: Int) async -> [NetworkContext.Resource] {
|
|
let params: [String: Any] = [
|
|
"client_id": SystemConfig.getClientId(),
|
|
"access_token": accesToken,
|
|
"id": id
|
|
]
|
|
|
|
if let detail = try? await SDLAPIClient.doPost(path: "/get_node_resources", params: params, as: NetworkContext.NodeDetail.self) {
|
|
return detail.resourceList
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
}
|