-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataService.swift
54 lines (43 loc) · 1.71 KB
/
DataService.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
//
// DataService.swift
// RESThub
//
// Created by Harrison on 7/28/19.
// Copyright © 2019 Harrison. All rights reserved.
//
import Foundation
class DataService {
static let shared = DataService()
fileprivate let baseURLString = "https://api.github.com"
func fetchGists(completion: @escaping (Result<[Gist], Error>) -> Void) {
/*var baseURL = URL(string: baseURLString)
baseURL?.appendPathComponent("/somePath")
let compusedURL = URL(string: "/somePath", relativeTo: baseURL)
print(baseURL!)
print(compusedURL?.absoluteString ?? "Relative URL failed...")*/
var componentURL = URLComponents()
componentURL.scheme = "https"
componentURL.host = "api.github.com"
componentURL.path = "/gists/public"
guard let validURL = componentURL.url else {
print("URL creation failed...")
return
}
URLSession.shared.dataTask(with: validURL) { (data, response, error) in
if let httpResponse = response as? HTTPURLResponse {
print("API status: \(httpResponse.statusCode)")
}
guard let validData = data, error == nil else {
completion(.failure(error!))
return
}
do {
//let json = try JSONSerialization.jsonObject(with: validData, options: [])
let gists = try JSONDecoder().decode([Gist].self, from: validData)
completion(.success(gists))
} catch let serializationError {
completion(.failure(serializationError))
}
}.resume()
}
}