refactored code structure
This commit is contained in:
34
Sources/Model/API/PingService.swift
Normal file
34
Sources/Model/API/PingService.swift
Normal file
@@ -0,0 +1,34 @@
|
||||
import Foundation
|
||||
|
||||
enum PingService {
|
||||
static func ping(hostname: String, apiKey: String) async -> Bool {
|
||||
guard let url = URL(string: "https://\(hostname)/api/v2/ping") else {
|
||||
print("❌ [PingService] Invalid URL for \(hostname)")
|
||||
return false
|
||||
}
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue(apiKey, forHTTPHeaderField: "X-API-KEY")
|
||||
request.timeoutInterval = 10
|
||||
|
||||
do {
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
|
||||
if let responseString = String(data: data, encoding: .utf8) {
|
||||
print("❌ [PingService] HTTP \(httpResponse.statusCode): \(responseString)")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if let result = try? JSONDecoder().decode([String: String].self, from: data), result["response"] == "pong" {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} catch {
|
||||
print("❌ [PingService] Error pinging \(hostname): \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user