@app-swiss-knife/react-native-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -0
- package/SwissKnifePurchases.podspec +40 -0
- package/android/build.gradle +89 -0
- package/android/gradle.properties +3 -0
- package/android/src/main/AndroidManifest.xml +26 -0
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +216 -0
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingPackage.kt +17 -0
- package/android/src/main/java/com/swissknife/purchases/Conversions.kt +15 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +371 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +497 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesPackage.kt +25 -0
- package/android/vendor/GENERATED.md +10 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +533 -0
- package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +326 -0
- package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +296 -0
- package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +503 -0
- package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +188 -0
- package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +204 -0
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +447 -0
- package/android/vendor/java/com/appswissknife/context/AppLifecycle.kt +120 -0
- package/android/vendor/java/com/appswissknife/context/Clock.kt +70 -0
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +138 -0
- package/android/vendor/java/com/appswissknife/context/IDs.kt +55 -0
- package/android/vendor/java/com/appswissknife/context/Reachability.kt +145 -0
- package/android/vendor/java/com/appswissknife/context/Session.kt +192 -0
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +945 -0
- package/android/vendor/java/com/appswissknife/crashreporting/SKScreenshot.kt +201 -0
- package/android/vendor/java/com/appswissknife/device/DeviceID.kt +172 -0
- package/android/vendor/java/com/appswissknife/diagnostics/SKLog.kt +97 -0
- package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +495 -0
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +522 -0
- package/android/vendor/java/com/appswissknife/models/CustomerInfo.kt +67 -0
- package/android/vendor/java/com/appswissknife/models/Enums.kt +35 -0
- package/android/vendor/java/com/appswissknife/models/Offerings.kt +80 -0
- package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +89 -0
- package/android/vendor/java/com/appswissknife/models/StoreTransaction.kt +17 -0
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +507 -0
- package/android/vendor/java/com/appswissknife/net/ExponentialBackoff.kt +69 -0
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +582 -0
- package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +320 -0
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +219 -0
- package/android/vendor/java/com/appswissknife/performance/SKFrameTracker.kt +94 -0
- package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +428 -0
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +560 -0
- package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +100 -0
- package/android/vendor/java/com/appswissknife/purchases/PriceEnrichmentProvider.kt +36 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +120 -0
- package/android/vendor/res/xml/swissknife_data_extraction_rules.xml +20 -0
- package/ios/SwissKnifeCrashReporting.m +32 -0
- package/ios/SwissKnifeCrashReporting.swift +186 -0
- package/ios/SwissKnifeJSON.swift +347 -0
- package/ios/SwissKnifePurchases.m +124 -0
- package/ios/SwissKnifePurchases.swift +523 -0
- package/ios/vendor/GENERATED.md +10 -0
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +488 -0
- package/ios/vendor/SwissKnifeiOS/AttributesClient.swift +460 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/Attribution.swift +307 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/AttributionNetwork.swift +92 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/AttributionState.swift +141 -0
- package/ios/vendor/SwissKnifeiOS/Context/AppLifecycle.swift +130 -0
- package/ios/vendor/SwissKnifeiOS/Context/Clock.swift +100 -0
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +190 -0
- package/ios/vendor/SwissKnifeiOS/Context/IDs.swift +64 -0
- package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +121 -0
- package/ios/vendor/SwissKnifeiOS/Context/Session.swift +223 -0
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +1287 -0
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKScreenshot.swift +175 -0
- package/ios/vendor/SwissKnifeiOS/DeviceID.swift +161 -0
- package/ios/vendor/SwissKnifeiOS/Diagnostics/SKLog.swift +128 -0
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +323 -0
- package/ios/vendor/SwissKnifeiOS/Identity/IdentityNetwork.swift +70 -0
- package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +329 -0
- package/ios/vendor/SwissKnifeiOS/Net/ExponentialBackoff.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +681 -0
- package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +409 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKFrameTracker.swift +89 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +440 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKURLSessionDelegate.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +646 -0
- package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +458 -0
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +374 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+Analytics.swift +46 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+DeviceID.swift +30 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+Session.swift +47 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +417 -0
- package/ios/vendor/SwissKnifeiOS/Types.swift +496 -0
- package/lib/commonjs/attribution.js +55 -0
- package/lib/commonjs/attribution.js.map +1 -0
- package/lib/commonjs/identity.js +126 -0
- package/lib/commonjs/identity.js.map +1 -0
- package/lib/commonjs/index.js +118 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/modules/analytics.js +646 -0
- package/lib/commonjs/modules/analytics.js.map +1 -0
- package/lib/commonjs/modules/attachments.js +169 -0
- package/lib/commonjs/modules/attachments.js.map +1 -0
- package/lib/commonjs/modules/crash-reporting.js +1327 -0
- package/lib/commonjs/modules/crash-reporting.js.map +1 -0
- package/lib/commonjs/modules/ids.js +50 -0
- package/lib/commonjs/modules/ids.js.map +1 -0
- package/lib/commonjs/modules/log.js +59 -0
- package/lib/commonjs/modules/log.js.map +1 -0
- package/lib/commonjs/modules/offline-store.js +44 -0
- package/lib/commonjs/modules/offline-store.js.map +1 -0
- package/lib/commonjs/modules/performance.js +494 -0
- package/lib/commonjs/modules/performance.js.map +1 -0
- package/lib/commonjs/native/module.js +41 -0
- package/lib/commonjs/native/module.js.map +1 -0
- package/lib/commonjs/native/spec.js +26 -0
- package/lib/commonjs/native/spec.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/purchases.js +194 -0
- package/lib/commonjs/purchases.js.map +1 -0
- package/lib/commonjs/types.js +236 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/attribution.js +51 -0
- package/lib/module/attribution.js.map +1 -0
- package/lib/module/identity.js +122 -0
- package/lib/module/identity.js.map +1 -0
- package/lib/module/index.js +36 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/modules/analytics.js +642 -0
- package/lib/module/modules/analytics.js.map +1 -0
- package/lib/module/modules/attachments.js +162 -0
- package/lib/module/modules/attachments.js.map +1 -0
- package/lib/module/modules/crash-reporting.js +1320 -0
- package/lib/module/modules/crash-reporting.js.map +1 -0
- package/lib/module/modules/ids.js +44 -0
- package/lib/module/modules/ids.js.map +1 -0
- package/lib/module/modules/log.js +54 -0
- package/lib/module/modules/log.js.map +1 -0
- package/lib/module/modules/offline-store.js +39 -0
- package/lib/module/modules/offline-store.js.map +1 -0
- package/lib/module/modules/performance.js +491 -0
- package/lib/module/modules/performance.js.map +1 -0
- package/lib/module/native/module.js +34 -0
- package/lib/module/native/module.js.map +1 -0
- package/lib/module/native/spec.js +22 -0
- package/lib/module/native/spec.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/purchases.js +187 -0
- package/lib/module/purchases.js.map +1 -0
- package/lib/module/types.js +230 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/commonjs/attribution.d.ts +24 -0
- package/lib/typescript/commonjs/attribution.d.ts.map +1 -0
- package/lib/typescript/commonjs/identity.d.ts +37 -0
- package/lib/typescript/commonjs/identity.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +15 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts +127 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/attachments.d.ts +50 -0
- package/lib/typescript/commonjs/modules/attachments.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +175 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/ids.d.ts +17 -0
- package/lib/typescript/commonjs/modules/ids.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/log.d.ts +12 -0
- package/lib/typescript/commonjs/modules/log.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/offline-store.d.ts +9 -0
- package/lib/typescript/commonjs/modules/offline-store.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/performance.d.ts +75 -0
- package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -0
- package/lib/typescript/commonjs/native/module.d.ts +6 -0
- package/lib/typescript/commonjs/native/module.d.ts.map +1 -0
- package/lib/typescript/commonjs/native/spec.d.ts +119 -0
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/purchases.d.ts +68 -0
- package/lib/typescript/commonjs/purchases.d.ts.map +1 -0
- package/lib/typescript/commonjs/types.d.ts +384 -0
- package/lib/typescript/commonjs/types.d.ts.map +1 -0
- package/lib/typescript/module/attribution.d.ts +24 -0
- package/lib/typescript/module/attribution.d.ts.map +1 -0
- package/lib/typescript/module/identity.d.ts +37 -0
- package/lib/typescript/module/identity.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +15 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/analytics.d.ts +127 -0
- package/lib/typescript/module/modules/analytics.d.ts.map +1 -0
- package/lib/typescript/module/modules/attachments.d.ts +50 -0
- package/lib/typescript/module/modules/attachments.d.ts.map +1 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts +175 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -0
- package/lib/typescript/module/modules/ids.d.ts +17 -0
- package/lib/typescript/module/modules/ids.d.ts.map +1 -0
- package/lib/typescript/module/modules/log.d.ts +12 -0
- package/lib/typescript/module/modules/log.d.ts.map +1 -0
- package/lib/typescript/module/modules/offline-store.d.ts +9 -0
- package/lib/typescript/module/modules/offline-store.d.ts.map +1 -0
- package/lib/typescript/module/modules/performance.d.ts +75 -0
- package/lib/typescript/module/modules/performance.d.ts.map +1 -0
- package/lib/typescript/module/native/module.d.ts +6 -0
- package/lib/typescript/module/native/module.d.ts.map +1 -0
- package/lib/typescript/module/native/spec.d.ts +119 -0
- package/lib/typescript/module/native/spec.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/purchases.d.ts +68 -0
- package/lib/typescript/module/purchases.d.ts.map +1 -0
- package/lib/typescript/module/types.d.ts +384 -0
- package/lib/typescript/module/types.d.ts.map +1 -0
- package/package.json +115 -0
- package/react-native.config.js +23 -0
- package/src/attribution.ts +56 -0
- package/src/identity.ts +133 -0
- package/src/index.ts +54 -0
- package/src/modules/analytics.ts +689 -0
- package/src/modules/attachments.ts +222 -0
- package/src/modules/crash-reporting.ts +1577 -0
- package/src/modules/ids.ts +41 -0
- package/src/modules/log.ts +56 -0
- package/src/modules/offline-store.ts +51 -0
- package/src/modules/performance.ts +589 -0
- package/src/native/module.ts +47 -0
- package/src/native/spec.ts +183 -0
- package/src/purchases.ts +214 -0
- package/src/types.ts +470 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/AttributesClient.swift
|
|
2
|
+
//
|
|
3
|
+
// Phase 3 of the RC rip-and-replace — user attributes route through our own
|
|
4
|
+
// backend (`POST /api/sdk/users/attributes`) instead of RevenueCat's
|
|
5
|
+
// attribution service. The four setter methods on `SwissKnife`
|
|
6
|
+
// (`setEmail` / `setPhoneNumber` / `setDisplayName` / `setPushToken` /
|
|
7
|
+
// `setAttributes`) call into this client now.
|
|
8
|
+
//
|
|
9
|
+
// Design constraints
|
|
10
|
+
// ------------------
|
|
11
|
+
// - Attributes are infrequent (a handful per session at most). No disk
|
|
12
|
+
// persistence — in-memory retry with bounded back-off is enough.
|
|
13
|
+
// - The endpoint is idempotent on the merge level (server overwrites
|
|
14
|
+
// attribute keys), so retrying a 5xx is safe.
|
|
15
|
+
// - 4xx (validation error) is permanent — log a warning and drop. Retrying
|
|
16
|
+
// the same bytes won't help.
|
|
17
|
+
// - Concurrent setters must not corrupt each other's state. We serialize
|
|
18
|
+
// all enqueue/send work on a private GCD queue.
|
|
19
|
+
//
|
|
20
|
+
// Back-off math
|
|
21
|
+
// -------------
|
|
22
|
+
// Mirrors `BackoffPolicy` from the analytics flush loop (1 / 2 / 4 / 8 / 16s
|
|
23
|
+
// ±25% jitter, capped at 5 retries). We don't reuse `BackoffPolicy` directly
|
|
24
|
+
// because that type is internal to the analytics module and the two surfaces
|
|
25
|
+
// have independent reliability needs — if we ever change the analytics
|
|
26
|
+
// schedule we shouldn't also drift the attributes schedule.
|
|
27
|
+
|
|
28
|
+
import Foundation
|
|
29
|
+
import os.log
|
|
30
|
+
|
|
31
|
+
// MARK: - Internal protocol over the network call (so tests can inject a fake)
|
|
32
|
+
|
|
33
|
+
/// One response from an attributes POST attempt.
|
|
34
|
+
internal struct AttributesTransportResponse {
|
|
35
|
+
let statusCode: Int
|
|
36
|
+
/// Value of the HTTP `Retry-After` header parsed as integer seconds, if
|
|
37
|
+
/// present. We only honor the integer form (RFC 7231 also allows an
|
|
38
|
+
/// HTTP-date; same reasoning as `EventTransport`).
|
|
39
|
+
let retryAfterSeconds: TimeInterval?
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
internal enum AttributesTransportResult {
|
|
43
|
+
case success(AttributesTransportResponse)
|
|
44
|
+
case failure(Error)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
internal protocol AttributesTransport {
|
|
48
|
+
func send(
|
|
49
|
+
url: URL,
|
|
50
|
+
apiKey: String,
|
|
51
|
+
body: Data,
|
|
52
|
+
completion: @escaping (AttributesTransportResult) -> Void
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Production transport — delegates to `BackendHttpClient`. The
|
|
57
|
+
/// `BackendHttpClient` handles X-API-Key injection, retry-on-5xx, and
|
|
58
|
+
/// `Date`-header Clock sync. We translate its typed errors back into
|
|
59
|
+
/// the `AttributesTransport`-shaped response so the in-class retry
|
|
60
|
+
/// scheduler (which already knows the 1/2/4/8/16s back-off contract)
|
|
61
|
+
/// keeps working without churn.
|
|
62
|
+
///
|
|
63
|
+
/// Why we still surface a per-feature transport seam: the AttributesClient
|
|
64
|
+
/// retry loop is INDEPENDENT of `BackendHttpClient`'s — it wants
|
|
65
|
+
/// dead-letter behavior + a 5-retry budget + scheduler injection for tests.
|
|
66
|
+
/// BackendHttpClient's retry is bounded inside its `performWithRetries`,
|
|
67
|
+
/// so we use `maxRetries: 0` here and let the AttributesClient's own loop
|
|
68
|
+
/// drive the schedule.
|
|
69
|
+
internal final class BackendHttpClientAttributesTransport: AttributesTransport {
|
|
70
|
+
|
|
71
|
+
private let http: BackendHttpClient
|
|
72
|
+
|
|
73
|
+
init(http: BackendHttpClient = .shared) {
|
|
74
|
+
self.http = http
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
func send(
|
|
78
|
+
url: URL,
|
|
79
|
+
apiKey: String,
|
|
80
|
+
body: Data,
|
|
81
|
+
completion: @escaping (AttributesTransportResult) -> Void
|
|
82
|
+
) {
|
|
83
|
+
// The `url` argument carries the historical (URL, apiKey)
|
|
84
|
+
// assembly; we extract the path portion and let BackendHttpClient
|
|
85
|
+
// re-assemble against its configured base URL. This means the
|
|
86
|
+
// `apiKey` argument is effectively ignored (BackendHttpClient
|
|
87
|
+
// resolves its own from the SwissKnife config) — same outcome
|
|
88
|
+
// because both consult the same shared config.
|
|
89
|
+
let path = url.path
|
|
90
|
+
Task {
|
|
91
|
+
do {
|
|
92
|
+
_ = try await self.http.postRaw(
|
|
93
|
+
path: path,
|
|
94
|
+
body: body,
|
|
95
|
+
maxRetries: 0
|
|
96
|
+
)
|
|
97
|
+
completion(.success(AttributesTransportResponse(
|
|
98
|
+
statusCode: 200,
|
|
99
|
+
retryAfterSeconds: nil
|
|
100
|
+
)))
|
|
101
|
+
} catch let error as PurchasesError {
|
|
102
|
+
switch error {
|
|
103
|
+
case .backendRejected(let code, _):
|
|
104
|
+
completion(.success(AttributesTransportResponse(
|
|
105
|
+
statusCode: code,
|
|
106
|
+
retryAfterSeconds: nil
|
|
107
|
+
)))
|
|
108
|
+
case .backendUnavailable(let code, _):
|
|
109
|
+
completion(.success(AttributesTransportResponse(
|
|
110
|
+
statusCode: code ?? 503,
|
|
111
|
+
retryAfterSeconds: nil
|
|
112
|
+
)))
|
|
113
|
+
default:
|
|
114
|
+
completion(.failure(error))
|
|
115
|
+
}
|
|
116
|
+
} catch {
|
|
117
|
+
completion(.failure(error))
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// MARK: - Client
|
|
124
|
+
|
|
125
|
+
/// Process-wide singleton that owns the attributes-POST pipeline.
|
|
126
|
+
internal final class AttributesClient {
|
|
127
|
+
|
|
128
|
+
static let shared = AttributesClient()
|
|
129
|
+
|
|
130
|
+
private let log = OSLog(subsystem: "com.swissknife", category: "attributes")
|
|
131
|
+
private let queue = DispatchQueue(label: "com.swissknife.attributes")
|
|
132
|
+
|
|
133
|
+
// Dependency seams — production wires the real transport + GCD-based
|
|
134
|
+
// delay; tests inject deterministic fakes.
|
|
135
|
+
internal var transport: AttributesTransport = BackendHttpClientAttributesTransport()
|
|
136
|
+
internal var retryScheduler: AttributesRetryScheduler = GCDAttributesRetryScheduler()
|
|
137
|
+
internal var randomSource: () -> Double = { Double.random(in: -1.0...1.0) }
|
|
138
|
+
|
|
139
|
+
// Configuration captured by `SwissKnife.configure()` and consulted by
|
|
140
|
+
// every setter call. We accept that the values may be nil if the caller
|
|
141
|
+
// forgets to configure first — in that case we drop the call with a
|
|
142
|
+
// warning, the same way `Purchases.shared.attribution.set*` would no-op
|
|
143
|
+
// pre-configure.
|
|
144
|
+
private var serverURL: String?
|
|
145
|
+
private var apiKey: String?
|
|
146
|
+
|
|
147
|
+
private init() {}
|
|
148
|
+
|
|
149
|
+
/// Called once on `SwissKnife.configure` with the resolved (proxy) URL +
|
|
150
|
+
/// API key. Idempotent — re-configuring just overwrites the cached
|
|
151
|
+
/// values. Both arguments may be empty/nil; setters short-circuit when
|
|
152
|
+
/// the configuration is incomplete.
|
|
153
|
+
internal func configure(serverURL: String?, apiKey: String) {
|
|
154
|
+
queue.sync {
|
|
155
|
+
self.serverURL = serverURL?.isEmpty == true ? nil : serverURL
|
|
156
|
+
self.apiKey = apiKey
|
|
157
|
+
}
|
|
158
|
+
os_log(
|
|
159
|
+
"AttributesClient.configure (instance %{public}@, apiKey len %d)",
|
|
160
|
+
log: log,
|
|
161
|
+
type: .info,
|
|
162
|
+
String(UInt(bitPattern: ObjectIdentifier(self).hashValue), radix: 16),
|
|
163
|
+
apiKey.count
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Test seam: snapshot the configured state so the test can assert it
|
|
168
|
+
/// was wired correctly without poking at private fields.
|
|
169
|
+
@available(*, unavailable, message: "Test seam — internal access only")
|
|
170
|
+
internal func _snapshotConfigurationForTesting() -> (serverURL: String?, apiKey: String?) {
|
|
171
|
+
queue.sync { (self.serverURL, self.apiKey) }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// Test seam used by `AttributesClientTests` only — leaves the
|
|
175
|
+
/// singleton in a known state between tests.
|
|
176
|
+
internal func _resetForTesting() {
|
|
177
|
+
queue.sync {
|
|
178
|
+
self.serverURL = nil
|
|
179
|
+
self.apiKey = nil
|
|
180
|
+
self.transport = BackendHttpClientAttributesTransport()
|
|
181
|
+
self.retryScheduler = GCDAttributesRetryScheduler()
|
|
182
|
+
self.randomSource = { Double.random(in: -1.0...1.0) }
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Test seam: block until all currently-queued work on the private
|
|
187
|
+
/// serial queue has finished. Lets tests assert on `sendCount` /
|
|
188
|
+
/// `observedDelays` without spin-waiting for late callbacks.
|
|
189
|
+
internal func _awaitForTesting() {
|
|
190
|
+
queue.sync {}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/// Drop in-memory configuration state. Called by `Identity.wipe()` for
|
|
194
|
+
/// GDPR-delete / test cleanup. Does NOT cancel in-flight retries —
|
|
195
|
+
/// those still consult the post-wipe (nil) config and short-circuit.
|
|
196
|
+
/// Distinct from `_resetForTesting`: it keeps the production
|
|
197
|
+
/// transport/scheduler in place.
|
|
198
|
+
internal func wipe() {
|
|
199
|
+
queue.sync {
|
|
200
|
+
self.serverURL = nil
|
|
201
|
+
self.apiKey = nil
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// MARK: Public setter API (called by SwissKnife)
|
|
206
|
+
|
|
207
|
+
/// Set a single reserved or non-reserved attribute. `nil` clears it.
|
|
208
|
+
/// Non-blocking — work is dispatched onto the private queue.
|
|
209
|
+
internal func set(
|
|
210
|
+
reservedKey key: String,
|
|
211
|
+
value: String?,
|
|
212
|
+
anonymousId: String?,
|
|
213
|
+
userId: String?
|
|
214
|
+
) {
|
|
215
|
+
let payload: [String: Any] = [key: value as Any? ?? NSNull()]
|
|
216
|
+
enqueue(payload: payload, anonymousId: anonymousId, userId: userId)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// Set a bag of attributes. Keys and values are passed through verbatim
|
|
220
|
+
/// — the backend enforces the validation contract; the client doesn't
|
|
221
|
+
/// duplicate it (one source of truth).
|
|
222
|
+
internal func setAttributes(
|
|
223
|
+
_ attributes: [String: String?],
|
|
224
|
+
anonymousId: String?,
|
|
225
|
+
userId: String?
|
|
226
|
+
) {
|
|
227
|
+
var payload: [String: Any] = [:]
|
|
228
|
+
for (k, v) in attributes {
|
|
229
|
+
payload[k] = v as Any? ?? NSNull()
|
|
230
|
+
}
|
|
231
|
+
enqueue(payload: payload, anonymousId: anonymousId, userId: userId)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// MARK: Internals
|
|
235
|
+
|
|
236
|
+
private func enqueue(
|
|
237
|
+
payload: [String: Any],
|
|
238
|
+
anonymousId: String?,
|
|
239
|
+
userId: String?
|
|
240
|
+
) {
|
|
241
|
+
queue.async { [weak self] in
|
|
242
|
+
guard let self = self else { return }
|
|
243
|
+
self.sendAttempt(
|
|
244
|
+
payload: payload,
|
|
245
|
+
anonymousId: anonymousId,
|
|
246
|
+
userId: userId,
|
|
247
|
+
attempt: 0
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private func sendAttempt(
|
|
253
|
+
payload: [String: Any],
|
|
254
|
+
anonymousId: String?,
|
|
255
|
+
userId: String?,
|
|
256
|
+
attempt: Int
|
|
257
|
+
) {
|
|
258
|
+
guard let apiKey = self.apiKey, !apiKey.isEmpty else {
|
|
259
|
+
os_log(
|
|
260
|
+
"AttributesClient: skipping setAttributes — SwissKnife.configure not called (instance %{public}@)",
|
|
261
|
+
log: log,
|
|
262
|
+
type: .info,
|
|
263
|
+
String(UInt(bitPattern: ObjectIdentifier(self).hashValue), radix: 16)
|
|
264
|
+
)
|
|
265
|
+
return
|
|
266
|
+
}
|
|
267
|
+
guard let anonymousId = anonymousId, !anonymousId.isEmpty else {
|
|
268
|
+
os_log(
|
|
269
|
+
"AttributesClient: skipping setAttributes — anonymous id unavailable",
|
|
270
|
+
log: log,
|
|
271
|
+
type: .info
|
|
272
|
+
)
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
let base = (self.serverURL?.isEmpty == false ? self.serverURL! : "https://api.swiss-knife.dev")
|
|
277
|
+
.trimmingTrailingSlash()
|
|
278
|
+
let urlString = "\(base)/api/sdk/users/attributes"
|
|
279
|
+
guard let url = URL(string: urlString) else {
|
|
280
|
+
os_log(
|
|
281
|
+
"AttributesClient: invalid attributes URL %{public}@",
|
|
282
|
+
log: log,
|
|
283
|
+
type: .error,
|
|
284
|
+
urlString
|
|
285
|
+
)
|
|
286
|
+
return
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
var body: [String: Any] = [
|
|
290
|
+
"anonymous_id": anonymousId,
|
|
291
|
+
"attributes": payload,
|
|
292
|
+
]
|
|
293
|
+
if let userId = userId, !userId.isEmpty {
|
|
294
|
+
body["user_id"] = userId
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
let data: Data
|
|
298
|
+
do {
|
|
299
|
+
data = try JSONSerialization.data(withJSONObject: body, options: [])
|
|
300
|
+
} catch {
|
|
301
|
+
os_log(
|
|
302
|
+
"AttributesClient: failed to encode attributes body: %{public}@",
|
|
303
|
+
log: log,
|
|
304
|
+
type: .error,
|
|
305
|
+
String(describing: error)
|
|
306
|
+
)
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
transport.send(url: url, apiKey: apiKey, body: data) { [weak self] result in
|
|
311
|
+
guard let self = self else { return }
|
|
312
|
+
self.queue.async {
|
|
313
|
+
self.handleResult(
|
|
314
|
+
result,
|
|
315
|
+
payload: payload,
|
|
316
|
+
anonymousId: anonymousId,
|
|
317
|
+
userId: userId,
|
|
318
|
+
attempt: attempt
|
|
319
|
+
)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private func handleResult(
|
|
325
|
+
_ result: AttributesTransportResult,
|
|
326
|
+
payload: [String: Any],
|
|
327
|
+
anonymousId: String,
|
|
328
|
+
userId: String?,
|
|
329
|
+
attempt: Int
|
|
330
|
+
) {
|
|
331
|
+
switch result {
|
|
332
|
+
case .success(let response):
|
|
333
|
+
switch response.statusCode {
|
|
334
|
+
case 200...299:
|
|
335
|
+
return // Done.
|
|
336
|
+
case 400...499 where response.statusCode != 429:
|
|
337
|
+
// Permanent failure — retry won't help, drop with a warning.
|
|
338
|
+
os_log(
|
|
339
|
+
"AttributesClient: server rejected attributes (HTTP %d); dropping",
|
|
340
|
+
log: log,
|
|
341
|
+
type: .error,
|
|
342
|
+
response.statusCode
|
|
343
|
+
)
|
|
344
|
+
return
|
|
345
|
+
case 429:
|
|
346
|
+
self.scheduleRetry(
|
|
347
|
+
payload: payload,
|
|
348
|
+
anonymousId: anonymousId,
|
|
349
|
+
userId: userId,
|
|
350
|
+
attempt: attempt,
|
|
351
|
+
retryAfter: response.retryAfterSeconds,
|
|
352
|
+
reason: "HTTP 429"
|
|
353
|
+
)
|
|
354
|
+
default:
|
|
355
|
+
self.scheduleRetry(
|
|
356
|
+
payload: payload,
|
|
357
|
+
anonymousId: anonymousId,
|
|
358
|
+
userId: userId,
|
|
359
|
+
attempt: attempt,
|
|
360
|
+
retryAfter: response.retryAfterSeconds,
|
|
361
|
+
reason: "HTTP \(response.statusCode)"
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
case .failure(let error):
|
|
365
|
+
self.scheduleRetry(
|
|
366
|
+
payload: payload,
|
|
367
|
+
anonymousId: anonymousId,
|
|
368
|
+
userId: userId,
|
|
369
|
+
attempt: attempt,
|
|
370
|
+
retryAfter: nil,
|
|
371
|
+
reason: "transport error: \(error.localizedDescription)"
|
|
372
|
+
)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private func scheduleRetry(
|
|
377
|
+
payload: [String: Any],
|
|
378
|
+
anonymousId: String,
|
|
379
|
+
userId: String?,
|
|
380
|
+
attempt: Int,
|
|
381
|
+
retryAfter: TimeInterval?,
|
|
382
|
+
reason: String
|
|
383
|
+
) {
|
|
384
|
+
let nextAttempt = attempt + 1
|
|
385
|
+
if nextAttempt > Self.maxRetries {
|
|
386
|
+
os_log(
|
|
387
|
+
"AttributesClient: dropped after %d attempts (%{public}@)",
|
|
388
|
+
log: log,
|
|
389
|
+
type: .error,
|
|
390
|
+
Self.maxRetries,
|
|
391
|
+
reason
|
|
392
|
+
)
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
let delay = computeDelay(attempt: attempt, honoring: retryAfter)
|
|
397
|
+
os_log(
|
|
398
|
+
"AttributesClient: retrying attempt %d/%d in %.2fs (%{public}@)",
|
|
399
|
+
log: log,
|
|
400
|
+
type: .info,
|
|
401
|
+
nextAttempt,
|
|
402
|
+
Self.maxRetries,
|
|
403
|
+
delay,
|
|
404
|
+
reason
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
retryScheduler.schedule(after: delay, on: queue) { [weak self] in
|
|
408
|
+
guard let self = self else { return }
|
|
409
|
+
self.sendAttempt(
|
|
410
|
+
payload: payload,
|
|
411
|
+
anonymousId: anonymousId,
|
|
412
|
+
userId: userId,
|
|
413
|
+
attempt: nextAttempt
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private func computeDelay(attempt: Int, honoring retryAfter: TimeInterval?) -> TimeInterval {
|
|
419
|
+
// 1/2/4/8/16s base, capped at maxBaseSeconds before jitter.
|
|
420
|
+
let exponential = pow(2.0, Double(attempt))
|
|
421
|
+
let capped = min(exponential, Self.maxBaseSeconds)
|
|
422
|
+
let r = randomSource()
|
|
423
|
+
let clamped = max(-1.0, min(1.0, r))
|
|
424
|
+
let jittered = capped * (1.0 + clamped * Self.jitterFraction)
|
|
425
|
+
guard let retryAfter = retryAfter, retryAfter > 0 else { return jittered }
|
|
426
|
+
return max(jittered, retryAfter)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// MARK: Tunables
|
|
430
|
+
|
|
431
|
+
private static let maxRetries = 5
|
|
432
|
+
private static let maxBaseSeconds: TimeInterval = 16.0
|
|
433
|
+
private static let jitterFraction: Double = 0.25
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// MARK: - Retry scheduler abstraction (mirrors SKAnalytics's seam)
|
|
437
|
+
|
|
438
|
+
internal protocol AttributesRetryScheduler {
|
|
439
|
+
func schedule(after delay: TimeInterval, on queue: DispatchQueue, _ block: @escaping () -> Void)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
internal final class GCDAttributesRetryScheduler: AttributesRetryScheduler {
|
|
443
|
+
func schedule(
|
|
444
|
+
after delay: TimeInterval,
|
|
445
|
+
on queue: DispatchQueue,
|
|
446
|
+
_ block: @escaping () -> Void
|
|
447
|
+
) {
|
|
448
|
+
queue.asyncAfter(deadline: .now() + delay, execute: block)
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// MARK: - String helper
|
|
453
|
+
|
|
454
|
+
private extension String {
|
|
455
|
+
func trimmingTrailingSlash() -> String {
|
|
456
|
+
var s = self
|
|
457
|
+
while s.hasSuffix("/") { s.removeLast() }
|
|
458
|
+
return s
|
|
459
|
+
}
|
|
460
|
+
}
|