Skip to content

Commit

Permalink
extension URL : ExpressibleByStringLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
kaunteya committed Jul 12, 2018
1 parent c5bf7a6 commit 75e2d4b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions MacCacheCleaner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
36 changes: 31 additions & 5 deletions MacCacheCleaner/Extensions/URLSession+Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ enum Result<Value> {
}

extension URLSession {
func dataTask<T: Decodable>(
with request: URLRequest,

func jsonDecodableTask<T: Decodable>(
with url: URL,
completion: @escaping (Result<T>) -> 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
Expand All @@ -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<T>(
with request: URL,
completion: @escaping (Result<T>) -> 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))
}
}
}
}
6 changes: 3 additions & 3 deletions MacCacheCleaner/Models/CacheFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceJSON>) in

URLSession.shared.jsonDecodableTask(with: url) { (result:Result<SourceJSON>) in
switch result {
case .success(let decoded):
assert(decoded.version == 1)
Expand Down
7 changes: 7 additions & 0 deletions MacCacheCleaner/Others/Other.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@

import AppKit

extension URL : ExpressibleByStringLiteral {
public typealias StringLiteralType = String

public init(stringLiteral value: StringLiteralType) {
self.init(string: value)!
}
}

0 comments on commit 75e2d4b

Please sign in to comment.