mobile-ios/CovidSafe/API/PhoneValidationAPI.swift

66 lines
1.9 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,
completion: @escaping (String?, Swift.Error?) -> Void) {
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)
case let .failure(error):
completion(nil, error)
}
}
}
}
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
}