2018-09-23 16:01:05 +00:00
|
|
|
//
|
|
|
|
// XCBSession.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/23/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class XCBSession {
|
2018-09-26 01:41:12 +00:00
|
|
|
static let encoder = JSONEncoder()
|
|
|
|
|
2018-09-24 01:35:33 +00:00
|
|
|
let type: XCBActionType
|
2018-09-26 01:41:12 +00:00
|
|
|
let request: XCBRequest
|
2018-09-23 16:01:05 +00:00
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
init(type: XCBActionType, request: XCBRequest) {
|
2018-09-23 16:01:05 +00:00
|
|
|
self.type = type
|
2018-09-26 01:41:12 +00:00
|
|
|
self.request = request
|
2018-09-23 16:01:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 22:43:33 +00:00
|
|
|
func complete(with result: XCBSessionResult, additionalData: [String: String?]? = nil) {
|
2018-09-26 01:41:12 +00:00
|
|
|
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))
|
2018-09-23 23:04:39 +00:00
|
|
|
}
|
2018-09-26 01:41:12 +00:00
|
|
|
url = components.url!
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
2018-09-23 16:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum XCBSessionResult {
|
|
|
|
case success, error, cancel
|
|
|
|
}
|