diff --git a/dcbattwebhook-swift.xcodeproj/project.xcworkspace/xcuserdata/Stella.xcuserdatad/UserInterfaceState.xcuserstate b/dcbattwebhook-swift.xcodeproj/project.xcworkspace/xcuserdata/Stella.xcuserdatad/UserInterfaceState.xcuserstate index 1a431b3..67dded2 100644 Binary files a/dcbattwebhook-swift.xcodeproj/project.xcworkspace/xcuserdata/Stella.xcuserdatad/UserInterfaceState.xcuserstate and b/dcbattwebhook-swift.xcodeproj/project.xcworkspace/xcuserdata/Stella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/dcbattwebhook-swift/SendBatteryInfo.swift b/dcbattwebhook-swift/SendBatteryInfo.swift index ddef429..d6ed738 100644 --- a/dcbattwebhook-swift/SendBatteryInfo.swift +++ b/dcbattwebhook-swift/SendBatteryInfo.swift @@ -46,38 +46,73 @@ func sendInfo(isCurrentlyCharging: Bool, didGetPluggedIn: Bool, didGetUnplugged: let jsonData = try! jsonEncoder.encode(fullmessageBlock) // if i need to print the encoded json to the console for inspecting later - let jsonString = String(data: jsonData, encoding: .utf8) + //let jsonString = String(data: jsonData, encoding: .utf8) - // our actual post - let webhookURL = URL(string: userWebhookUrl.trimmingCharacters(in: .whitespacesAndNewlines))! // grab the url from user settings - var request = URLRequest(url: webhookURL) // create a urlrequest object with the grabbed url as the url - request.httpMethod = "POST" // make it a POST - request.addValue("application/json", forHTTPHeaderField: "content-type") // make sure its json - request.httpBody = jsonData // add the prepped json data as the body - // no idea what this shit does but it works rofl - // update 07/30/2023 I might know what this does but i need time to mess with it - let sem = DispatchSemaphore.init(value: 0) + // Scuffed multi-webhook functionality until I properly implement this in Battery Webhook's rewrite + let webhookUrls = userWebhookUrl.split(separator: "@") + if (webhookUrls.count == 1) { + NSLog("Sending to one webhook, because that's normal") + // our actual post, just once + let webhookURL = URL(string: webhookUrls[0].trimmingCharacters(in: .whitespacesAndNewlines))! // grab the url + var request = URLRequest(url: webhookURL) // create a urlrequest object with the grabbed url as the url + request.httpMethod = "POST" // make it a POST + request.addValue("application/json", forHTTPHeaderField: "content-type") // make sure its json + request.httpBody = jsonData // add the prepped json data as the body - let task = URLSession.shared.dataTask(with: request) { data, response, error in - defer { sem.signal() } - guard let data = data, error == nil else { - returnErr = true - returnErrMsg = error?.localizedDescription ?? "No data" - print(error?.localizedDescription ?? "No data") - return + let sem = DispatchSemaphore.init(value: 0) + + let task = URLSession.shared.dataTask(with: request) { data, response, error in + defer { sem.signal() } + guard let data = data, error == nil else { + returnErr = true + returnErrMsg = error?.localizedDescription ?? "No data" + print(error?.localizedDescription ?? "No data") + return + } + let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) + if let responseJSON = responseJSON as? [String: Any] { + //print(responseJSON) + let jsonErrString = String(data: data, encoding: .utf8) + returnErr = true + returnErrMsg = jsonErrString ?? "there was an error while getting the error text" + } } - let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) - if let responseJSON = responseJSON as? [String: Any] { - //print(responseJSON) - let jsonErrString = String(data: data, encoding: .utf8) - returnErr = true - returnErrMsg = jsonErrString ?? "there was an error while getting the error text" + + task.resume() + sem.wait() + } else { + // loop through each url, somehow retaining error handling + NSLog("Sending to multiple webhooks by looping") + let sem = DispatchSemaphore.init(value: 0) + for url in webhookUrls { + NSLog("Sending to \(url)") + let webhookURL = URL(string: url.trimmingCharacters(in: .whitespacesAndNewlines))! // grab the url + var request = URLRequest(url: webhookURL) // create a urlrequest object with the grabbed url as the url + request.httpMethod = "POST" // make it a POST + request.addValue("application/json", forHTTPHeaderField: "content-type") // make sure its json + request.httpBody = jsonData // add the prepped json data as the body + + let task = URLSession.shared.dataTask(with: request) { data, response, error in + defer { sem.signal() } + guard let data = data, error == nil else { + returnErr = true + returnErrMsg = returnErrMsg + (error?.localizedDescription ?? "No data") + print(error?.localizedDescription ?? "No data") + return + } + let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) + if let responseJSON = responseJSON as? [String: Any] { + //print(responseJSON) + let jsonErrString = String(data: data, encoding: .utf8) + returnErr = true + returnErrMsg = returnErrMsg + (jsonErrString ?? "there was an error while getting the error text") + } + } + task.resume() + sem.wait() } } - - task.resume() - sem.wait() return (returnErr, returnErrMsg) }