2022-06-14 15:12:46 -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
|
|
|
|
|
//
|
|
|
|
|
// BackendConfiguration.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Joshua Liebowitz on 6/13/22.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2023-05-19 13:27:44 -04:00
|
|
|
class BackendConfiguration {
|
2022-06-14 15:12:46 -07:00
|
|
|
|
|
|
|
|
let httpClient: HTTPClient
|
|
|
|
|
|
2022-08-16 09:29:36 -07:00
|
|
|
let operationDispatcher: OperationDispatcher
|
2022-06-14 15:12:46 -07:00
|
|
|
let operationQueue: OperationQueue
|
2024-04-10 14:48:09 +02:00
|
|
|
let diagnosticsQueue: OperationQueue
|
2022-06-14 15:12:46 -07:00
|
|
|
let dateProvider: DateProvider
|
2023-04-25 17:21:46 -03:00
|
|
|
let systemInfo: SystemInfo
|
2023-05-19 13:27:44 -04:00
|
|
|
let offlineCustomerInfoCreator: OfflineCustomerInfoCreator?
|
2022-06-14 15:12:46 -07:00
|
|
|
|
|
|
|
|
init(httpClient: HTTPClient,
|
2022-08-16 09:29:36 -07:00
|
|
|
operationDispatcher: OperationDispatcher,
|
2022-06-14 15:12:46 -07:00
|
|
|
operationQueue: OperationQueue,
|
2024-04-10 14:48:09 +02:00
|
|
|
diagnosticsQueue: OperationQueue,
|
2023-05-19 13:27:44 -04:00
|
|
|
systemInfo: SystemInfo,
|
|
|
|
|
offlineCustomerInfoCreator: OfflineCustomerInfoCreator?,
|
|
|
|
|
dateProvider: DateProvider = DateProvider()) {
|
2022-06-14 15:12:46 -07:00
|
|
|
self.httpClient = httpClient
|
2022-08-16 09:29:36 -07:00
|
|
|
self.operationDispatcher = operationDispatcher
|
2022-06-14 15:12:46 -07:00
|
|
|
self.operationQueue = operationQueue
|
2024-04-10 14:48:09 +02:00
|
|
|
self.diagnosticsQueue = diagnosticsQueue
|
2023-05-19 13:27:44 -04:00
|
|
|
self.offlineCustomerInfoCreator = offlineCustomerInfoCreator
|
2022-06-14 15:12:46 -07:00
|
|
|
self.dateProvider = dateProvider
|
2023-04-25 17:21:46 -03:00
|
|
|
self.systemInfo = systemInfo
|
2022-06-14 15:12:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func clearCache() {
|
|
|
|
|
self.httpClient.clearCaches()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-08-16 09:29:36 -07:00
|
|
|
|
2023-03-23 09:18:51 -07:00
|
|
|
extension BackendConfiguration: NetworkConfiguration {}
|
|
|
|
|
|
2022-08-16 09:29:36 -07:00
|
|
|
extension BackendConfiguration {
|
|
|
|
|
|
|
|
|
|
/// Adds the `operation` to the `OperationQueue` (based on `CallbackCacheStatus`) potentially adding a random delay.
|
2022-12-21 08:13:41 -08:00
|
|
|
func addCacheableOperation<T: CacheableNetworkOperation>(
|
|
|
|
|
with factory: CacheableNetworkOperationFactory<T>,
|
2024-07-23 12:41:16 -05:00
|
|
|
delay: JitterableDelay,
|
2022-08-16 09:29:36 -07:00
|
|
|
cacheStatus: CallbackCacheStatus
|
|
|
|
|
) {
|
2024-07-23 12:41:16 -05:00
|
|
|
self.operationDispatcher.dispatchOnWorkerThread(jitterableDelay: delay) {
|
2022-12-21 08:13:41 -08:00
|
|
|
self.operationQueue.addCacheableOperation(with: factory, cacheStatus: cacheStatus)
|
2022-08-16 09:29:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 14:48:09 +02:00
|
|
|
/// Adds the `operation` to the diagnostics `OperationQueue` potentially adding a random delay.
|
|
|
|
|
func addDiagnosticsOperation(
|
|
|
|
|
_ operation: NetworkOperation,
|
2024-07-23 12:41:16 -05:00
|
|
|
delay: JitterableDelay = .long
|
2024-04-10 14:48:09 +02:00
|
|
|
) {
|
2024-07-23 12:41:16 -05:00
|
|
|
self.operationDispatcher.dispatchOnWorkerThread(jitterableDelay: delay) {
|
2024-04-10 14:48:09 +02:00
|
|
|
self.diagnosticsQueue.addOperation(operation)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-16 09:29:36 -07:00
|
|
|
}
|
2022-08-31 13:30:38 -07:00
|
|
|
|
|
|
|
|
// @unchecked because:
|
|
|
|
|
// - `OperationQueue` is not `Sendable` as of Swift 5.7
|
2023-05-19 13:27:44 -04:00
|
|
|
// - Class is not `final` (it's mocked). This implicitly makes subclasses `Sendable` even if they're not thread-safe.
|
2022-08-31 13:30:38 -07:00
|
|
|
extension BackendConfiguration: @unchecked Sendable {}
|