2022-06-16 14:00:40 -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
|
|
|
|
|
//
|
|
|
|
|
// OfferingsAPI.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Joshua Liebowitz on 6/15/22.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
class OfferingsAPI {
|
|
|
|
|
|
|
|
|
|
typealias IntroEligibilityResponseHandler = ([String: IntroEligibility], BackendError?) -> Void
|
2023-03-23 09:18:51 -07:00
|
|
|
typealias OfferSigningResponseHandler = Backend.ResponseHandler<PostOfferForSigningOperation.SigningData>
|
|
|
|
|
typealias OfferingsResponseHandler = Backend.ResponseHandler<OfferingsResponse>
|
2022-06-16 14:00:40 -07:00
|
|
|
|
|
|
|
|
private let offeringsCallbacksCache: CallbackCache<OfferingsCallback>
|
|
|
|
|
private let backendConfig: BackendConfiguration
|
|
|
|
|
|
|
|
|
|
init(backendConfig: BackendConfiguration) {
|
|
|
|
|
self.backendConfig = backendConfig
|
2023-03-23 09:18:51 -07:00
|
|
|
self.offeringsCallbacksCache = .init()
|
2022-06-16 14:00:40 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-16 09:29:36 -07:00
|
|
|
func getOfferings(appUserID: String,
|
2023-08-07 17:35:23 -07:00
|
|
|
isAppBackgrounded: Bool,
|
2022-08-16 09:29:36 -07:00
|
|
|
completion: @escaping OfferingsResponseHandler) {
|
2022-06-16 14:00:40 -07:00
|
|
|
let config = NetworkOperation.UserSpecificConfiguration(httpClient: self.backendConfig.httpClient,
|
|
|
|
|
appUserID: appUserID)
|
2022-12-21 08:13:41 -08:00
|
|
|
let factory = GetOfferingsOperation.createFactory(
|
|
|
|
|
configuration: config,
|
|
|
|
|
offeringsCallbackCache: self.offeringsCallbacksCache
|
|
|
|
|
)
|
2022-06-16 14:00:40 -07:00
|
|
|
|
2022-12-21 08:13:41 -08:00
|
|
|
let offeringsCallback = OfferingsCallback(cacheKey: factory.cacheKey, completion: completion)
|
2022-10-11 12:47:17 -07:00
|
|
|
let cacheStatus = self.offeringsCallbacksCache.add(offeringsCallback)
|
2022-06-16 14:00:40 -07:00
|
|
|
|
2023-08-07 17:35:23 -07:00
|
|
|
if cacheStatus == .firstCallbackAddedToList {
|
|
|
|
|
Logger.debug(isAppBackgrounded
|
|
|
|
|
? Strings.offering.offerings_stale_updating_in_background
|
|
|
|
|
: Strings.offering.offerings_stale_updating_in_foreground)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 08:13:41 -08:00
|
|
|
self.backendConfig.addCacheableOperation(
|
|
|
|
|
with: factory,
|
2023-09-08 07:28:54 -07:00
|
|
|
delay: .default(forBackgroundedApp: isAppBackgrounded),
|
2022-12-21 08:13:41 -08:00
|
|
|
cacheStatus: cacheStatus
|
|
|
|
|
)
|
2022-06-16 14:00:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getIntroEligibility(appUserID: String,
|
|
|
|
|
receiptData: Data,
|
2023-08-07 13:30:26 -07:00
|
|
|
productIdentifiers: Set<String>,
|
2022-06-16 14:00:40 -07:00
|
|
|
completion: @escaping IntroEligibilityResponseHandler) {
|
|
|
|
|
let config = NetworkOperation.UserSpecificConfiguration(httpClient: self.backendConfig.httpClient,
|
|
|
|
|
appUserID: appUserID)
|
|
|
|
|
let getIntroEligibilityOperation = GetIntroEligibilityOperation(configuration: config,
|
|
|
|
|
receiptData: receiptData,
|
|
|
|
|
productIdentifiers: productIdentifiers,
|
|
|
|
|
responseHandler: completion)
|
|
|
|
|
self.backendConfig.operationQueue.addOperation(getIntroEligibilityOperation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// swiftlint:disable:next function_parameter_count
|
|
|
|
|
func post(offerIdForSigning offerIdentifier: String,
|
|
|
|
|
productIdentifier: String,
|
|
|
|
|
subscriptionGroup: String,
|
2023-10-16 22:45:29 +02:00
|
|
|
receipt: EncodedAppleReceipt,
|
2022-06-16 14:00:40 -07:00
|
|
|
appUserID: String,
|
|
|
|
|
completion: @escaping OfferSigningResponseHandler) {
|
|
|
|
|
let config = NetworkOperation.UserSpecificConfiguration(httpClient: self.backendConfig.httpClient,
|
|
|
|
|
appUserID: appUserID)
|
|
|
|
|
|
|
|
|
|
let postOfferData = PostOfferForSigningOperation.PostOfferForSigningData(offerIdentifier: offerIdentifier,
|
|
|
|
|
productIdentifier: productIdentifier,
|
|
|
|
|
subscriptionGroup: subscriptionGroup,
|
2023-10-16 22:45:29 +02:00
|
|
|
receipt: receipt)
|
2022-06-16 14:00:40 -07:00
|
|
|
let postOfferForSigningOperation = PostOfferForSigningOperation(configuration: config,
|
|
|
|
|
postOfferForSigningData: postOfferData,
|
|
|
|
|
responseHandler: completion)
|
|
|
|
|
self.backendConfig.operationQueue.addOperation(postOfferForSigningOperation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
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 OfferingsAPI: @unchecked Sendable {}
|