Skip to content

Commit

Permalink
Adapty: implement test users
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksei-valiano committed Jul 4, 2024
1 parent 95d1c90 commit f0f51c2
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 16 deletions.
9 changes: 6 additions & 3 deletions Sources/Adapty+Paywalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ extension Adapty {
profileId: profileId,
placementId: placementId,
locale: locale,
cached: nil
cached: nil,
disableServerCache: false // don't loaded profile
) { (result: AdaptyResult<AdaptyPaywallChosen>) in
completion(
result
Expand Down Expand Up @@ -163,7 +164,8 @@ private extension AdaptyProfileManager {
placementId: placementId,
locale: locale,
segmentId: segmentId,
cached: cached
cached: cached,
disableServerCache: profile.value.isTestUser
) { [weak self] (result: AdaptyResult<AdaptyPaywallChosen>) in

guard let strongSelf = self, strongSelf.isActive else {
Expand Down Expand Up @@ -233,7 +235,8 @@ private extension AdaptyProfileManager {
profileId: profileId,
placementId: placementId,
locale: locale,
cached: cached
cached: cached,
disableServerCache: profile.value.isTestUser
) { [weak self] (result: AdaptyResult<AdaptyPaywallChosen>) in

switch result {
Expand Down
2 changes: 2 additions & 0 deletions Sources/AdaptyUI/AdaptyUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extension Adapty {
apiKeyPrefix: apiKeyPrefix,
paywallInstanceIdentity: paywallInstanceIdentity,
locale: locale,
disableServerCache: profileStorage.getProfile()?.value.isTestUser ?? false,
completion
)
}
Expand Down Expand Up @@ -151,6 +152,7 @@ extension Adapty {
paywallVariationId: paywallVariationId,
locale: locale,
adaptyUISDKVersion: adaptyUISDKVersion,
disableServerCache: self.profileStorage.getProfile()?.value.isTestUser ?? false,
termination
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ struct FetchFallbackViewConfigurationRequest: HTTPRequestWithDecodableResponse {
typealias ResponseBody = Backend.Response.ValueOfData<AdaptyUI.ViewConfiguration>

let endpoint: HTTPEndpoint
let queryItems: QueryItems

init(apiKeyPrefix: String, paywallInstanceIdentity: String, locale: AdaptyLocale) {
init(apiKeyPrefix: String, paywallInstanceIdentity: String, locale: AdaptyLocale, disableServerCache: Bool) {
endpoint = HTTPEndpoint(
method: .get,
path: "/sdk/in-apps/\(apiKeyPrefix)/paywall-builder/\(paywallInstanceIdentity)/\(AdaptyUI.builderVersion)/\(locale.languageCode)/fallback.json"
)

queryItems = QueryItems().setDisableServerCache(disableServerCache)
}
}

Expand All @@ -26,12 +29,14 @@ extension HTTPSession {
apiKeyPrefix: String,
paywallInstanceIdentity: String,
locale: AdaptyLocale,
disableServerCache: Bool,
_ completion: @escaping AdaptyResultCompletion<AdaptyUI.ViewConfiguration>
) {
let request = FetchFallbackViewConfigurationRequest(
apiKeyPrefix: apiKeyPrefix,
paywallInstanceIdentity: paywallInstanceIdentity,
locale: locale
locale: locale,
disableServerCache: disableServerCache
)
perform(
request,
Expand All @@ -41,6 +46,7 @@ extension HTTPSession {
"paywall_instance_id": .value(paywallInstanceIdentity),
"builder_version": .value(AdaptyUI.builderVersion),
"language_code": .valueOrNil(locale.languageCode),
"disable_server_cache": .value(disableServerCache),
]
) { [weak self] (result: FetchFallbackViewConfigurationRequest.Result) in
switch result {
Expand All @@ -62,6 +68,7 @@ extension HTTPSession {
apiKeyPrefix: apiKeyPrefix,
paywallInstanceIdentity: paywallInstanceIdentity,
locale: .defaultPaywallLocale,
disableServerCache: disableServerCache,
completion
)
}
Expand Down
14 changes: 11 additions & 3 deletions Sources/AdaptyUI/Requests/FetchViewConfigurationRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ struct FetchViewConfigurationRequest: HTTPRequestWithDecodableResponse {

let endpoint: HTTPEndpoint
let headers: Headers
let queryItems: QueryItems

init(apiKeyPrefix: String, paywallVariationId: String, locale: AdaptyLocale, md5Hash: String, adaptyUISDKVersion: String) {
init(apiKeyPrefix: String, paywallVariationId: String, locale: AdaptyLocale, md5Hash: String, adaptyUISDKVersion: String, disableServerCache: Bool) {
endpoint = HTTPEndpoint(
method: .get,
path: "/sdk/in-apps/\(apiKeyPrefix)/paywall-builder/\(paywallVariationId)/\(md5Hash)/"
Expand All @@ -24,6 +25,8 @@ struct FetchViewConfigurationRequest: HTTPRequestWithDecodableResponse {
.setViewConfigurationLocale(locale)
.setVisualBuilderVersion(AdaptyUI.builderVersion)
.setAdaptyUISDKVersion(adaptyUISDKVersion)

queryItems = QueryItems().setDisableServerCache(disableServerCache)
}
}

Expand All @@ -33,6 +36,7 @@ extension HTTPSession {
paywallVariationId: String,
locale: AdaptyLocale,
adaptyUISDKVersion: String,
disableServerCache: Bool,
_ completion: @escaping AdaptyResultCompletion<AdaptyUI.ViewConfiguration>
) {
let md5Hash = "{\"builder_version\":\"\(AdaptyUI.builderVersion)\",\"locale\":\"\(locale.id.lowercased())\"}".md5.hexString
Expand All @@ -42,13 +46,17 @@ extension HTTPSession {
paywallVariationId: paywallVariationId,
locale: locale,
md5Hash: md5Hash,
adaptyUISDKVersion: adaptyUISDKVersion
adaptyUISDKVersion: adaptyUISDKVersion,
disableServerCache: disableServerCache
)

perform(
request,
logName: "get_paywall_builder",
logParams: ["variation_id": .value(paywallVariationId)]
logParams: [
"variation_id": .value(paywallVariationId),
"disable_server_cache": .value(disableServerCache),
]
) { (result: FetchViewConfigurationRequest.Result) in
switch result {
case let .failure(error):
Expand Down
18 changes: 14 additions & 4 deletions Sources/Backend/Backend.QueryItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import Foundation

extension Backend.Request {
fileprivate static let profileIdQueryItemName = "profile_id"
fileprivate static let discountIdQueryItemName = "offer_code"
fileprivate static let vendorProductIdQueryItemName = "product"
private extension Backend.Request {
static let profileIdQueryItemName = "profile_id"
static let discountIdQueryItemName = "offer_code"
static let vendorProductIdQueryItemName = "product"
static let disableServerCacheQueryItemName = "disable_cache"
}

extension [HTTPRequest.QueryItems.Element] {
Expand Down Expand Up @@ -40,4 +41,13 @@ extension [HTTPRequest.QueryItems.Element] {
}
return queryItems
}

func setDisableServerCache(_ disableServerCache: Bool) -> Self {
var queryItems = filter { $0.name != Backend.Request.disableServerCacheQueryItemName }

if disableServerCache {
queryItems.append(URLQueryItem(name: Backend.Request.disableServerCacheQueryItemName, value: UUID().uuidString))
}
return queryItems
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ private struct FetchFallbackPaywallVariationsRequest: HTTPRequestWithDecodableRe
let endpoint: HTTPEndpoint
let profileId: String
let cached: AdaptyPaywall?
let queryItems: QueryItems

func getDecoder(_ jsonDecoder: JSONDecoder) -> (HTTPDataResponse) -> HTTPResponse<ResponseBody>.Result {
createDecoder(jsonDecoder, profileId, cached)
}

init(apiKeyPrefix: String, profileId: String, placementId: String, locale: AdaptyLocale, cached: AdaptyPaywall?) {
init(apiKeyPrefix: String, profileId: String, placementId: String, locale: AdaptyLocale, cached: AdaptyPaywall?, disableServerCache: Bool) {
self.profileId = profileId
self.cached = cached
endpoint = HTTPEndpoint(
method: .get,
path: "/sdk/in-apps/\(apiKeyPrefix)/paywall/variations/\(placementId)/app_store/\(locale.languageCode.lowercased())/\(AdaptyUI.builderVersion)/fallback.json"
)

queryItems = QueryItems().setDisableServerCache(disableServerCache)
}
}

Expand All @@ -35,14 +38,16 @@ extension HTTPSession {
placementId: String,
locale: AdaptyLocale,
cached: AdaptyPaywall?,
disableServerCache: Bool,
_ completion: @escaping AdaptyResultCompletion<AdaptyPaywallChosen>
) {
let request = FetchFallbackPaywallVariationsRequest(
apiKeyPrefix: apiKeyPrefix,
profileId: profileId,
placementId: placementId,
locale: locale,
cached: cached
cached: cached,
disableServerCache: disableServerCache
)

perform(
Expand All @@ -53,6 +58,7 @@ extension HTTPSession {
"placement_id": .value(placementId),
"language_code": .valueOrNil(locale.languageCode),
"builder_version": .value(AdaptyUI.builderVersion),
"disable_server_cache": .value(disableServerCache),
]
) { [weak self] (result: FetchFallbackPaywallVariationsRequest.Result) in
switch result {
Expand All @@ -75,6 +81,7 @@ extension HTTPSession {
placementId: placementId,
locale: .defaultPaywallLocale,
cached: cached,
disableServerCache: disableServerCache,
completion
)
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/Backend/Requests/FetchPaywallVariationsRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ private struct FetchPaywallVariationsRequest: HTTPRequestWithDecodableResponse {
let headers: Headers
let profileId: String
let cached: AdaptyPaywall?
let queryItems: QueryItems

func getDecoder(_ jsonDecoder: JSONDecoder) -> (HTTPDataResponse) -> HTTPResponse<ResponseBody>.Result {
createDecoder(jsonDecoder, profileId, cached)
}

init(apiKeyPrefix: String, profileId: String, placementId: String, locale: AdaptyLocale, md5Hash: String, segmentId: String, cached: AdaptyPaywall?) {
init(apiKeyPrefix: String, profileId: String, placementId: String, locale: AdaptyLocale, md5Hash: String, segmentId: String, cached: AdaptyPaywall?, disableServerCache: Bool) {
self.profileId = profileId
self.cached = cached

Expand All @@ -34,6 +35,8 @@ private struct FetchPaywallVariationsRequest: HTTPRequestWithDecodableResponse {
.setBackendProfileId(profileId)
.setVisualBuilderVersion(AdaptyUI.builderVersion)
.setSegmentId(segmentId)

queryItems = QueryItems().setDisableServerCache(disableServerCache)
}
}

Expand Down Expand Up @@ -91,6 +94,7 @@ extension HTTPSession {
locale: AdaptyLocale,
segmentId: String,
cached: AdaptyPaywall?,
disableServerCache: Bool,
_ completion: @escaping AdaptyResultCompletion<AdaptyPaywallChosen>
) {
let md5Hash = "{\"builder_version\":\"\(AdaptyUI.builderVersion)\",\"locale\":\"\(locale.id.lowercased())\",\"segment_hash\":\"\(segmentId)\",\"store\":\"app_store\"}".md5.hexString
Expand All @@ -102,7 +106,8 @@ extension HTTPSession {
locale: locale,
md5Hash: md5Hash,
segmentId: segmentId,
cached: cached
cached: cached,
disableServerCache: disableServerCache
)

perform(
Expand All @@ -115,6 +120,7 @@ extension HTTPSession {
"segment_id": .value(segmentId),
"builder_version": .value(AdaptyUI.builderVersion),
"md5": .value(md5Hash),
"disable_server_cache": .value(disableServerCache),
]
) { (result: FetchPaywallVariationsRequest.Result) in
completion(result.map { $0.body }.mapError { $0.asAdaptyError })
Expand Down
6 changes: 6 additions & 0 deletions Sources/Entities/AdaptyProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public struct AdaptyProfile {
public let customerUserId: String?

let segmentId: String
let isTestUser: Bool

let codableCustomAttributes: AdaptyProfile.CustomAttributes?

Expand All @@ -38,6 +39,7 @@ extension AdaptyProfile: Equatable {
lhs.profileId == rhs.profileId
&& lhs.customerUserId == rhs.customerUserId
&& lhs.segmentId == rhs.segmentId
&& lhs.isTestUser == rhs.isTestUser
&& lhs.codableCustomAttributes == rhs.codableCustomAttributes
&& lhs.accessLevels == rhs.accessLevels
&& lhs.subscriptions == rhs.subscriptions
Expand All @@ -50,6 +52,7 @@ extension AdaptyProfile: CustomStringConvertible {
"(profileId: \(profileId), "
+ (customerUserId.map { "customerUserId: \($0), " } ?? "")
+ "segmentId: \(segmentId), "
+ "segmentId: \(segmentId), isTestuser: \(isTestUser), "
+ (codableCustomAttributes == nil ? "" : "customAttributes: \(customAttributes), ")
+ "accessLevels: \(accessLevels), subscriptions: \(subscriptions), nonSubscriptions: \(nonSubscriptions))"
}
Expand All @@ -68,6 +71,7 @@ extension AdaptyProfile: Codable {
case nonSubscriptions = "non_subscriptions"
case version = "timestamp"

case isTestUser = "is_test_user"
case attributes
}

Expand All @@ -80,6 +84,7 @@ extension AdaptyProfile: Codable {
profileId = try container.decode(String.self, forKey: .profileId)
customerUserId = try container.decodeIfPresent(String.self, forKey: .customerUserId)
segmentId = try container.decode(String.self, forKey: .segmentId)
isTestUser = try container.decodeIfPresent(Bool.self, forKey: .isTestUser) ?? false
version = try container.decodeIfPresent(Int64.self, forKey: .version) ?? 0
codableCustomAttributes = try container.decodeIfPresent(AdaptyProfile.CustomAttributes.self, forKey: .customAttributes)
customAttributes = codableCustomAttributes?.convertToSimpleDictionary() ?? [:]
Expand All @@ -93,6 +98,7 @@ extension AdaptyProfile: Codable {
try container.encode(profileId, forKey: .profileId)
try container.encodeIfPresent(customerUserId, forKey: .customerUserId)
try container.encode(segmentId, forKey: .segmentId)
try container.encode(isTestUser, forKey: .isTestUser)
try container.encode(version, forKey: .version)
try container.encodeIfPresent(codableCustomAttributes, forKey: .customAttributes)
if !accessLevels.isEmpty {
Expand Down

0 comments on commit f0f51c2

Please sign in to comment.