diff options
| author | Jon Petersson <jon.petersson@mullvad.net> | 2025-04-08 16:51:28 +0200 |
|---|---|---|
| committer | Jon Petersson <jon.petersson@mullvad.net> | 2025-04-08 16:51:28 +0200 |
| commit | 9a6938a4a32ecc162cc5afcf15fa636de2f9b9fe (patch) | |
| tree | b19fdb5f1a4b299de54d323965be32a3756270d6 /ios/MullvadREST/MullvadAPI/APIHandlers | |
| parent | 5a53a0479d33d9cdab1f3859706fb2ff776ee56a (diff) | |
| parent | 4ae7d50075a6e82a0d1edabf26ce13d9357479cb (diff) | |
| download | mullvadvpn-9a6938a4a32ecc162cc5afcf15fa636de2f9b9fe.tar.xz mullvadvpn-9a6938a4a32ecc162cc5afcf15fa636de2f9b9fe.zip | |
Merge branch 'use-mullvad-api-instead-of-urlsession-in-accounts-proxy-ios-982'
Diffstat (limited to 'ios/MullvadREST/MullvadAPI/APIHandlers')
| -rw-r--r-- | ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAPIProxy.swift | 31 | ||||
| -rw-r--r-- | ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAccountProxy.swift | 125 |
2 files changed, 156 insertions, 0 deletions
diff --git a/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAPIProxy.swift b/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAPIProxy.swift index be478ecb59..5d2555f07f 100644 --- a/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAPIProxy.swift +++ b/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAPIProxy.swift @@ -11,6 +11,37 @@ import MullvadTypes import Operations import WireGuardKitTypes +public protocol APIQuerying: Sendable { + func getAddressList( + retryStrategy: REST.RetryStrategy, + completionHandler: @escaping @Sendable ProxyCompletionHandler<[AnyIPEndpoint]> + ) -> Cancellable + + func getRelays( + etag: String?, + retryStrategy: REST.RetryStrategy, + completionHandler: @escaping @Sendable ProxyCompletionHandler<REST.ServerRelaysCacheResponse> + ) -> Cancellable + + func createApplePayment( + accountNumber: String, + receiptString: Data + ) -> any RESTRequestExecutor<REST.CreateApplePaymentResponse> + + func sendProblemReport( + _ body: REST.ProblemReportRequest, + retryStrategy: REST.RetryStrategy, + completionHandler: @escaping @Sendable ProxyCompletionHandler<Void> + ) -> Cancellable + + func submitVoucher( + voucherCode: String, + accountNumber: String, + retryStrategy: REST.RetryStrategy, + completionHandler: @escaping @Sendable ProxyCompletionHandler<REST.SubmitVoucherResponse> + ) -> Cancellable +} + extension REST { public final class MullvadAPIProxy: APIQuerying, @unchecked Sendable { let transportProvider: APITransportProviderProtocol diff --git a/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAccountProxy.swift b/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAccountProxy.swift new file mode 100644 index 0000000000..7ee228b8a5 --- /dev/null +++ b/ios/MullvadREST/MullvadAPI/APIHandlers/MullvadAccountProxy.swift @@ -0,0 +1,125 @@ +// +// MullvadAccountProxy.swift +// MullvadVPN +// +// Created by Jon Petersson on 2025-03-31. +// Copyright © 2025 Mullvad VPN AB. All rights reserved. +// + +import MullvadRustRuntime +import MullvadTypes +import Operations +import WireGuardKitTypes + +public protocol RESTAccountHandling: Sendable { + func createAccount( + retryStrategy: REST.RetryStrategy, + completion: @escaping @Sendable ProxyCompletionHandler<NewAccountData> + ) -> Cancellable + + func getAccountData( + accountNumber: String, + retryStrategy: REST.RetryStrategy, + completion: @escaping @Sendable ProxyCompletionHandler<Account> + ) -> Cancellable + + func deleteAccount( + accountNumber: String, + retryStrategy: REST.RetryStrategy, + completion: @escaping ProxyCompletionHandler<Void> + ) -> Cancellable +} + +extension REST { + public final class MullvadAccountProxy: RESTAccountHandling, @unchecked Sendable { + let transportProvider: APITransportProviderProtocol + let dispatchQueue: DispatchQueue + let operationQueue = AsyncOperationQueue() + let responseDecoder: JSONDecoder + + public init( + transportProvider: APITransportProviderProtocol, + dispatchQueue: DispatchQueue, + responseDecoder: JSONDecoder + ) { + self.transportProvider = transportProvider + self.dispatchQueue = dispatchQueue + self.responseDecoder = responseDecoder + } + + public func createAccount( + retryStrategy: REST.RetryStrategy, + completion: @escaping ProxyCompletionHandler<NewAccountData> + ) -> Cancellable { + let responseHandler = rustResponseHandler( + decoding: NewAccountData.self, + with: responseDecoder + ) + + return createNetworkOperation( + request: .createAccount(retryStrategy), + responseHandler: responseHandler, + completionHandler: completion + ) + } + + public func getAccountData( + accountNumber: String, + retryStrategy: REST.RetryStrategy, + completion: @escaping ProxyCompletionHandler<Account> + ) -> Cancellable { + let responseHandler = rustResponseHandler( + decoding: Account.self, + with: responseDecoder + ) + + return createNetworkOperation( + request: .getAccount(retryStrategy, accountNumber: accountNumber), + responseHandler: responseHandler, + completionHandler: completion + ) + } + + public func deleteAccount( + accountNumber: String, + retryStrategy: RetryStrategy, + completion: @escaping ProxyCompletionHandler<Void> + ) -> Cancellable { + let request = APIRequest.deleteAccount(retryStrategy, accountNumber: accountNumber) + + let networkOperation = MullvadApiNetworkOperation( + name: request.name, + dispatchQueue: dispatchQueue, + request: request, + transportProvider: transportProvider, + responseDecoder: responseDecoder, + responseHandler: rustEmptyResponseHandler(), + completionHandler: completion + ) + + operationQueue.addOperation(networkOperation) + + return networkOperation + } + + private func createNetworkOperation<Success: Decodable>( + request: APIRequest, + responseHandler: RustResponseHandler<Success>, + completionHandler: @escaping @Sendable ProxyCompletionHandler<Success> + ) -> MullvadApiNetworkOperation<Success> { + let networkOperation = MullvadApiNetworkOperation( + name: request.name, + dispatchQueue: dispatchQueue, + request: request, + transportProvider: transportProvider, + responseDecoder: responseDecoder, + responseHandler: responseHandler, + completionHandler: completionHandler + ) + + operationQueue.addOperation(networkOperation) + + return networkOperation + } + } +} |
