forked from tvidenov/appstore-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiService.swift
72 lines (50 loc) · 2.06 KB
/
ApiService.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// ApiService.swift
// AppStore
//
// Created by Tihomir Videnov on 11/12/16.
// Copyright © 2016 Tihomir Videnov. All rights reserved.
//
import Foundation
class ApiService: NSObject {
static let sharedInstance = ApiService()
func fetchFeaturedApps(completionHandler: @escaping (FeaturedApps) -> ()) {
let urlString = "https://dl.dropboxusercontent.com/u/48453924/appStore%3Afeatured.json"
URLSession.shared.dataTask(with: URL(string: urlString)!) { (data, response, error) in
if error != nil {
print(error ?? "")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String:Any]
print(json)
let featuredApps = FeaturedApps()
featuredApps.setValuesForKeys(json)
DispatchQueue.main.async {
completionHandler(featuredApps)
}
} catch let err {
print(err)
}
}.resume()
}
func fetchAppDetails(id: Int, completionHandler: @escaping (App) -> ()) {
let urlString = "http://www.statsallday.com/appstore/appdetail?id=\(id)"
URLSession.shared.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, error) in
if error != nil {
print(error ?? "")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String:Any]
let appDetail = App()
appDetail.setValuesForKeys(json)
DispatchQueue.main.async {
completionHandler(appDetail)
}
} catch let err {
print(err)
}
}).resume()
}
}