From d1e60b89bf2c01c81b7d8968af7c8e111f793fc7 Mon Sep 17 00:00:00 2001 From: Jon Petersson Date: Mon, 20 Nov 2023 11:48:06 +0100 Subject: [PATCH] Stop using old completion based MullvadREST interface --- ios/MullvadREST/RESTAPIProxy.swift | 52 ++++++------------- ios/MullvadREST/RESTAccountsProxy.swift | 34 +++--------- ios/MullvadREST/RESTAuthenticationProxy.swift | 8 +-- ios/MullvadREST/RESTDevicesProxy.swift | 40 +++++++------- ios/MullvadREST/RESTProxy.swift | 17 ------ .../SendStoreReceiptOperation.swift | 5 +- .../StorePaymentManager.swift | 5 +- .../TunnelManager/SetAccountOperation.swift | 35 +++++++------ .../UpdateAccountDataOperation.swift | 3 +- ios/MullvadVPNTests/APIProxy+Stubs.swift | 9 ---- ios/MullvadVPNTests/AccountsProxy+Stubs.swift | 8 --- 11 files changed, 71 insertions(+), 145 deletions(-) diff --git a/ios/MullvadREST/RESTAPIProxy.swift b/ios/MullvadREST/RESTAPIProxy.swift index 8594297410a7..24d02d36e787 100644 --- a/ios/MullvadREST/RESTAPIProxy.swift +++ b/ios/MullvadREST/RESTAPIProxy.swift @@ -27,13 +27,6 @@ public protocol APIQuerying { receiptString: Data ) -> any RESTRequestExecutor - func createApplePayment( - accountNumber: String, - receiptString: Data, - retryStrategy: REST.RetryStrategy, - completionHandler: @escaping ProxyCompletionHandler - ) -> Cancellable - func sendProblemReport( _ body: REST.ProblemReportRequest, retryStrategy: REST.RetryStrategy, @@ -79,13 +72,13 @@ extension REST { with: responseDecoder ) - return addOperation( + let executor = makeRequestExecutor( name: "get-api-addrs", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completionHandler + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completionHandler) } public func getRelays( @@ -136,13 +129,13 @@ extension REST { } } - return addOperation( + let executor = makeRequestExecutor( name: "get-relays", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completionHandler + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completionHandler) } public func createApplePayment( @@ -202,19 +195,6 @@ extension REST { ) } - @available(*, deprecated, message: "Use createApplePayment(accountNumber:, receiptString:) instead") - public func createApplePayment( - accountNumber: String, - receiptString: Data, - retryStrategy: REST.RetryStrategy, - completionHandler: @escaping ProxyCompletionHandler - ) -> Cancellable { - return createApplePayment(accountNumber: accountNumber, receiptString: receiptString).execute( - retryStrategy: retryStrategy, - completionHandler: completionHandler - ) - } - public func sendProblemReport( _ body: ProblemReportRequest, retryStrategy: REST.RetryStrategy, @@ -246,13 +226,13 @@ extension REST { } } - return addOperation( + let executor = makeRequestExecutor( name: "send-problem-report", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completionHandler + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completionHandler) } public func submitVoucher( @@ -290,13 +270,13 @@ extension REST { } } - return addOperation( + let executor = makeRequestExecutor( name: "submit-voucher", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completionHandler + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completionHandler) } } diff --git a/ios/MullvadREST/RESTAccountsProxy.swift b/ios/MullvadREST/RESTAccountsProxy.swift index fad967aac703..4088a2abb40a 100644 --- a/ios/MullvadREST/RESTAccountsProxy.swift +++ b/ios/MullvadREST/RESTAccountsProxy.swift @@ -17,12 +17,6 @@ public protocol RESTAccountHandling { func getAccountData(accountNumber: String) -> any RESTRequestExecutor - func getAccountData( - accountNumber: String, - retryStrategy: REST.RetryStrategy, - completion: @escaping ProxyCompletionHandler - ) -> Cancellable - func deleteAccount( accountNumber: String, retryStrategy: REST.RetryStrategy, @@ -61,13 +55,13 @@ extension REST { with: responseDecoder ) - return addOperation( + let executor = makeRequestExecutor( name: "create-account", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } public func getAccountData(accountNumber: String) -> any RESTRequestExecutor { @@ -98,18 +92,6 @@ extension REST { ) } - @available(*, deprecated, message: "Use getAccountData(accountNumber:) instead") - public func getAccountData( - accountNumber: String, - retryStrategy: REST.RetryStrategy, - completion: @escaping ProxyCompletionHandler - ) -> Cancellable { - return getAccountData(accountNumber: accountNumber).execute( - retryStrategy: retryStrategy, - completionHandler: completion - ) - } - public func deleteAccount( accountNumber: String, retryStrategy: RetryStrategy, @@ -143,13 +125,13 @@ extension REST { } } - return addOperation( + let executor = makeRequestExecutor( name: "delete-my-account", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } } diff --git a/ios/MullvadREST/RESTAuthenticationProxy.swift b/ios/MullvadREST/RESTAuthenticationProxy.swift index 0b5cabe8cb18..cbcd6fdc0702 100644 --- a/ios/MullvadREST/RESTAuthenticationProxy.swift +++ b/ios/MullvadREST/RESTAuthenticationProxy.swift @@ -47,13 +47,13 @@ extension REST { with: responseDecoder ) - return addOperation( + let executor = makeRequestExecutor( name: "get-access-token", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } } diff --git a/ios/MullvadREST/RESTDevicesProxy.swift b/ios/MullvadREST/RESTDevicesProxy.swift index 7047f3acd404..dd49179c3037 100644 --- a/ios/MullvadREST/RESTDevicesProxy.swift +++ b/ios/MullvadREST/RESTDevicesProxy.swift @@ -110,13 +110,13 @@ extension REST { } } - return addOperation( + let executor = makeRequestExecutor( name: "get-device", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } /// Fetch a list of created devices. @@ -145,13 +145,13 @@ extension REST { with: responseDecoder ) - return addOperation( + let executor = makeRequestExecutor( name: "get-devices", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } /// Create new device. @@ -184,13 +184,13 @@ extension REST { with: responseDecoder ) - return addOperation( + let executor = makeRequestExecutor( name: "create-device", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } /// Delete device by identifier. @@ -247,13 +247,13 @@ extension REST { } } - return addOperation( + let executor = makeRequestExecutor( name: "delete-device", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } /// Rotate device key @@ -300,13 +300,13 @@ extension REST { with: responseDecoder ) - return addOperation( + let executor = makeRequestExecutor( name: "rotate-device-key", - retryStrategy: retryStrategy, requestHandler: requestHandler, - responseHandler: responseHandler, - completionHandler: completion + responseHandler: responseHandler ) + + return executor.execute(retryStrategy: retryStrategy, completionHandler: completion) } } diff --git a/ios/MullvadREST/RESTProxy.swift b/ios/MullvadREST/RESTProxy.swift index 3ca5c9630eef..49d4fc8968b2 100644 --- a/ios/MullvadREST/RESTProxy.swift +++ b/ios/MullvadREST/RESTProxy.swift @@ -43,23 +43,6 @@ extension REST { self.responseDecoder = responseDecoder } - @available(*, deprecated, message: "Use makeRequestExecutor() instead") - func addOperation( - name: String, - retryStrategy: REST.RetryStrategy, - requestHandler: RESTRequestHandler, - responseHandler: some RESTResponseHandler, - completionHandler: @escaping ProxyCompletionHandler - ) -> Cancellable { - let executor = makeRequestExecutor( - name: name, - requestHandler: requestHandler, - responseHandler: responseHandler - ) - - return executor.execute(retryStrategy: retryStrategy, completionHandler: completionHandler) - } - func makeRequestExecutor( name: String, requestHandler: RESTRequestHandler, diff --git a/ios/MullvadVPN/StorePaymentManager/SendStoreReceiptOperation.swift b/ios/MullvadVPN/StorePaymentManager/SendStoreReceiptOperation.swift index d29bde3d9db4..8efbc960958e 100644 --- a/ios/MullvadVPN/StorePaymentManager/SendStoreReceiptOperation.swift +++ b/ios/MullvadVPN/StorePaymentManager/SendStoreReceiptOperation.swift @@ -132,9 +132,8 @@ class SendStoreReceiptOperation: ResultOperation Void ) { let accountOperation = ResultBlockOperation(dispatchQueue: .main) { finish in - self.accountsProxy.getAccountData( - accountNumber: accountNumber, + self.accountsProxy.getAccountData(accountNumber: accountNumber).execute( retryStrategy: .default, - completion: finish + completionHandler: finish ) } diff --git a/ios/MullvadVPN/TunnelManager/SetAccountOperation.swift b/ios/MullvadVPN/TunnelManager/SetAccountOperation.swift index d59beef36886..1f8307d2b7e3 100644 --- a/ios/MullvadVPN/TunnelManager/SetAccountOperation.swift +++ b/ios/MullvadVPN/TunnelManager/SetAccountOperation.swift @@ -239,26 +239,27 @@ class SetAccountOperation: ResultOperation { private func getAccount(accountNumber: String, completion: @escaping (Result) -> Void) { logger.debug("Request account data...") - let task = accountsProxy - .getAccountData(accountNumber: accountNumber, retryStrategy: .default) { [self] result in - dispatchQueue.async { [self] in - let result = result.inspectError { error in - guard !error.isOperationCancellationError else { return } - - logger.error(error: error, message: "Failed to receive account data.") - }.map { accountData -> StoredAccountData in - logger.debug("Received account data.") - - return StoredAccountData( - identifier: accountData.id, - number: accountNumber, - expiry: accountData.expiry - ) - } + let task = accountsProxy.getAccountData(accountNumber: accountNumber).execute( + retryStrategy: .default + ) { [self] result in + dispatchQueue.async { [self] in + let result = result.inspectError { error in + guard !error.isOperationCancellationError else { return } - completion(result) + logger.error(error: error, message: "Failed to receive account data.") + }.map { accountData -> StoredAccountData in + logger.debug("Received account data.") + + return StoredAccountData( + identifier: accountData.id, + number: accountNumber, + expiry: accountData.expiry + ) } + + completion(result) } + } tasks.append(task) } diff --git a/ios/MullvadVPN/TunnelManager/UpdateAccountDataOperation.swift b/ios/MullvadVPN/TunnelManager/UpdateAccountDataOperation.swift index 55f7e0eb8814..3992f17c9031 100644 --- a/ios/MullvadVPN/TunnelManager/UpdateAccountDataOperation.swift +++ b/ios/MullvadVPN/TunnelManager/UpdateAccountDataOperation.swift @@ -36,8 +36,7 @@ class UpdateAccountDataOperation: ResultOperation { return } - task = accountsProxy.getAccountData( - accountNumber: accountData.number, + task = accountsProxy.getAccountData(accountNumber: accountData.number).execute( retryStrategy: .default ) { result in self.dispatchQueue.async { diff --git a/ios/MullvadVPNTests/APIProxy+Stubs.swift b/ios/MullvadVPNTests/APIProxy+Stubs.swift index 7602a1fe7aa2..c802a8fa7154 100644 --- a/ios/MullvadVPNTests/APIProxy+Stubs.swift +++ b/ios/MullvadVPNTests/APIProxy+Stubs.swift @@ -36,15 +36,6 @@ struct APIProxyStub: APIQuerying { }) } - func createApplePayment( - accountNumber: String, - receiptString: Data, - retryStrategy: REST.RetryStrategy, - completionHandler: @escaping ProxyCompletionHandler - ) -> Cancellable { - AnyCancellable() - } - func sendProblemReport( _ body: REST.ProblemReportRequest, retryStrategy: REST.RetryStrategy, diff --git a/ios/MullvadVPNTests/AccountsProxy+Stubs.swift b/ios/MullvadVPNTests/AccountsProxy+Stubs.swift index 6dc2afdade60..18e940e8ae66 100644 --- a/ios/MullvadVPNTests/AccountsProxy+Stubs.swift +++ b/ios/MullvadVPNTests/AccountsProxy+Stubs.swift @@ -24,14 +24,6 @@ struct AccountsProxyStub: RESTAccountHandling { }) } - func getAccountData( - accountNumber: String, - retryStrategy: REST.RetryStrategy, - completion: @escaping ProxyCompletionHandler - ) -> Cancellable { - AnyCancellable() - } - func deleteAccount( accountNumber: String, retryStrategy: REST.RetryStrategy,