我对 swift 还很陌生,所以请耐心等待。我遇到了一个问题,这个应用程序在存档后在我的开发机器上运行良好,并且网守签名。但在其他用户机器上,它无法返回变量。我想让它工作,所以捕获的东西可能需要一些工作,就像我说我在 swift 上非常新/绿色
enum apiCalls: Error {
case badRequest
}
func CheckPortSpeeds() {
if NetMon.shared.isConnected == true {
guard let url = URL(string: MdnUrl) else {
return
}
var request = URLRequest(url: url )
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let data = ["jsonrpc": "2.0", "method": "runCmds", "params": ["version": 1, "cmds": ["enable", "show interfaces ethernet1/1-30/1 status"], "format": "json"], "id": 1] as [String : Any]
request.httpBody = try! JSONSerialization.data(withJSONObject: data, options: JSONSerialization.WritingOptions.prettyPrinted)
var response : JSON
var status : Int
do {
(response, status) = try APICaller().getResults(request: request)
}
catch apiCalls.badRequest {
status = 400
response = ["nil": "nil"]
Alerts.shared.AlertUser(Message: "Host Not Reachable", Info: "Api Module")
}
catch SwiftyJSONError.notExist {
status = 400
response = ["nil": "nil"]
Alerts.shared.AlertUser(Message: "Bad JSON response from host", Info: "Api Module")
}
catch {
status = 400
response = ["nil": "nil"]
Alerts.shared.AlertUser(Message: "Unknown Error", Info: "Api Module")
}
if status == 200 {
for n in 1...30 {
InterfaceDict["eth\(n)"] = Dictionary<String, String>()
InterfaceDict["eth\(n)"]?["portSpeed"] = response["result"][1]["interfaceStatuses"]["Ethernet\(n)/1"]["bandwidth"].int64
InterfaceDict["eth\(n)"]?["optic"] = response["result"][1]["interfaceStatuses"]["Ethernet\(n)/1"]["interfaceType"].string
guard let optic = InterfaceDict["eth\(n)"]?["optic"] as? String else {
return
}
InstalledOptic[n] = optic
guard let prtspd = InterfaceDict["eth\(n)"]?["portSpeed"] as? Int64 else {
return
}
PortSpeed[n] = prtspd
}
PortHandler().PortFlopper()
ContentView().replies.responses += 1
for r in 1...30 {
if PortMatch[r] == false {
FlipPorts(port: r, optic: InstalledOptic[r]!)
}
}
}
}
else{
Alerts.shared.AlertUser(Message: "Network connection is down, please check netwok conection and try again", Info: "Network Monitor")
}
}
以上基本上是设置api调用等。
下面是我对 Arista 交换机的 eapi 进行 api 调用的地方
import Cocoa
import SwiftyJSON
class APICaller: NSObject {
enum apiCalls: Error {
case badRequest
}
func getResults(request: URLRequest) throws -> (JSON, Int) {
var result: Result<(JSON, Int), Error> = .failure(SwiftyJSONError.notExist)
let group = DispatchGroup()
group.enter()
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
result = Result(catching: {
if let e = error { throw e }
guard let status = (response as? HTTPURLResponse)?.statusCode else {
throw apiCalls.badRequest
}
let json = try JSON(data: data ?? Data())
return (json, status)
})
group.leave()
}
task.resume()
group.wait()
return try result.get()
}
}
我已经尝试为此创建一个调度队列并在调度队列中以异步方式运行整个块,我尝试过同步,我尝试了一些疯狂的事情,就返回而言,它似乎不适用于用户机器变量,它似乎并没有等待。等待我的并且完美地工作......我在这里有点困惑,apicall部分已经在帮助下重写了几次,因为它最初锁定用户机器上的线程并触发线程安全,现在它只返回一个nil变量和弹出我的自定义错误消息...
请帮忙....