//
//  TSNetworkManager.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2025/1/13.
//

class TSNetworkManager {

    /// 发送 POST 请求
    /// - Parameters:
    ///   - urlString: 请求的 URL 字符串
    ///   - parameters: 请求的参数字典（将自动转换为 JSON）
    ///   - timeout: 超时时间（默认 30 秒）
    ///   - completion: 请求完成的回调，返回 `Data?` 和 `Error?`
    static func postRequest(
        urlString: String,
        httpBody: Data?,
        timeout: TimeInterval = 90,
        completion: @escaping (Data?, Error?) -> Void
    ) {
        // 确保 URL 有效
        guard let url = URL(string: urlString) else {
            completion(nil, NSError(domain: "Invalid URL", code: -1, userInfo: nil))
            return
        }
        debugPrint("postRequest urlString=",urlString)
        // 创建请求
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.timeoutInterval = timeout
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = httpBody
  
        // 创建数据任务
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            completion(data, error)
        }
        
        // 启动任务
        task.resume()
    }
}
