jellyflood/Shared/Objects/XtreamServer.swift

68 lines
1.7 KiB
Swift

//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2025 Jellyfin & Jellyfin Contributors
//
import Defaults
import Foundation
/// Represents an Xtream Codes server configuration
struct XtreamServer: Codable, Hashable, Identifiable {
var id: String
var name: String
var url: URL
var username: String
var password: String
init(
id: String = UUID().uuidString,
name: String,
url: URL,
username: String,
password: String
) {
self.id = id
self.name = name
self.url = url
self.username = username
self.password = password
}
/// Base API URL for this server
var apiURL: URL {
url.appendingPathComponent("player_api.php")
}
/// Construct authenticated API URL with username and password
func authenticatedURL(parameters: [URLQueryItem] = []) -> URL? {
var components = URLComponents(url: apiURL, resolvingAgainstBaseURL: false)
var queryItems = [
URLQueryItem(name: "username", value: username),
URLQueryItem(name: "password", value: password),
]
queryItems.append(contentsOf: parameters)
components?.queryItems = queryItems
return components?.url
}
}
// MARK: - Defaults Bridge
extension XtreamServer: Defaults.Serializable {}
extension Defaults.Keys {
/// Current active Xtream server
static let currentXtreamServerID = Key<String?>("currentXtreamServerID", default: nil)
/// All saved Xtream servers
static let xtreamServers = Key<[XtreamServer]>("xtreamServers", default: [])
}