-
-
Save takiguri/f2f62a568eccf8bb7fc0323ddc8decef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
typealias JSON = [String : Any] | |
fileprivate let imageCache = NSCache<NSString, UIImage>() | |
extension NSError { | |
static func generalParsingError(domain: String) -> Error { | |
return NSError(domain: domain, code: 400, userInfo: [NSLocalizedDescriptionKey : NSLocalizedString("Error retrieving data", comment: "General Parsing Error Description")]) | |
} | |
} | |
class MTAPIClient { | |
//MARK: - Public | |
static func downloadImage(url: URL, completion: @escaping (_ image: UIImage?, _ error: Error? ) -> Void) { | |
if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) { | |
completion(cachedImage, nil) | |
} else { | |
MTAPIClient.downloadData(url: url) { data, response, error in | |
if let error = error { | |
completion(nil, error) | |
} else if let data = data, let image = UIImage(data: data) { | |
imageCache.setObject(image, forKey: url.absoluteString as NSString) | |
completion(image, nil) | |
} else { | |
completion(nil, NSError.generalParsingError(domain: url.absoluteString)) | |
} | |
} | |
} | |
} | |
static func search(for query: String, page: Int, completion: @escaping (_ responseObject: [String : Any]?, _ error: Error?) -> Void) { | |
if let encodedQuery = query.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), | |
let url = URL(string:"http://www.omdbapi.com/?s=\(encodedQuery)&page=\(page)") { | |
MTAPIClient.downloadData(url: url) { data, response, error in | |
if let error = error { | |
completion(nil, error) | |
} else { | |
if let data = data, let responseObject = self.convertToJSON(with: data) { | |
completion(responseObject, nil) | |
} else { | |
completion(nil, NSError.generalParsingError(domain: url.absoluteString)) | |
} | |
} | |
} | |
} | |
} | |
//MARK: - Private | |
fileprivate static func downloadData(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) { | |
URLSession(configuration: .ephemeral).dataTask(with: URLRequest(url: url)) { data, response, error in | |
completion(data, response, error) | |
}.resume() | |
} | |
fileprivate static func convertToJSON(with data: Data) -> JSON? { | |
do { | |
return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? JSON | |
} catch { | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment