-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathFeatureFlagRemote.swift
More file actions
58 lines (45 loc) · 2.39 KB
/
Copy pathFeatureFlagRemote.swift
File metadata and controls
58 lines (45 loc) · 2.39 KB
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
import Foundation
import WordPressKitObjC
open class FeatureFlagRemote: ServiceRemoteWordPressComREST {
public typealias FeatureFlagResponseCallback = (Result<FeatureFlagList, Error>) -> Void
public enum FeatureFlagRemoteError: Error {
case InvalidDataError
}
open func getRemoteFeatureFlags(forDeviceId deviceId: String, callback: @escaping FeatureFlagResponseCallback) {
let params = SessionDetails(deviceId: deviceId)
let endpoint = "mobile/feature-flags"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)
var dictionary: [String: AnyObject]?
do {
dictionary = try params.dictionaryRepresentation()
} catch let error {
callback(.failure(error))
return
}
wordPressComRESTAPI.get(path,
parameters: dictionary,
success: { response, _ in
if let featureFlagList = response as? NSDictionary {
let reconstitutedList = featureFlagList.compactMap { row -> FeatureFlag? in
guard
let title = row.key as? String,
let value = row.value as? Bool
else {
return nil
}
return FeatureFlag(title: title, value: value)
}.sorted()
callback(.success(reconstitutedList))
} else {
callback(.failure(FeatureFlagRemoteError.InvalidDataError))
}
}, failure: { error, response in
WPKitLogError("Error retrieving remote feature flags")
WPKitLogError("\(error)")
if let response {
WPKitLogDebug("Response Code: \(response.statusCode)")
}
callback(.failure(error))
})
}
}