diff --git a/MacCacheCleaner/AppDelegate.swift b/MacCacheCleaner/AppDelegate.swift index 3a29852..a73bf64 100644 --- a/MacCacheCleaner/AppDelegate.swift +++ b/MacCacheCleaner/AppDelegate.swift @@ -7,12 +7,12 @@ // import Cocoa -let sourceJSONPath = "https://raw.githubusercontent.com/kaunteya/MacCacheCleaner/master/Source.json" +let sourceJSONPath: URL = "https://raw.githubusercontent.com/kaunteya/MacCacheCleaner/master/Source.json" @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { - let cacheListFetcher = CacheFetcher(urlString: sourceJSONPath) + let cacheListFetcher = CacheFetcher.init(url: sourceJSONPath) let cacheList = CacheList() diff --git a/MacCacheCleaner/Extensions/URLSession+Result.swift b/MacCacheCleaner/Extensions/URLSession+Result.swift index 12ae12d..edf56a2 100644 --- a/MacCacheCleaner/Extensions/URLSession+Result.swift +++ b/MacCacheCleaner/Extensions/URLSession+Result.swift @@ -14,12 +14,13 @@ enum Result { } extension URLSession { - func dataTask( - with request: URLRequest, + + func jsonDecodableTask( + with url: URL, completion: @escaping (Result) -> Void ) -> URLSessionDataTask { - return self.dataTask(with: request) { (data, response, error) in + return self.dataTask(with: url) { (data, response, error) in guard error == nil else { completion(.failure(error!)) return @@ -29,8 +30,33 @@ extension URLSession { return } - let decoded = try! JSONDecoder().decode(T.self, from: data) - completion(.success(decoded)) + if let decoded = try? JSONDecoder().decode(T.self, from: data) { + completion(.success(decoded)) + } else { + completion(.failure(nil)) + } + } + } + + func jsonSerializedTask( + with request: URL, + completion: @escaping (Result) -> Void + ) -> URLSessionDataTask { + + return self.dataTask(with: request) { (data, response, error) in + guard error == nil else { + completion(.failure(error!)) + return + } + guard let data = data, let _ = response else { + completion(.failure(nil)) + return + } + if let json = try! JSONSerialization.jsonObject(with: data, options: []) as? T { + completion(.success(json)) + } else { + completion(.failure(nil)) + } } } } diff --git a/MacCacheCleaner/Models/CacheFetcher.swift b/MacCacheCleaner/Models/CacheFetcher.swift index e926db7..f0a4c8a 100644 --- a/MacCacheCleaner/Models/CacheFetcher.swift +++ b/MacCacheCleaner/Models/CacheFetcher.swift @@ -14,12 +14,12 @@ struct SourceJSON: Decodable { } struct CacheFetcher { - let urlString: String + let url: URL func fromNetwork(completion: @escaping([CacheItem]) -> Void, failure: ((Error?) -> Void)?) { - let urlRequest = URLRequest(url: URL(string: urlString)!) - URLSession.shared.dataTask(with: urlRequest) { (result:Result) in + + URLSession.shared.jsonDecodableTask(with: url) { (result:Result) in switch result { case .success(let decoded): assert(decoded.version == 1) diff --git a/MacCacheCleaner/Others/Other.swift b/MacCacheCleaner/Others/Other.swift index bc0d5fb..b69e7c6 100644 --- a/MacCacheCleaner/Others/Other.swift +++ b/MacCacheCleaner/Others/Other.swift @@ -8,3 +8,10 @@ import AppKit +extension URL : ExpressibleByStringLiteral { + public typealias StringLiteralType = String + + public init(stringLiteral value: StringLiteralType) { + self.init(string: value)! + } +}