Files
purchases-ios/Sources/Networking/BackendConfiguration.swift
T
NachoSoto 99c8a34e62 Offline Entitlements: use offline-computed CustomerInfo when server is down (#2368)
### Changes:
- Created `PurchasedProductsFetcherType` to be able to mock and hold
fetchers, even on older devices
- Created `ProductEntitlementMappingFetcher` to abstract injecting
`ProductEntitlementMapping`
- Added logic to `CustomerInfoResponseHandler` to compute offline
`CustomerInfo` after a server error (see #2367). **This is used by all
`CustomerInfo` requests as well as `PostReceiptDataOperation`**.
- Wrote comprehensive `CustomerInfoResponseHandler` tests

### TODO:
- [x] Don't cache offline computed `CustomerInfo`: #2378
- [x] Integration tests: #2501.
2023-05-19 10:27:44 -07:00

67 lines
2.1 KiB
Swift

//
// 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
class BackendConfiguration {
let httpClient: HTTPClient
let operationDispatcher: OperationDispatcher
let operationQueue: OperationQueue
let dateProvider: DateProvider
let systemInfo: SystemInfo
let offlineCustomerInfoCreator: OfflineCustomerInfoCreator?
init(httpClient: HTTPClient,
operationDispatcher: OperationDispatcher,
operationQueue: OperationQueue,
systemInfo: SystemInfo,
offlineCustomerInfoCreator: OfflineCustomerInfoCreator?,
dateProvider: DateProvider = DateProvider()) {
self.httpClient = httpClient
self.operationDispatcher = operationDispatcher
self.operationQueue = operationQueue
self.offlineCustomerInfoCreator = offlineCustomerInfoCreator
self.dateProvider = dateProvider
self.systemInfo = systemInfo
}
func clearCache() {
self.httpClient.clearCaches()
}
}
extension BackendConfiguration: NetworkConfiguration {}
extension BackendConfiguration {
/// Adds the `operation` to the `OperationQueue` (based on `CallbackCacheStatus`) potentially adding a random delay.
func addCacheableOperation<T: CacheableNetworkOperation>(
with factory: CacheableNetworkOperationFactory<T>,
withRandomDelay randomDelay: Bool,
cacheStatus: CallbackCacheStatus
) {
self.operationDispatcher.dispatchOnWorkerThread(withRandomDelay: randomDelay) {
self.operationQueue.addCacheableOperation(with: factory, cacheStatus: cacheStatus)
}
}
}
// @unchecked because:
// - `OperationQueue` is not `Sendable` as of Swift 5.7
// - Class is not `final` (it's mocked). This implicitly makes subclasses `Sendable` even if they're not thread-safe.
extension BackendConfiguration: @unchecked Sendable {}