Tusker/Tusker/XCallbackURL/XCBRequest.swift

45 lines
1.4 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
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
}
})
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
}
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) }
}
}