2020-09-14 11:23:11 +10:00
|
|
|
//
|
|
|
|
// MessageAPI.swift
|
|
|
|
// CovidSafe
|
|
|
|
//
|
|
|
|
// Copyright © 2020 Australian Government. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Alamofire
|
|
|
|
|
2020-11-09 16:51:00 -08:00
|
|
|
class StatisticsAPI: CovidSafeAuthenticatedAPI {
|
2020-09-14 11:23:11 +10:00
|
|
|
|
|
|
|
static let keyCovidStatistics = "keyCovidStatistics"
|
|
|
|
|
2021-02-02 11:04:43 +11:00
|
|
|
static func getStatistics(forState: StateTerritory = StateTerritory.AU, completion: @escaping (StatisticsResponse?, CovidSafeAPIError?) -> Void) {
|
2020-09-14 11:23:11 +10:00
|
|
|
guard let apiHost = PlistHelper.getvalueFromInfoPlist(withKey: "API_Host", plistName: "CovidSafe-config") else {
|
|
|
|
completion(nil, .RequestError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:04:43 +11:00
|
|
|
let parameters = ["state" : "\(forState.rawValue)"]
|
|
|
|
|
2021-05-12 17:39:38 -07:00
|
|
|
guard let authHeaders = try? authenticatedHeaders() else {
|
|
|
|
completion(nil, .RequestError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:04:43 +11:00
|
|
|
CovidNetworking.shared.session.request("\(apiHost)/v2/statistics",
|
2020-09-14 11:23:11 +10:00
|
|
|
method: .get,
|
2021-02-02 11:04:43 +11:00
|
|
|
parameters: parameters,
|
2021-05-12 17:39:38 -07:00
|
|
|
headers: authHeaders,
|
2021-03-18 14:16:35 +11:00
|
|
|
interceptor: CovidRequestRetrier(retries: 3)
|
2020-09-14 11:23:11 +10:00
|
|
|
).validate().responseDecodable(of: StatisticsResponse.self) { (response) in
|
|
|
|
switch response.result {
|
|
|
|
case .success:
|
|
|
|
guard let statisticsResponse = response.value else { return }
|
|
|
|
let statsData = try? PropertyListEncoder().encode(statisticsResponse)
|
|
|
|
UserDefaults.standard.set(statsData, forKey: keyCovidStatistics)
|
|
|
|
|
|
|
|
completion(statisticsResponse, nil)
|
|
|
|
case .failure(_):
|
|
|
|
var lastStats: StatisticsResponse? = nil
|
|
|
|
if let savedStats = UserDefaults.standard.data(forKey: keyCovidStatistics) {
|
|
|
|
lastStats = try? PropertyListDecoder().decode(StatisticsResponse.self, from: savedStats)
|
|
|
|
}
|
|
|
|
guard let statusCode = response.response?.statusCode else {
|
|
|
|
completion(lastStats, .UnknownError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (statusCode == 200) {
|
|
|
|
completion(lastStats, .ResponseError)
|
2020-11-09 16:51:00 -08:00
|
|
|
return
|
2020-09-14 11:23:11 +10:00
|
|
|
}
|
2020-11-09 16:51:00 -08:00
|
|
|
|
|
|
|
if statusCode == 401, let respData = response.data {
|
|
|
|
completion(nil, processUnauthorizedError(respData))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:23:11 +10:00
|
|
|
if (statusCode >= 400 && statusCode < 500) {
|
|
|
|
completion(lastStats, .RequestError)
|
2020-11-09 16:51:00 -08:00
|
|
|
return
|
2020-09-14 11:23:11 +10:00
|
|
|
}
|
|
|
|
completion(lastStats, .ServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StatisticsResponse: Codable {
|
|
|
|
|
|
|
|
let updatedDate: String?
|
2021-02-02 11:04:43 +11:00
|
|
|
let responseVersion: String?
|
2020-09-14 11:23:11 +10:00
|
|
|
|
|
|
|
let national: StateTerritoryStatistics?
|
|
|
|
let act: StateTerritoryStatistics?
|
|
|
|
let nsw: StateTerritoryStatistics?
|
|
|
|
let nt: StateTerritoryStatistics?
|
|
|
|
let qld: StateTerritoryStatistics?
|
|
|
|
let sa: StateTerritoryStatistics?
|
|
|
|
let tas: StateTerritoryStatistics?
|
|
|
|
let vic: StateTerritoryStatistics?
|
|
|
|
let wa: StateTerritoryStatistics?
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
case updatedDate = "updated_date"
|
|
|
|
case national
|
|
|
|
case act
|
|
|
|
case nsw
|
|
|
|
case nt
|
|
|
|
case qld
|
|
|
|
case sa
|
|
|
|
case tas
|
|
|
|
case vic
|
|
|
|
case wa
|
2021-02-02 11:04:43 +11:00
|
|
|
case responseVersion = "version"
|
|
|
|
}
|
|
|
|
|
|
|
|
func version() -> Int {
|
|
|
|
guard let versionStr = responseVersion else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return Int(versionStr) ?? 0
|
2020-09-14 11:23:11 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StateTerritoryStatistics: Codable {
|
|
|
|
let totalCases: Int?
|
|
|
|
let activeCases: Int?
|
|
|
|
let newCases: Int?
|
|
|
|
let recoveredCases: Int?
|
|
|
|
let deaths: Int?
|
2021-02-02 11:04:43 +11:00
|
|
|
let newLocallyAcquired: Int?
|
|
|
|
let locallyAcquired: Int?
|
|
|
|
let newOverseasAcquired: Int?
|
2021-07-04 21:09:56 -07:00
|
|
|
let newUnderInvestigation: Int?
|
2021-02-02 11:04:43 +11:00
|
|
|
let overseasAcquired: Int?
|
|
|
|
let historicalCases: [HistoricalCase]?
|
2020-09-14 11:23:11 +10:00
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
case totalCases = "total_cases"
|
|
|
|
case activeCases = "active_cases"
|
|
|
|
case newCases = "new_cases"
|
|
|
|
case recoveredCases = "recovered_cases"
|
2021-02-02 11:04:43 +11:00
|
|
|
case newLocallyAcquired = "new_locally_acquired"
|
|
|
|
case locallyAcquired = "locally_acquired"
|
|
|
|
case newOverseasAcquired = "new_overseas_acquired"
|
|
|
|
case overseasAcquired = "overseas_acquired"
|
|
|
|
case historicalCases = "historical_cases"
|
2021-07-04 21:09:56 -07:00
|
|
|
case newUnderInvestigation = "new_under_investigation"
|
2020-09-14 11:23:11 +10:00
|
|
|
case deaths
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 11:04:43 +11:00
|
|
|
|
|
|
|
struct HistoricalCase: Codable {
|
|
|
|
let date: String
|
|
|
|
let newCases: Int
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
case date
|
|
|
|
case newCases = "new_cases"
|
|
|
|
}
|
|
|
|
}
|