Skip to content

Commit

Permalink
Resolved #9: Remove throws for services inits
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed Feb 28, 2024
1 parent e1fdfdf commit f50ab5e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 64 deletions.
57 changes: 5 additions & 52 deletions Sources/Onboarding/WDOActivationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ public class WDOActivationService {
/// - powerAuth: Configured PowerAuthSDK instance. This instance needs to be without valid activation.
/// - config: Configuration for the networking.
/// - canRestoreSession: If the activation session can be restored (when app restarts). `true` by default.
/// - Throws: An error of type `WPNError` with additional information. Thrown when provided PowerAuthSDK instance is in the wrong state.
public convenience init(powerAuth: PowerAuthSDK, config: WPNConfig, canRestoreSession: Bool = true) throws {
try self.init(
public convenience init(powerAuth: PowerAuthSDK, config: WPNConfig, canRestoreSession: Bool = true) {
self.init(
networking: WPNNetworkingService(powerAuth: powerAuth, config: config, serviceName: "WDOActivationNetworking"),
canRestoreSession: canRestoreSession
)
Expand All @@ -95,19 +94,13 @@ public class WDOActivationService {
/// - Parameters:
/// - networking: Networking service for the onboarding server with configured PowerAuthSDK instance that needs to be without valid activation.
/// - canRestoreSession: If the activation session can be restored (when app restarts). `true` by default.
/// - Throws: An error of type `WPNError` with additional information. Thrown when provided PowerAuthSDK instance is in the wrong state.
public convenience init(networking: WPNNetworkingService, canRestoreSession: Bool = true) throws {
try self.init(api: .init(networking: networking), canRestoreSession: canRestoreSession)
public convenience init(networking: WPNNetworkingService, canRestoreSession: Bool = true) {
self.init(api: .init(networking: networking), canRestoreSession: canRestoreSession)
}

// MARK: - Internal initializers

init(api: Networking, canRestoreSession: Bool) throws {

guard api.networking.powerAuth.canStartActivation() else {
throw WPNError(reason: .wdo_activation_cannotActivate)
}

init(api: Networking, canRestoreSession: Bool) {
self.api = api
self.keychainKey = "wdopid_\(api.networking.powerAuth.configuration.instanceId)"
if canRestoreSession == false {
Expand Down Expand Up @@ -426,43 +419,3 @@ struct UserData: Codable {
let userID: String
let birthDate: String
}

class MyUserService {
// prepared service
private var activationService: WDOActivationService!

func activate(smsOTP: String) {
activationService.activate(otp: smsOTP) { result in
switch result {
case .success(let resultData):
// PowerAuthSDK instance was activated.
// At this moment, navigate the user to
// the PIN keyboard to finish the PowerAuthSDK initialization.
// Fore more information, follow the PowerAuthSDK documentation.
break
case .failure(let failure):
if failure.allowOnboardingOtpRetry {
// User entered a wrong OTP, prompt for a new one.
// Remaining OTP attempts cound: failure.onboardingOtpRemainingAttempts
} else {
// show error UI
}
}
}
}

func startActivation(id: String, bday: String) {
let data = UserData(userID: id, birthDate: bday)
activationService.start(credentials: data) { result in
switch result {
case .success:
// success, continue with `activate()`
// at this moment, the `hasActiveProcess` starts return true
break
case .failure(let error):
// show error to the user
break
}
}
}
}
17 changes: 5 additions & 12 deletions Sources/Verification/WDOVerificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,23 @@ public class WDOVerificationService {
/// - Parameters:
/// - powerAuth: Configured PowerAuthSDK instance. This instance needs to have a valid activation.
/// - config: Configuration of the networking service.
/// - Throws: An error of type `WPNError` with additional information. Thrown when provided PowerAuthSDK instance is in the wrong state.
public convenience init(powerAuth: PowerAuthSDK, wpnConfig: WPNConfig) throws {
try self.init(networking: .init(powerAuth: powerAuth, config: wpnConfig, serviceName: "WDOVerificationNetworking"))
public convenience init(powerAuth: PowerAuthSDK, wpnConfig: WPNConfig) {
self.init(networking: .init(powerAuth: powerAuth, config: wpnConfig, serviceName: "WDOVerificationNetworking"))
}

/// Creates service instance
/// - Parameters:
/// - networking: Networking service for the onboarding server with configured PowerAuthSDK instance that needs to have a valid activation.
/// - Throws: An error of type `WPNError` with additional information. Thrown when provided PowerAuthSDK instance is in the wrong state.
public convenience init(networking: WPNNetworkingService) throws {
try self.init(api: .init(networking: networking))
public convenience init(networking: WPNNetworkingService) {
self.init(api: .init(networking: networking))
if networking.powerAuth.hasValidActivation() == false {
cachedProcess = nil
}
}

// MARK: - Private initializers

init(api: Networking) throws {

guard api.networking.powerAuth.hasValidActivation() else {
throw WPNError(reason: .wdo_verification_activationNotActive)
}

init(api: Networking) {
self.api = api
self.keychainKey = "wdocp_\(api.networking.powerAuth.configuration.instanceId)"
}
Expand Down

0 comments on commit f50ab5e

Please sign in to comment.