🔥 Old requestSynchronous* methods

This commit is contained in:
Ben Chatelain 2019-01-11 19:51:40 -07:00
parent 4546dfd18e
commit c5c6639c8b
2 changed files with 0 additions and 88 deletions

View file

@ -9,53 +9,3 @@
@objc public protocol NetworkSession {
@objc func loadData(from url: URL, completionHandler: @escaping (Data?, Error?) -> Void)
}
// MARK: - URLSession+Synchronous
extension NetworkSession {
/// Return data from synchronous URL request
public func requestSynchronousData(_ request: URLRequest) -> Data? {
var data: Data?
let semaphore = DispatchSemaphore(value: 0)
let task = URLSession.shared.dataTask(with: request) { (taskData, _, error) -> Void in
data = taskData
if data == nil, let error = error {
print(error)
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: .distantFuture)
return data
}
/// Return data synchronous from specified endpoint
public func requestSynchronousDataWithURLString(_ requestString: String) -> Data? {
guard let url = URL(string: requestString) else { return nil }
let request = URLRequest(url: url)
return requestSynchronousData(request)
}
/// Return JSON synchronous from URL request
public func requestSynchronousJSON(_ request: URLRequest) -> Any? {
guard let data = requestSynchronousData(request) else { return nil }
do {
return try JSONSerialization.jsonObject(with: data, options: [])
} catch {
printError("\(error.localizedDescription)")
return nil
}
}
/// Return JSON synchronous from specified endpoint
public func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? {
guard let url = URL(string: requestString) else { return nil }
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
return requestSynchronousJSON(request)
}
}

View file

@ -37,42 +37,4 @@ class NetworkSessionMockFromFile: NetworkSessionMock {
completionHandler(nil, error)
}
}
/// Override which returns Data from a file.
///
/// - Parameter requestString: Ignored URL string
/// - Returns: Contents of responseFile
@objc func requestSynchronousDataWithURLString(_ requestString: String) -> Data? {
guard let fileURL = Bundle.jsonResponse(fileName: responseFile)
else { fatalError("Unable to load file \(responseFile)") }
do {
let data = try Data(contentsOf: fileURL, options: .mappedIfSafe)
return data
} catch {
print("Error opening file: \(error)")
}
return nil
}
/// Override which returns JSON contents from a file.
///
/// - Parameter requestString: Ignored URL string
/// - Returns: Parsed contents of responseFile
@objc func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? {
guard let data = requestSynchronousDataWithURLString(requestString)
else { return nil }
do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
if let jsonResult = jsonResult as? [String: AnyObject] {
return jsonResult
}
} catch {
print("Error parsing JSON: \(error)")
}
return nil
}
}