Tusker/Tusker/XCallbackURL/XCBRequest.swift

52 lines
1.6 KiB
Swift

//
// XCBRequest.swift
// Tusker
//
// Created by Shadowfacts on 9/25/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
struct XCBRequest {
let path: String
let arguments: [String: String]
let json: Bool
let silent: Bool
let source: String?
let success: URL?
let error: URL?
let cancel: URL?
init(spec: XCBRequestSpec, components: URLComponents) {
self.path = spec.path
if let queryItems = components.queryItems {
self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in
if let value = queryItems.first(where: { $0.name == el.key })?.value {
result[el.key] = value
}
})
source = queryItems.first(where: { $0.name == "x-source" }).flatMap { $0.value }
success = queryItems.first(where: { $0.name == "x-success" }).flatMap { $0.value }.flatMap { URL(string: $0) }
error = queryItems.first(where: { $0.name == "x-error" }).flatMap { $0.value }.flatMap { URL(string: $0) }
cancel = queryItems.first(where: { $0.name == "x-cancel" }).flatMap { $0.value }.flatMap { URL(string: $0) }
} else {
self.arguments = [:]
source = nil
success = nil
error = nil
cancel = nil
}
if let arg = arguments["json"] {
json = Bool(arg) ?? false
} else {
json = false
}
if spec.canRunSilently, let arg = arguments["silent"] {
silent = Bool(arg) ?? false
} else {
silent = false
}
}
}