-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataLoader.swift
57 lines (42 loc) · 1.42 KB
/
DataLoader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// URLCreator.swift
// NYT-Reader
//
// Created by Jeremy Yarley on 6/16/20.
// Copyright © 2020 Jeremy Yarley. All rights reserved.
//
import Foundation
enum ArticleType: String {
case shared = "shared"
case viewed = "viewed"
case emailed = "emailed"
}
enum DaysViewed: String {
case one = "1"
case seven = "7"
case thirty = "30"
}
class DataLoader {
static func buildURLFromType(articleType: ArticleType, daysViewed: DaysViewed) -> String {
let urlRoot = "https://api.nytimes.com/svc/mostpopular/v2/"
let myKey = ".json?api-key=CjtyVGMUguR6nEVONdDSYVSGNk6rP9uU"
let builtURL = urlRoot + articleType.rawValue + "/" + daysViewed.rawValue + myKey
return builtURL
}
static func LoadURLData(articleType: ArticleType, daysViewed: DaysViewed, success: @escaping (_ data: Data?) -> Void) {
let urlString = buildURLFromType(articleType: articleType, daysViewed: daysViewed)
guard let url = URL(string: urlString) else {
print("URL is not valid")
success(nil)
return
}
DispatchQueue.global(qos: .background).async {
if let data = try? Data(contentsOf: url) {
success(data)
} else {
print("Unable to get data")
success(nil)
}
}
} //END OF FUNC
}