Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved #9: Remove throws for services inits #10

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.1 (Feb 29, 2024)

- `WDOActivationService` and `WDOVerificationService` initializers no longer throw exceptions.

## 1.1.0 (Feb 9, 2024)

- Added support for the PowerAuth SDK 1.8.0+
Expand Down