mirror of
https://github.com/AU-COVIDSafe/mobile-ios.git
synced 2025-04-19 21:15:21 +00:00
COVIDSafe code from version 1.1
This commit is contained in:
commit
3640e52eb2
330 changed files with 261540 additions and 0 deletions
45
CovidSafe/Feedback/Sources/JMCTargetJSONFromDisk.swift
Normal file
45
CovidSafe/Feedback/Sources/JMCTargetJSONFromDisk.swift
Normal file
|
@ -0,0 +1,45 @@
|
|||
// Copyright © 2020 Australian Government All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
extension JMCTarget {
|
||||
static let JSONFileName = "JMCTarget"
|
||||
|
||||
public static func createTargetFromJSONOnDisk() throws -> JMCTarget {
|
||||
guard let JMCTargetJSONPath = Foundation.Bundle.main.path(forResource: JSONFileName, ofType: "json") else {
|
||||
throw JMCTargetJSONOnDiskError.jsonFileMissingFromBundleError
|
||||
}
|
||||
return try JMCTarget(JSONFilePath: JMCTargetJSONPath)
|
||||
}
|
||||
|
||||
init(JSONFilePath: String) throws {
|
||||
guard let JMCTargetData = try? Data(contentsOf: URL(fileURLWithPath: JSONFilePath)) else {
|
||||
throw JMCTargetJSONOnDiskError.readJSONFileError
|
||||
}
|
||||
try self.init(JSONData: JMCTargetData)
|
||||
}
|
||||
|
||||
init(JSONData: Data) throws {
|
||||
let JSONObject = try JSONSerialization.jsonObject(with: JSONData, options: [])
|
||||
guard let JMCTargetDictionary = JSONObject as? [String : NSObject] else {
|
||||
throw JMCTargetJSONOnDiskError.jsonDictionaryToInstanceError
|
||||
}
|
||||
try self.init(JSONDictionary: JMCTargetDictionary )
|
||||
}
|
||||
|
||||
init(JSONDictionary: [String: NSObject]) throws {
|
||||
guard let host = JSONDictionary["host"] as? String,
|
||||
let apiKey = JSONDictionary["apiKey"] as? String,
|
||||
let projectKey = JSONDictionary["projectKey"] as? String else {
|
||||
throw JMCTargetJSONOnDiskError.jsonDictionaryToInstanceError
|
||||
}
|
||||
self.init(host: host, apiKey: apiKey, projectKey: projectKey)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum JMCTargetJSONOnDiskError: Error {
|
||||
case jsonFileMissingFromBundleError
|
||||
case readJSONFileError
|
||||
case jsonDictionaryToInstanceError
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue