2022-06-16 11:17:29 -07:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// IdentityAPI.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Joshua Liebowitz on 6/15/22.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
class IdentityAPI {
|
|
|
|
|
|
2022-08-15 15:01:51 -07:00
|
|
|
typealias LogInResponse = Result<(info: CustomerInfo, created: Bool), BackendError>
|
|
|
|
|
typealias LogInResponseHandler = (LogInResponse) -> Void
|
2022-06-16 11:17:29 -07:00
|
|
|
|
|
|
|
|
private let logInCallbacksCache: CallbackCache<LogInCallback>
|
|
|
|
|
private let backendConfig: BackendConfiguration
|
|
|
|
|
|
|
|
|
|
init(backendConfig: BackendConfiguration) {
|
|
|
|
|
self.backendConfig = backendConfig
|
2022-08-15 15:10:50 -07:00
|
|
|
self.logInCallbacksCache = CallbackCache<LogInCallback>()
|
2022-06-16 11:17:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func logIn(currentAppUserID: String,
|
|
|
|
|
newAppUserID: String,
|
|
|
|
|
completion: @escaping LogInResponseHandler) {
|
|
|
|
|
let config = NetworkOperation.UserSpecificConfiguration(httpClient: self.backendConfig.httpClient,
|
|
|
|
|
appUserID: currentAppUserID)
|
2022-12-21 08:13:41 -08:00
|
|
|
let factory = LogInOperation.createFactory(configuration: config,
|
|
|
|
|
newAppUserID: newAppUserID,
|
|
|
|
|
loginCallbackCache: self.logInCallbacksCache)
|
2022-06-16 11:17:29 -07:00
|
|
|
|
2022-12-21 08:13:41 -08:00
|
|
|
let loginCallback = LogInCallback(cacheKey: factory.cacheKey, completion: completion)
|
2022-10-11 12:47:17 -07:00
|
|
|
let cacheStatus = self.logInCallbacksCache.add(loginCallback)
|
2022-06-16 11:17:29 -07:00
|
|
|
|
2022-12-21 08:13:41 -08:00
|
|
|
self.backendConfig.operationQueue.addCacheableOperation(with: factory, cacheStatus: cacheStatus)
|
2022-06-16 11:17:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-08-31 13:30:38 -07:00
|
|
|
|
|
|
|
|
// @unchecked because:
|
|
|
|
|
// - Class is not `final` (it's mocked). This implicitly makes subclasses `Sendable` even if they're not thread-safe.
|
|
|
|
|
extension IdentityAPI: @unchecked Sendable {}
|