Skip to content

Commit

Permalink
Support sending to multiple webhooks by providing an @ delimited URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatStella7922 committed Feb 9, 2024
1 parent f682dc3 commit 08669c9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
Binary file not shown.
87 changes: 61 additions & 26 deletions dcbattwebhook-swift/SendBatteryInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 08669c9

Please sign in to comment.