mobile-ios/CovidSafe/API/PhoneValidationAPI.swift

80 lines
2.5 KiB
Swift
Raw Normal View History

2020-05-08 07:49:14 +00:00
//
// PhoneValidationAPI.swift
// CovidSafe
//
// Copyright © 2020 Australian Government. All rights reserved.
//
import Foundation
import Alamofire
class PhoneValidationAPI {
static func verifyPhoneNumber(regInfo: RegistrationRequest,
2021-05-13 00:39:38 +00:00
completion: @escaping (String?, CovidSafeAPIError?) -> Void) {
2020-05-08 07:49:14 +00:00
guard let apiHost = PlistHelper.getvalueFromInfoPlist(withKey: "API_Host", plistName: "CovidSafe-config") else {
return
}
2020-06-19 07:43:33 +00:00
2020-05-08 07:49:14 +00:00
let params = [
2020-06-19 07:43:33 +00:00
"country_code": "+\(regInfo.countryPhoneCode ?? "61")",
2020-05-08 07:49:14 +00:00
"phone_number": regInfo.phoneNumber,
"age": String(regInfo.age),
"postcode": regInfo.postcode,
"name": regInfo.fullName,
"device_id": UIDevice.current.identifierForVendor!.uuidString
]
CovidNetworking.shared.session.request("\(apiHost)/initiateAuth",
method: .post,
parameters: params,
2021-03-18 03:16:35 +00:00
encoding: JSONEncoding.default).validate().responseDecodable(of: AuthResponse.self) { (response) in
2020-05-08 07:49:14 +00:00
switch response.result {
case .success:
guard let authResponse = response.value else { return }
completion(authResponse.session, nil)
2021-05-13 00:39:38 +00:00
case .failure(_):
var apiError = CovidSafeAPIError.RequestError
if let respData = response.data {
do {
let errorResponse = try JSONDecoder().decode(CovidSafeErrorResponse.self, from: respData)
if errorResponse.message == "MaxRegistrationsReached" {
apiError = .MaxRegistrationError
}
} catch {
// unable to parse response
apiError = .ResponseError
}
}
completion(nil, apiError)
2020-05-08 07:49:14 +00:00
}
}
}
}
struct RegistrationRequest {
var fullName: String
var postcode: String
var age: Int
var isMinor: Bool
var phoneNumber: String
2020-06-19 07:43:33 +00:00
var countryPhoneCode: String?
2020-05-08 07:49:14 +00:00
}
struct AuthResponse: Decodable {
let session: String
let challengeName: String
enum CodingKeys: String, CodingKey {
case session
case challengeName
}
}
protocol RegistrationHandler {
var registrationInfo: RegistrationRequest? { get set }
2020-11-10 00:51:00 +00:00
var reauthenticating: Bool { get set }
2020-05-08 07:49:14 +00:00
}