Tusker/Tusker/XCallbackURL/XCBSession.swift

46 lines
1.4 KiB
Swift
Raw Normal View History

//
// XCBSession.swift
// Tusker
//
// Created by Shadowfacts on 9/23/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
class XCBSession {
static let encoder = JSONEncoder()
let type: XCBActionType
let request: XCBRequest
init(type: XCBActionType, request: XCBRequest) {
self.type = type
self.request = request
}
2018-09-23 22:43:33 +00:00
func complete(with result: XCBSessionResult, additionalData: [String: String?]? = nil) {
guard var url = result == .success ? request.success : result == .error ? request.error : request.cancel else { return }
XCBManager.currentSession = nil
if let additionalData = additionalData {
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
components.queryItems = components.queryItems ?? []
if request.json {
let data = try! XCBSession.encoder.encode(additionalData)
let response = String(data: data, encoding: .utf8)
components.queryItems!.append(URLQueryItem(name: "response", value: response))
} else {
2018-09-23 22:43:33 +00:00
components.queryItems!.append(contentsOf: additionalData.map(URLQueryItem.init))
}
url = components.url!
}
DispatchQueue.main.async {
UIApplication.shared.open(url, options: [:])
}
}
}
enum XCBSessionResult {
case success, error, cancel
}