oncall-mobile-ios/ntfy/Utils/ActionExecutor.swift
2022-06-05 19:14:05 -04:00

54 lines
1.8 KiB
Swift

import Foundation
import UIKit
struct ActionExecutor {
private static let tag = "ActionExecutor"
static func execute(_ action: Action) {
Log.d(tag, "Executing user action", action)
switch action.action {
case "view":
if let url = URL(string: action.url ?? "") {
open(url: url)
} else {
Log.w(tag, "Unable to parse action URL", action)
}
case "http":
http(action)
default:
Log.w(tag, "Action \(action.action) not supported", action)
}
}
private static func http(_ action: Action) {
guard let actionUrl = action.url, let url = URL(string: actionUrl) else {
Log.w(tag, "Unable to execute HTTP action, no or invalid URL", action)
return
}
let method = action.method ?? "POST" // POST is the default!!
let body = action.body ?? ""
Log.d(tag, "Performing HTTP \(method) \(url)")
var request = URLRequest(url: url)
request.httpMethod = method
action.headers?.forEach { key, value in
request.setValue(value, forHTTPHeaderField: key)
}
if !["GET", "HEAD"].contains(method) {
request.httpBody = body.data(using: .utf8)
}
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard error == nil else {
Log.e(self.tag, "Error performing HTTP \(method)", error!)
return
}
Log.d(self.tag, "HTTP \(method) succeeded", response)
}.resume()
}
private static func open(url: URL) {
Log.d(tag, "Opening URL \(url)")
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}