2016-04-14 16:14:06 +00:00
|
|
|
//
|
|
|
|
// NSURLSession+Synchronous.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Michael Schneider on 4/14/16.
|
|
|
|
// Copyright © 2016 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-04-14 19:27:07 +00:00
|
|
|
// Synchronous NSURLSession code found at: http://ericasadun.com/2015/11/12/more-bad-things-synchronous-nsurlsessions/
|
|
|
|
|
2016-04-14 16:14:06 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
/// NSURLSession synchronous behavior
|
|
|
|
/// Particularly for playground sessions that need to run sequentially
|
2016-09-17 12:58:38 +00:00
|
|
|
public extension URLSession {
|
2016-04-14 16:14:06 +00:00
|
|
|
/// Return data from synchronous URL request
|
2018-11-14 04:47:06 +00:00
|
|
|
// TODO: Unused, remove
|
2016-09-17 12:58:38 +00:00
|
|
|
public static func requestSynchronousData(_ request: URLRequest) -> Data? {
|
|
|
|
var data: Data? = nil
|
2016-10-16 17:08:40 +00:00
|
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
|
|
let task = URLSession.shared.dataTask(with: request) {
|
2016-04-14 16:14:06 +00:00
|
|
|
taskData, _, error -> () in
|
|
|
|
data = taskData
|
|
|
|
if data == nil, let error = error {print(error)}
|
2016-10-16 17:08:40 +00:00
|
|
|
semaphore.signal()
|
|
|
|
}
|
2016-04-14 16:14:06 +00:00
|
|
|
task.resume()
|
2016-10-16 17:08:40 +00:00
|
|
|
let _ = semaphore.wait(timeout: .distantFuture)
|
2016-04-14 16:14:06 +00:00
|
|
|
return data
|
|
|
|
}
|
2018-11-14 04:41:11 +00:00
|
|
|
|
2016-04-14 16:14:06 +00:00
|
|
|
/// Return data synchronous from specified endpoint
|
2018-11-14 04:47:06 +00:00
|
|
|
// TODO: Unused, remove
|
2016-09-17 12:58:38 +00:00
|
|
|
public static func requestSynchronousDataWithURLString(_ requestString: String) -> Data? {
|
|
|
|
guard let url = URL(string:requestString) else {return nil}
|
|
|
|
let request = URLRequest(url: url)
|
|
|
|
return URLSession.requestSynchronousData(request)
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2018-11-14 04:41:11 +00:00
|
|
|
|
2016-04-14 16:14:06 +00:00
|
|
|
/// Return JSON synchronous from URL request
|
2018-11-14 04:47:06 +00:00
|
|
|
// TODO: Unused, remove
|
2016-10-16 17:08:40 +00:00
|
|
|
public static func requestSynchronousJSON(_ request: URLRequest) -> Any? {
|
2016-09-17 12:58:38 +00:00
|
|
|
guard let data = URLSession.requestSynchronousData(request) else {return nil}
|
2016-10-16 17:08:40 +00:00
|
|
|
return try! JSONSerialization.jsonObject(with: data, options: [])
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2018-11-14 04:41:11 +00:00
|
|
|
|
2016-04-14 16:14:06 +00:00
|
|
|
/// Return JSON synchronous from specified endpoint
|
2018-11-18 02:04:27 +00:00
|
|
|
@objc public func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? {
|
2016-09-17 12:58:38 +00:00
|
|
|
guard let url = URL(string: requestString) else {return nil}
|
2016-10-16 17:08:40 +00:00
|
|
|
var request = URLRequest(url:url)
|
2016-09-17 12:58:38 +00:00
|
|
|
request.httpMethod = "GET"
|
2016-04-14 16:14:06 +00:00
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
2016-10-16 17:08:40 +00:00
|
|
|
return URLSession.requestSynchronousJSON(request)
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-14 19:27:07 +00:00
|
|
|
|
|
|
|
public extension String {
|
|
|
|
/// Return an URL encoded string
|
2016-10-21 23:02:19 +00:00
|
|
|
var URLEncodedString: String? {
|
|
|
|
return addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
|
2016-04-14 19:27:07 +00:00
|
|
|
}
|
|
|
|
}
|