I am still very new to swift, so bear with me. I am having an issue, where this app works fine on my dev machine after being archived, and gatekeeper signed. but on other users machines, it fails to return the variables. I wanted to get it working so the catch stuff probably needs some work, like I said im very new/green here on 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")
}
}
this above is basically setting up the api call etc..
this below is where im making the api call to an Arista switch's eapi
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()
}
}
I have tried creating a dispatch queue for this and running the entire block inside the dispatch queue in async, ive tried in sync, i've tried some crazy things, and it doesn't seems to work on user machines as far as returning the variable, it just doesn't seem to wait. waits on mine and works perfectly... Im kinda just confused here, the apicall portion has been rewritten several times with help, as it was originally locking the threads on the user machine and triggering thread safety, now it just returns a nil variable and pops up my custom error message...
please... help....