Skip to content

Commit 75e2d4b

Browse files
committed
extension URL : ExpressibleByStringLiteral
1 parent c5bf7a6 commit 75e2d4b

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

MacCacheCleaner/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
//
88

99
import Cocoa
10-
let sourceJSONPath = "https://raw.githubusercontent.com/kaunteya/MacCacheCleaner/master/Source.json"
10+
let sourceJSONPath: URL = "https://raw.githubusercontent.com/kaunteya/MacCacheCleaner/master/Source.json"
1111

1212
@NSApplicationMain
1313
class AppDelegate: NSObject, NSApplicationDelegate {
1414

15-
let cacheListFetcher = CacheFetcher(urlString: sourceJSONPath)
15+
let cacheListFetcher = CacheFetcher.init(url: sourceJSONPath)
1616

1717
let cacheList = CacheList()
1818

MacCacheCleaner/Extensions/URLSession+Result.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ enum Result<Value> {
1414
}
1515

1616
extension URLSession {
17-
func dataTask<T: Decodable>(
18-
with request: URLRequest,
17+
18+
func jsonDecodableTask<T: Decodable>(
19+
with url: URL,
1920
completion: @escaping (Result<T>) -> Void
2021
) -> URLSessionDataTask {
2122

22-
return self.dataTask(with: request) { (data, response, error) in
23+
return self.dataTask(with: url) { (data, response, error) in
2324
guard error == nil else {
2425
completion(.failure(error!))
2526
return
@@ -29,8 +30,33 @@ extension URLSession {
2930
return
3031
}
3132

32-
let decoded = try! JSONDecoder().decode(T.self, from: data)
33-
completion(.success(decoded))
33+
if let decoded = try? JSONDecoder().decode(T.self, from: data) {
34+
completion(.success(decoded))
35+
} else {
36+
completion(.failure(nil))
37+
}
38+
}
39+
}
40+
41+
func jsonSerializedTask<T>(
42+
with request: URL,
43+
completion: @escaping (Result<T>) -> Void
44+
) -> URLSessionDataTask {
45+
46+
return self.dataTask(with: request) { (data, response, error) in
47+
guard error == nil else {
48+
completion(.failure(error!))
49+
return
50+
}
51+
guard let data = data, let _ = response else {
52+
completion(.failure(nil))
53+
return
54+
}
55+
if let json = try! JSONSerialization.jsonObject(with: data, options: []) as? T {
56+
completion(.success(json))
57+
} else {
58+
completion(.failure(nil))
59+
}
3460
}
3561
}
3662
}

MacCacheCleaner/Models/CacheFetcher.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ struct SourceJSON: Decodable {
1414
}
1515

1616
struct CacheFetcher {
17-
let urlString: String
17+
let url: URL
1818

1919
func fromNetwork(completion: @escaping([CacheItem]) -> Void,
2020
failure: ((Error?) -> Void)?) {
21-
let urlRequest = URLRequest(url: URL(string: urlString)!)
22-
URLSession.shared.dataTask(with: urlRequest) { (result:Result<SourceJSON>) in
21+
22+
URLSession.shared.jsonDecodableTask(with: url) { (result:Result<SourceJSON>) in
2323
switch result {
2424
case .success(let decoded):
2525
assert(decoded.version == 1)

MacCacheCleaner/Others/Other.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@
88

99
import AppKit
1010

11+
extension URL : ExpressibleByStringLiteral {
12+
public typealias StringLiteralType = String
13+
14+
public init(stringLiteral value: StringLiteralType) {
15+
self.init(string: value)!
16+
}
17+
}

0 commit comments

Comments
 (0)