Files
purchases-ios/Sources/Networking/OfferingsAPI.swift
T
Antonio PallaresandRevenueCat Git Bot 510f4e73a4 Add internal Offerings source properties (#5749)
* Add `isLoadShedderResponse` and `isFallbackURLResponse` properties to `VerifiedHTTPResponse`

Keeps the values also when returned by the ETagManager

* lint

* Add some tests for ETagManager

* Rename `isFallbackURLResponse` to `isFallbackUrlResponse` to prevent decoding issues

* Add tests for actual source of original cached response of ETagManager

* Add tests to HTTPClientTests regarding the new booleans

* lint

* Add `originalSource` to `CustomerInfo`

* Adds `isLoadedFromCache` boolean to CustomerInfo

* Add docs

* Unit tests for new customer info properties

* Make `OriginalSource.CustomerInfo` a `String` enum

* Add decoding tests for Customer Info with new original source field

* Add `.loadShedder` check for Customer Info obtained in Load Shedder integration tests

* Update fixture

* improve internal docs

* Add intermediate object `Offerings.Contents` to wrap `OfferingsResponse` + `OriginalSource` and `loadedFromCache`

* Adapt existing unit tests to changes in `Offerings` underlying objects

* Check Offerings original source in existing integration tests

* Fix StoreKit unit tests

* Fix RevenueCatUI tests

* More Offerings.Contents tests

* Fix test

* Add more tests

* Fix lint

* [skip ci] Generating new test snapshots (#5741)

* [skip ci] Generating new test snapshots (#5742)

* [skip ci] Generating new test snapshots (#5743)

* [skip ci] Generating new test snapshots (#5744)

* [skip ci] Generating new test snapshots (#5745)

* [skip ci] Generating new test snapshots (#5746)

* [skip ci] Generating new test snapshots (#5747)

* [skip ci] Generating new test snapshots (#5748)

* Use enum for `HTTPResponseSource`

* Update tests

* Adapt to changes in `VerifiedHTTPResponse`

* Lint

* Adapt to changes in `VerifiedHTTPResponse`

* update tests

* Add log when a request to the fallback URL was handled by load shedder (which should never happen)

* rename `HTTPResponseSource` to `HTTPResponseOriginalSource`

* Adapt to changes

* fix lint

* Adapt to changes

* Fix CustomerInfo's OriginalSource implementation + fix unit test

* Fix tests compilation

* Fix RCUI compilation

* Move isFromCache boolean from `Offerings.Contents` to `Offerings`

* Fix compilation of `DebugViewSwiftUITests`

* Fix compilation of RevenueCatUI tests

---------

Co-authored-by: RevenueCat Git Bot <72824662+RCGitBot@users.noreply.github.com>
2025-11-06 14:12:42 +01:00

115 lines
5.6 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
//
// OfferingsAPI.swift
//
// Created by Joshua Liebowitz on 6/15/22.
import Foundation
class OfferingsAPI {
typealias IntroEligibilityResponseHandler = ([String: IntroEligibility], BackendError?) -> Void
typealias OfferSigningResponseHandler = Backend.ResponseHandler<PostOfferForSigningOperation.SigningData>
typealias OfferingsResponseHandler = Backend.ResponseHandler<Offerings.Contents>
typealias WebOfferingProductsResponseHandler = Backend.ResponseHandler<WebOfferingProductsResponse>
private let offeringsCallbacksCache: CallbackCache<OfferingsCallback>
private let webOfferingProductsCallbacksCache: CallbackCache<WebOfferingProductsCallback>
private let backendConfig: BackendConfiguration
init(backendConfig: BackendConfiguration) {
self.backendConfig = backendConfig
self.offeringsCallbacksCache = .init()
self.webOfferingProductsCallbacksCache = .init()
}
func getOfferings(appUserID: String,
isAppBackgrounded: Bool,
completion: @escaping OfferingsResponseHandler) {
let config = NetworkOperation.UserSpecificConfiguration(httpClient: self.backendConfig.httpClient,
appUserID: appUserID)
let factory = GetOfferingsOperation.createFactory(
configuration: config,
offeringsCallbackCache: self.offeringsCallbacksCache
)
let offeringsCallback = OfferingsCallback(cacheKey: factory.cacheKey, completion: completion)
let cacheStatus = self.offeringsCallbacksCache.add(offeringsCallback)
if cacheStatus == .firstCallbackAddedToList {
Logger.debug(isAppBackgrounded
? Strings.offering.offerings_stale_updating_in_background
: Strings.offering.offerings_stale_updating_in_foreground)
}
self.backendConfig.addCacheableOperation(
with: factory,
delay: .default(forBackgroundedApp: isAppBackgrounded),
cacheStatus: cacheStatus
)
}
func getWebOfferingProducts(appUserID: String, completion: @escaping WebOfferingProductsResponseHandler) {
let config = NetworkOperation.UserSpecificConfiguration(httpClient: self.backendConfig.httpClient,
appUserID: appUserID)
let factory = GetWebOfferingProductsOperation.createFactory(
configuration: config,
webOfferingProductsCallbackCache: self.webOfferingProductsCallbacksCache
)
let webProductsCallback = WebOfferingProductsCallback(cacheKey: factory.cacheKey, completion: completion)
let cacheStatus = self.webOfferingProductsCallbacksCache.add(webProductsCallback)
self.backendConfig.addCacheableOperation(
with: factory,
delay: .none,
cacheStatus: cacheStatus
)
}
func getIntroEligibility(appUserID: String,
receiptData: Data,
productIdentifiers: Set<String>,
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,
receipt: EncodedAppleReceipt,
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,
receipt: receipt)
let postOfferForSigningOperation = PostOfferForSigningOperation(configuration: config,
postOfferForSigningData: postOfferData,
responseHandler: completion)
self.backendConfig.operationQueue.addOperation(postOfferForSigningOperation)
}
}
// @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 {}