-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathAPI+Command.swift
485 lines (437 loc) · 21.4 KB
/
API+Command.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//
// API+Command.swift
// ParseSwift
//
// Created by Florent Vilmart on 17-09-24.
// Copyright © 2020 Parse Community. All rights reserved.
//
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
internal extension API {
// MARK: API.Command
struct Command<T, U>: Encodable where T: ParseType {
typealias ReturnType = U // swiftlint:disable:this nesting
let method: API.Method
let path: API.Endpoint
let body: T?
let mapper: ((Data) throws -> U)
let params: [String: String?]?
let uploadData: Data?
let uploadFile: URL?
let parseURL: URL?
let otherURL: URL?
let stream: InputStream?
init(method: API.Method,
path: API.Endpoint,
params: [String: String]? = nil,
body: T? = nil,
uploadData: Data? = nil,
uploadFile: URL? = nil,
parseURL: URL? = nil,
otherURL: URL? = nil,
stream: InputStream? = nil,
mapper: @escaping ((Data) throws -> U)) {
self.method = method
self.path = path
self.body = body
self.uploadData = uploadData
self.uploadFile = uploadFile
self.parseURL = parseURL
self.otherURL = otherURL
self.stream = stream
self.mapper = mapper
self.params = params
}
// MARK: Synchronous Execution
func executeStream(options: API.Options,
callbackQueue: DispatchQueue,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil,
uploadProgress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil,
stream: InputStream) throws {
switch self.prepareURLRequest(options: options, childObjects: childObjects, childFiles: childFiles) {
case .success(let urlRequest):
if method == .POST || method == .PUT || method == .PATCH {
let task = URLSession.parse.uploadTask(withStreamedRequest: urlRequest)
ParseSwift.sessionDelegate.uploadDelegates[task] = uploadProgress
ParseSwift.sessionDelegate.streamDelegates[task] = stream
ParseSwift.sessionDelegate.taskCallbackQueues[task] = callbackQueue
task.resume()
return
}
case .failure(let error):
throw error
}
}
func execute(options: API.Options,
callbackQueue: DispatchQueue,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil,
uploadProgress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil,
downloadProgress: ((URLSessionDownloadTask, Int64, Int64, Int64) -> Void)? = nil) throws -> U {
var responseResult: Result<U, ParseError>?
let group = DispatchGroup()
group.enter()
self.executeAsync(options: options,
callbackQueue: callbackQueue,
childObjects: childObjects,
childFiles: childFiles,
uploadProgress: uploadProgress,
downloadProgress: downloadProgress) { result in
responseResult = result
group.leave()
}
group.wait()
guard let response = responseResult else {
throw ParseError(code: .unknownError,
message: "couldn't unrwrap server response")
}
return try response.get()
}
// MARK: Asynchronous Execution
// swiftlint:disable:next function_body_length cyclomatic_complexity
func executeAsync(options: API.Options,
callbackQueue: DispatchQueue,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil,
uploadProgress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil,
downloadProgress: ((URLSessionDownloadTask, Int64, Int64, Int64) -> Void)? = nil,
completion: @escaping(Result<U, ParseError>) -> Void) {
if !path.urlComponent.contains("/files/") {
//All ParseObjects use the shared URLSession
switch self.prepareURLRequest(options: options,
childObjects: childObjects,
childFiles: childFiles) {
case .success(let urlRequest):
URLSession.parse.dataTask(with: urlRequest, mapper: mapper) { result in
switch result {
case .success(let decoded):
completion(.success(decoded))
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
} else {
//ParseFiles are handled with a dedicated URLSession
if method == .POST || method == .PUT || method == .PATCH {
switch self.prepareURLRequest(options: options,
childObjects: childObjects,
childFiles: childFiles) {
case .success(let urlRequest):
URLSession
.parse
.uploadTask(callbackQueue: callbackQueue,
with: urlRequest,
from: uploadData,
from: uploadFile,
progress: uploadProgress,
mapper: mapper) { result in
switch result {
case .success(let decoded):
completion(.success(decoded))
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
} else if method == .DELETE {
switch self.prepareURLRequest(options: options,
childObjects: childObjects,
childFiles: childFiles) {
case .success(let urlRequest):
URLSession.parse.dataTask(with: urlRequest, mapper: mapper) { result in
switch result {
case .success(let decoded):
completion(.success(decoded))
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
} else {
if parseURL != nil {
switch self.prepareURLRequest(options: options,
childObjects: childObjects,
childFiles: childFiles) {
case .success(let urlRequest):
URLSession
.parse
.downloadTask(callbackQueue: callbackQueue,
with: urlRequest,
progress: downloadProgress,
mapper: mapper) { result in
switch result {
case .success(let decoded):
completion(.success(decoded))
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
} else if let otherURL = self.otherURL {
//Non-parse servers don't receive any parse dedicated request info
var request = URLRequest(url: otherURL)
request.cachePolicy = requestCachePolicy(options: options)
URLSession.parse.downloadTask(with: request, mapper: mapper) { result in
switch result {
case .success(let decoded):
completion(.success(decoded))
case .failure(let error):
completion(.failure(error))
}
}
} else {
completion(.failure(ParseError(code: .unknownError,
message: "Can't download the file without specifying the url")))
}
}
}
}
// MARK: URL Preperation
func prepareURLRequest(options: API.Options,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil) -> Result<URLRequest, ParseError> {
let params = self.params?.getQueryItems()
var headers = API.getHeaders(options: options)
if !(method == .POST) && !(method == .PUT) && !(method == .PATCH) {
headers.removeValue(forKey: "X-Parse-Request-Id")
}
let url = parseURL == nil ?
ParseSwift.configuration.serverURL.appendingPathComponent(path.urlComponent) : parseURL!
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return .failure(ParseError(code: .unknownError,
message: "couldn't unrwrap url components for \(url)"))
}
components.queryItems = params
guard let urlComponents = components.url else {
return .failure(ParseError(code: .unknownError,
message: "couldn't create url from components for \(components)"))
}
var urlRequest = URLRequest(url: urlComponents)
urlRequest.allHTTPHeaderFields = headers
if let urlBody = body {
if (urlBody as? CloudType) != nil {
guard let bodyData = try? ParseCoding.parseEncoder().encode(urlBody, skipKeys: .cloud) else {
return .failure(ParseError(code: .unknownError,
message: "couldn't encode body \(urlBody)"))
}
urlRequest.httpBody = bodyData
} else {
guard let bodyData = try? ParseCoding
.parseEncoder()
.encode(urlBody, collectChildren: false,
objectsSavedBeforeThisOne: childObjects,
filesSavedBeforeThisOne: childFiles) else {
return .failure(ParseError(code: .unknownError,
message: "couldn't encode body \(urlBody)"))
}
urlRequest.httpBody = bodyData.encoded
}
}
urlRequest.httpMethod = method.rawValue
urlRequest.cachePolicy = requestCachePolicy(options: options)
return .success(urlRequest)
}
enum CodingKeys: String, CodingKey { // swiftlint:disable:this nesting
case method, body, path
}
}
static func requestCachePolicy(options: API.Options) -> URLRequest.CachePolicy {
var policy: URLRequest.CachePolicy = ParseSwift.configuration.requestCachePolicy
options.forEach { option in
if case .cachePolicy(let updatedPolicy) = option {
policy = updatedPolicy
}
}
return policy
}
}
internal extension API.Command {
// MARK: Uploading File
static func uploadFile(_ object: ParseFile) throws -> API.Command<ParseFile, ParseFile> {
if !object.isSaved {
return createFile(object)
} else {
throw ParseError(code: .unknownError,
message: "File is already saved and cannot be updated.")
}
}
// MARK: Uploading File - private
private static func createFile(_ object: ParseFile) -> API.Command<ParseFile, ParseFile> {
API.Command(method: .POST,
path: .file(fileName: object.name),
uploadData: object.data,
uploadFile: object.localURL) { (data) -> ParseFile in
try ParseCoding.jsonDecoder().decode(FileUploadResponse.self, from: data).apply(to: object)
}
}
// MARK: Downloading File
static func downloadFile(_ object: ParseFile) -> API.Command<ParseFile, ParseFile> {
API.Command(method: .GET,
path: .file(fileName: object.name),
parseURL: object.url,
otherURL: object.cloudURL) { (data) -> ParseFile in
let tempFileLocation = try ParseCoding.jsonDecoder().decode(URL.self, from: data)
guard let fileManager = ParseFileManager(),
let defaultDirectoryPath = fileManager.defaultDataDirectoryPath else {
throw ParseError(code: .unknownError, message: "Can't create fileManager")
}
let downloadDirectoryPath = defaultDirectoryPath
.appendingPathComponent(ParseConstants.fileDownloadsDirectory, isDirectory: true)
try fileManager.createDirectoryIfNeeded(downloadDirectoryPath.relativePath)
let fileLocation = downloadDirectoryPath.appendingPathComponent(object.name)
if tempFileLocation != fileLocation {
try? FileManager.default.removeItem(at: fileLocation) //Remove file if it's already present
try FileManager.default.moveItem(at: tempFileLocation, to: fileLocation)
}
var object = object
object.localURL = fileLocation
return object
}
}
// MARK: Deleting File
static func deleteFile(_ object: ParseFile) -> API.Command<ParseFile, NoBody> {
API.Command(method: .DELETE,
path: .file(fileName: object.name),
parseURL: object.url) { (data) -> NoBody in
let parseError: ParseError!
do {
parseError = try ParseCoding.jsonDecoder().decode(ParseError.self, from: data)
} catch {
return try ParseCoding.jsonDecoder().decode(NoBody.self, from: data)
}
throw parseError
}
}
// MARK: Saving ParseObjects
static func save<T>(_ object: T,
isIgnoreCustomObjectIdConfig: Bool) throws -> API.Command<T, T> where T: ParseObject {
if ParseSwift.configuration.allowCustomObjectId && object.objectId == nil && !isIgnoreCustomObjectIdConfig {
throw ParseError(code: .missingObjectId, message: "objectId must not be nil")
}
if object.isSaved {
return update(object)
}
return create(object)
}
// MARK: Saving ParseObjects - private
private static func create<T>(_ object: T) -> API.Command<T, T> where T: ParseObject {
let mapper = { (data) -> T in
try ParseCoding.jsonDecoder().decode(SaveResponse.self, from: data).apply(to: object)
}
return API.Command<T, T>(method: .POST,
path: object.endpoint(.POST),
body: object,
mapper: mapper)
}
private static func update<T>(_ object: T) -> API.Command<T, T> where T: ParseObject {
let mapper = { (data) -> T in
try ParseCoding.jsonDecoder().decode(UpdateResponse.self, from: data).apply(to: object)
}
return API.Command<T, T>(method: .PUT,
path: object.endpoint,
body: object,
mapper: mapper)
}
// MARK: Fetching
static func fetch<T>(_ object: T, include: [String]?) throws -> API.Command<T, T> where T: ParseObject {
guard object.objectId != nil else {
throw ParseError(code: .unknownError, message: "Cannot Fetch an object without id")
}
var params: [String: String]?
if let includeParams = include {
params = ["include": "\(includeParams)"]
}
return API.Command<T, T>(
method: .GET,
path: object.endpoint,
params: params
) { (data) -> T in
try ParseCoding.jsonDecoder().decode(T.self, from: data)
}
}
}
internal extension API.Command where T: ParseObject {
// MARK: Batch - Saving, Fetching
static func batch(commands: [API.Command<T, T>],
transaction: Bool) -> RESTBatchCommandType<T> {
let batchCommands = commands.compactMap { (command) -> API.Command<T, T>? in
let path = ParseSwift.configuration.mountPath + command.path.urlComponent
guard let body = command.body else {
return nil
}
return API.Command<T, T>(method: command.method, path: .any(path),
body: body, mapper: command.mapper)
}
let mapper = { (data: Data) -> [Result<T, ParseError>] in
let decodingType = [BatchResponseItem<WriteResponse>].self
do {
let responses = try ParseCoding.jsonDecoder().decode(decodingType, from: data)
return commands.enumerated().map({ (object) -> (Result<T, ParseError>) in
let response = responses[object.offset]
if let success = response.success,
let body = object.element.body {
return .success(success.apply(to: body, method: object.element.method))
} else {
guard let parseError = response.error else {
return .failure(ParseError(code: .unknownError, message: "unknown error"))
}
return .failure(parseError)
}
})
} catch {
guard let parseError = error as? ParseError else {
return [(.failure(ParseError(code: .unknownError, message: "decoding error: \(error)")))]
}
return [(.failure(parseError))]
}
}
let batchCommand = BatchCommand(requests: batchCommands, transaction: transaction)
return RESTBatchCommandType<T>(method: .POST, path: .batch, body: batchCommand, mapper: mapper)
}
// MARK: Batch - Deleting
static func batch(commands: [API.NonParseBodyCommand<NoBody, NoBody>],
transaction: Bool) -> RESTBatchCommandNoBodyType<NoBody> {
let commands = commands.compactMap { (command) -> API.NonParseBodyCommand<NoBody, NoBody>? in
let path = ParseSwift.configuration.mountPath + command.path.urlComponent
return API.NonParseBodyCommand<NoBody, NoBody>(
method: command.method,
path: .any(path), mapper: command.mapper)
}
let mapper = { (data: Data) -> [(Result<Void, ParseError>)] in
let decodingType = [BatchResponseItem<NoBody>].self
do {
let responses = try ParseCoding.jsonDecoder().decode(decodingType, from: data)
return responses.enumerated().map({ (object) -> (Result<Void, ParseError>) in
let response = responses[object.offset]
if response.success != nil {
return .success(())
} else {
guard let parseError = response.error else {
return .failure(ParseError(code: .unknownError, message: "unknown error"))
}
return .failure(parseError)
}
})
} catch {
guard let parseError = error as? ParseError else {
return [(.failure(ParseError(code: .unknownError, message: "decoding error: \(error)")))]
}
return [(.failure(parseError))]
}
}
let batchCommand = BatchCommandNoBody(requests: commands, transaction: transaction)
return RESTBatchCommandNoBodyType<NoBody>(method: .POST, path: .batch, body: batchCommand, mapper: mapper)
}
}