-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFirebase.swift
146 lines (121 loc) · 5.28 KB
/
Firebase.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import Foundation
/*
READ <- start the application
POST <- create, creating a new record, Firebase will return a new record identifier
PUT <- update a specific record
DELETE <- delete a specific record
*/
class Firebase<Item: Codable & FirebaseItem> {
static var baseURL: URL! { return URL(string: "https://put-and-post.firebaseio.com/") }
static func requestURL(_ method: String, for recordIdentifier: String = "unknownid") -> URL {
switch method {
case "POST":
// You post to the main DB. It will return a new record identifier
return baseURL.appendingPathExtension("json")
case "DELETE", "PUT", "GET":
// These all work on individual records, and you need to use the
// record identifier in your URL with one exception, which is when
// all the records at once, in which case, you do not need the record
// identifier.
return baseURL
.appendingPathComponent(recordIdentifier)
.appendingPathExtension("json")
default:
fatalError("Unknown request method: \(method)")
}
}
// Handle a single request: meant for DELETE, PUT, POST.
// If you were doing a GET, you'd want to pass back a record and not a success token
static func processRequest(
method: String,
for item: Item,
with completion: @escaping (_ success: Bool) -> Void = { _ in }
) {
// Fetch appropriate request URL customized to method
var request = URLRequest(url: requestURL(method, for: item.recordIdentifier))
request.httpMethod = method
// Encode this record
do {
request.httpBody = try JSONEncoder().encode(item)
} catch {
NSLog("Unable to encode \(item): \(error)")
completion(false)
return
}
// Create data task to perform request
let dataTask = URLSession.shared.dataTask(with: request) { data, _ , error in
// Fail on error
if let error = error {
NSLog("Server \(method) error: \(error)")
completion(false)
return
}
// Handle PUT, GET, DELETE and leave
guard method == "POST" else {
completion(true)
return
}
// Process POST requests
// Fetch identifier from POST
guard let data = data else {
NSLog("Invalid server response data")
completion(false)
return
}
do {
// POST request returns `["name": recordIdentifier]`. Store the
// record identifier
let nameDict = try JSONDecoder().decode([String: String].self, from: data)
guard let name = nameDict["name"] else {
completion(false)
return
}
// Update record and store that name. POST is now successful
// and includes the recordIdentifier as part of the item record.
item.recordIdentifier = name
processRequest(method: "PUT", for: item)
completion(true)
} catch {
NSLog("Error decoding JSON response: \(error)")
completion(false)
return
}
}
dataTask.resume()
}
static func delete(item: Item, completion: @escaping (_ success: Bool) -> Void = { _ in }) {
processRequest(method: "DELETE", for: item, with: completion)
}
static func save(item: Item, completion: @escaping (_ success: Bool) -> Void = { _ in }) {
switch item.recordIdentifier.isEmpty {
case true: // POST, new record
processRequest(method: "POST", for: item, with: completion)
case false: // PUT, existing record
processRequest(method: "PUT", for: item, with: completion)
}
}
// Fetch all records and pass them to sender via completion handler
static func fetchRecords(completion: @escaping ([Item]?) -> Void) {
let requestURL = baseURL.appendingPathExtension("json")
let dataTask = URLSession.shared.dataTask(with: requestURL) { data, _, error in
guard error == nil, let data = data else {
// Guaranteed to work
if let error = error {
NSLog("Error fetching entries: \(error)")
}
completion(nil)
return
}
do {
let recordDict = try JSONDecoder().decode([String: Item].self, from:data)
for (key, value) in recordDict { value.recordIdentifier = key } // pure paranoia
let records = recordDict.map({ $0.value }) // This converts the [[id: item]] array of dictionary entries into an array of items
completion(records)
} catch {
NSLog("Error decoding received data: \(error)")
completion([])
}
}
dataTask.resume()
}
}