Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement migration identity keys #556

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LeanplumSDK
//
// Created by Nikola Zagorchev on 6.10.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

@objc public extension MigrationManager {
var state: MigrationState {
Expand All @@ -26,6 +26,10 @@
return attributeMappings
}

var cleverTapIdentityKeys: [String] {
return identityKeys
}

var hasLaunched: Bool {
guard let wrapper = wrapper else { return false }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LeanplumSDK
//
// Created by Nikola Zagorchev on 2.10.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation

Expand All @@ -15,15 +15,9 @@ import Foundation
static let MigrationStateKey = "__leanplum_migration_state"
static let RegionCodeKey = "__leanplum_region_code"
static let AttributeMappingsKey = "__leanplum_attribute_mappings"
static let IdentityKeysKey = "__leanplum_identity_keys"

static let MigrateStateResponseParam = "migrateState"
static let SdkResponseParam = "sdk"
static let CTResponseParam = "ct"
static let AccountIdResponseParam = "accountId"
static let AccountTokenResponseParam = "token"
static let RegionCodeResponseParam = "regionCode"
static let AttributeMappingsResponseParam = "attributeMappings"
static let HashResponseParam = "sha256";
static let DefaultIdentityKeys = [IdentityManager.Constants.Identity]

static let CleverTapRequestArg = "ct"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,130 @@
// LeanplumSDK
//
// Created by Nikola Zagorchev on 3.10.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation

@objc public extension MigrationManager {
// migrateState = {

// MARK: - ResponseParams
enum ResponseParams {
static let MigrateState = "migrateState"
static let Hash = "sha256";
}

// MARK: - MigrationData
struct MigrationData: Codable, Equatable {
let ct: CTConfig?
let migrationState: String?
let hash: String?

enum CodingKeys: String, CodingKey {
case hash = "sha256"
case migrationState = "sdk"
case ct
}
}

// MARK: - CTConfig
struct CTConfig: Codable, Equatable {
let accountID: String?
let token: String?
let regionCode: String?
let attributeMappings: [String: String]?
let identityKeys: [String]?

enum CodingKeys: String, CodingKey {
case accountID = "accountId"
case token, regionCode, attributeMappings, identityKeys
}
}

// MARK: - Handle Responses
// migrateState = {
// sha256 = 31484a565dcd3e1672922c7c4166bfeee0f500b6d6473fc412091304cc162ca8;
// };
@objc
func handleMigrateState(multiApiResponse: Any) {
guard let migrateState = getValue(dict: multiApiResponse,
key: Constants.MigrateStateResponseParam)
key: ResponseParams.MigrateState)
else { return }

if let hash = getValue(dict: migrateState, key: Constants.HashResponseParam) as? String,
if let hash = getValue(dict: migrateState, key: ResponseParams.Hash) as? String,
hash != self.migrationHash {
Log.debug("[Wrapper] CleverTap Hash changed")
fetchMigrationStateAsync {}
}
}

// response = (
// {
// api = {
// events = "lp+ct";
// profile = "lp+ct";
// };
// ct = {
// accountId = "accId";
// attributeMappings = {
// name1 = "ct-name1";
// };
// regionCode = eu1;
// token = "token";
// };
// eventsUploadStartedTs = "2022-10-02T17:46:01.356Z";
// profileUploadStartedTs = "2022-10-02T17:46:01.356Z";
// reqId = "A285641F-9903-4182-8A10-EB42782CAE69";
// sdk = "lp+ct";
// sha256 = 31484a565dcd3e1672922c7c4166bfeee0f500b6d6473fc412091304cc162ca8;
// state = "EVENTS_UPLOAD_STARTED";
// success = 1;
// "response": [
// {
// "api": {
// "events": "lp+ct",
// "profile": "lp+ct",
// },
// "ct": {
// "accountId": "accId",
// "attributeMappings": {
// "name1": "ct-name1",
// },
// "identityKeys": ["Identity", "Email"],
// "regionCode": "eu1",
// "token": "token",
// },
// "eventsUploadStartedTs": "2022-10-02T17:46:01.356Z",
// "profileUploadStartedTs": "2022-10-02T17:46:01.356Z",
// "reqId": "A285641F-9903-4182-8A10-EB42782CAE69",
// "sdk": "lp+ct",
// "sha256": "31484a565dcd3e1672922c7c4166bfeee0f500b6d6473fc412091304cc162ca8",
// "state": "EVENTS_UPLOAD_STARTED",
// "success": 1,
// }
// );
// ]
func handleGetMigrateState(apiResponse: Any) {
if let ct = getValue(dict: apiResponse, key: Constants.CTResponseParam) {
if let id = getValue(dict: ct, key: Constants.AccountIdResponseParam) as? String {
guard let migrationData = parseResponse(apiResponse: apiResponse) else {
return
}

if let ct = migrationData.ct {
if let id = ct.accountID {
accountId = id
}
if let token = getValue(dict: ct, key: Constants.AccountTokenResponseParam) as? String {
if let token = ct.token {
accountToken = token
}
if let region = getValue(dict: ct, key: Constants.RegionCodeResponseParam) as? String {
if let region = ct.regionCode {
regionCode = region
}
if let mappings = getValue(dict: ct, key: Constants.AttributeMappingsResponseParam) as? [String: String] {
if let mappings = ct.attributeMappings {
attributeMappings = mappings
}
if let keys = ct.identityKeys, keys.count > 0 {
identityKeys = keys
}
}

if let sdk = getValue(dict: apiResponse, key: Constants.SdkResponseParam) as? String {
if let sdk = migrationData.migrationState {
migrationState = MigrationState(stringValue: sdk)
}
if let hash = getValue(dict: apiResponse, key: Constants.HashResponseParam) as? String {
if let hash = migrationData.hash {
migrationHash = hash
}
}

// MARK: - Utils
@nonobjc internal func parseResponse(apiResponse: Any) -> MigrationData? {
if let dict = apiResponse as? [String: Any] {
do {
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
return try JSONDecoder().decode(MigrationData.self, from: jsonData)
} catch {
Log.error("[Wrapper] Error parsing getMigrateState response: \(error.localizedDescription), error: \(String(describing: error))")
}
}
return nil
}

private func getValue(dict: Any, key: String) -> Any? {
guard let dict = dict as? [String: Any] else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LeanplumSDK
//
// Created by Nikola Zagorchev on 13.07.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation

Expand Down Expand Up @@ -33,6 +33,9 @@ import Foundation
@PropUserDefaults(key: Constants.AttributeMappingsKey, defaultValue: [:])
var attributeMappings: [String: String]

@PropUserDefaults(key: Constants.IdentityKeysKey, defaultValue: Constants.DefaultIdentityKeys)
var identityKeys: [String]

@MigrationStateUserDefaults(key: Constants.MigrationStateKey, defaultValue: .undefined)
var migrationState: MigrationState {
didSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LeanplumSDK
//
// Created by Nikola Zagorchev on 9.07.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation
// Use @_implementationOnly to *not* expose CleverTapSDK to the Leanplum-Swift header
Expand Down Expand Up @@ -53,6 +53,7 @@ class CTWrapper: Wrapper {

func launch() {
let config = CleverTapInstanceConfig.init(accountId: accountId, accountToken: accountToken, accountRegion: accountRegion)
config.identityKeys = MigrationManager.shared.identityKeys
config.useCustomCleverTapId = true
config.logLevel = CleverTapLogLevel(LPLogManager.logLevel())
cleverTapInstance = CleverTap.instance(with: config, andCleverTapID: identityManager.cleverTapID)
Expand Down
22 changes: 22 additions & 0 deletions LeanplumSDKApp/LeanplumSDKApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@
6A9D0A9A273430A700466133 /* NotificationTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A9D0A99273430A700466133 /* NotificationTestHelper.swift */; };
6A9DECA527B6ABFB00052807 /* change_host_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 6A9DECA427B69D8800052807 /* change_host_response.json */; };
6A9DECA627B6ABFB00052807 /* change_host_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 6A9DECA427B69D8800052807 /* change_host_response.json */; };
6AA3505B29953CFF008677C1 /* get_migrate_state_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AA3505529953CEF008677C1 /* get_migrate_state_response.json */; };
6AA3505C29953D00008677C1 /* get_migrate_state_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AA3505529953CEF008677C1 /* get_migrate_state_response.json */; };
6AA3505E29955FD4008677C1 /* get_migrate_state_response_error.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AA3505D29955FD4008677C1 /* get_migrate_state_response_error.json */; };
6AA3505F29955FD4008677C1 /* get_migrate_state_response_error.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AA3505D29955FD4008677C1 /* get_migrate_state_response_error.json */; };
6AA3506129956040008677C1 /* get_migrate_state_response_missing.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AA3506029956040008677C1 /* get_migrate_state_response_missing.json */; };
6AA3506229956040008677C1 /* get_migrate_state_response_missing.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AA3506029956040008677C1 /* get_migrate_state_response_missing.json */; };
6AA3506429956AEC008677C1 /* MigrationResponsesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AA3506329956AEC008677C1 /* MigrationResponsesTest.swift */; };
6AF543A228E883AD0025F2A9 /* LeanplumLocationAndBeacons.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6AF5439728E882CA0025F2A9 /* LeanplumLocationAndBeacons.framework */; };
6AF6426D298198160021A997 /* Leanplum.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6AA13C7B2900546400EDCA69 /* Leanplum.framework */; };
6AF6426E298198160021A997 /* Leanplum.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 6AA13C7B2900546400EDCA69 /* Leanplum.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -355,6 +362,10 @@
6A9D0A99273430A700466133 /* NotificationTestHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationTestHelper.swift; sourceTree = "<group>"; };
6A9DECA427B69D8800052807 /* change_host_response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = change_host_response.json; sourceTree = "<group>"; };
6AA13C742900546400EDCA69 /* LeanplumSDK.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = LeanplumSDK.xcodeproj; path = ../LeanplumSDK/LeanplumSDK.xcodeproj; sourceTree = "<group>"; };
6AA3505529953CEF008677C1 /* get_migrate_state_response.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = get_migrate_state_response.json; sourceTree = "<group>"; };
6AA3505D29955FD4008677C1 /* get_migrate_state_response_error.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = get_migrate_state_response_error.json; sourceTree = "<group>"; };
6AA3506029956040008677C1 /* get_migrate_state_response_missing.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = get_migrate_state_response_missing.json; sourceTree = "<group>"; };
6AA3506329956AEC008677C1 /* MigrationResponsesTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MigrationResponsesTest.swift; sourceTree = "<group>"; };
6AEAEE592795B68700680F84 /* Leanplum.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Leanplum.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6AEAEE612795B72A00680F84 /* LeanplumLocationAndBeacons.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = LeanplumLocationAndBeacons.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6AF5438D28E882CA0025F2A9 /* LeanplumSDKLocation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = LeanplumSDKLocation.xcodeproj; path = ../LeanplumSDKLocation/LeanplumSDKLocation.xcodeproj; sourceTree = "<group>"; };
Expand Down Expand Up @@ -589,6 +600,9 @@
075AB1342684AB9D007CA1BD /* variants_response.json */,
075AB13A2684AB9D007CA1BD /* Malformed */,
6A9DECA427B69D8800052807 /* change_host_response.json */,
6AA3505529953CEF008677C1 /* get_migrate_state_response.json */,
6AA3505D29955FD4008677C1 /* get_migrate_state_response_error.json */,
6AA3506029956040008677C1 /* get_migrate_state_response_missing.json */,
);
path = Responses;
sourceTree = "<group>";
Expand Down Expand Up @@ -658,6 +672,7 @@
6A37A8A928F0078500F4339F /* Migration+Extensions.swift */,
6A37A8AB28F00B5800F4339F /* IdentityManagerTest.swift */,
6A37A8AD28F0164700F4339F /* IdentityManagerMocks.swift */,
6AA3506329956AEC008677C1 /* MigrationResponsesTest.swift */,
);
path = Migration;
sourceTree = "<group>";
Expand Down Expand Up @@ -856,13 +871,16 @@
07828DB2268B4A630029A339 /* local_caps_week_response.json in Resources */,
07828DB5268B4A630029A339 /* local_caps_session_response.json in Resources */,
075AACF726847BF4007CA1BD /* LaunchScreen.storyboard in Resources */,
6AA3505E29955FD4008677C1 /* get_migrate_state_response_error.json in Resources */,
075AB1842684AB9D007CA1BD /* TiedPriorities2.json in Resources */,
07828DB4268B4A630029A339 /* local_caps_day_response.json in Resources */,
07828DB3268B4A630029A339 /* local_caps_response.json in Resources */,
075AB1862684AB9D007CA1BD /* TiedPrioritiesDifferentDelay.json in Resources */,
075AB15E2684AB9D007CA1BD /* action_response.json in Resources */,
075AB16C2684AB9D007CA1BD /* state_response.json in Resources */,
6AA3506129956040008677C1 /* get_migrate_state_response_missing.json in Resources */,
075AB0962684A1A4007CA1BD /* Leanplum-Info.plist in Resources */,
6AA3505B29953CFF008677C1 /* get_migrate_state_response.json in Resources */,
075AB1622684AB9D007CA1BD /* start_variables_response.json in Resources */,
075AB1702684AB9D007CA1BD /* batch_response.json in Resources */,
075AB1602684AB9D007CA1BD /* track_event_response.json in Resources */,
Expand Down Expand Up @@ -924,15 +942,18 @@
075AB1672684AB9D007CA1BD /* secured_vars_no_sign_response.json in Resources */,
075AB17F2684AB9D007CA1BD /* DifferentPriorities2.json in Resources */,
075AB1772684AB9D007CA1BD /* variables_response.json in Resources */,
6AA3506229956040008677C1 /* get_migrate_state_response_missing.json in Resources */,
075AB1572684AB9D007CA1BD /* test.pdf in Resources */,
075AB1792684AB9D007CA1BD /* secured_vars_response.json in Resources */,
075AB1892684AB9D007CA1BD /* TiedPriorities1.json in Resources */,
07828DAE268B4A5E0029A339 /* local_caps_day_response.json in Resources */,
075AB18B2684AB9D007CA1BD /* DifferentPrioritiesWithMissingValues.json in Resources */,
075AB1712684AB9D007CA1BD /* batch_response.json in Resources */,
6AA3505F29955FD4008677C1 /* get_migrate_state_response_error.json in Resources */,
075AB1692684AB9D007CA1BD /* malformed_track_event_response.json in Resources */,
075AB1592684AB9D007CA1BD /* test.file in Resources */,
075AB1872684AB9D007CA1BD /* TiedPrioritiesDifferentDelay.json in Resources */,
6AA3505C29953D00008677C1 /* get_migrate_state_response.json in Resources */,
075AB1652684AB9D007CA1BD /* start_with_variant_debug_info_response.json in Resources */,
075AB16F2684AB9D007CA1BD /* simple_start_response.json in Resources */,
);
Expand Down Expand Up @@ -1080,6 +1101,7 @@
075AB1172684AAD9007CA1BD /* LPRequestTest.m in Sources */,
6A9D0A9827342EE300466133 /* NotificationsManagerMock.swift in Sources */,
075AB1282684AAD9007CA1BD /* LPRequestFactory+Extension.m in Sources */,
6AA3506429956AEC008677C1 /* MigrationResponsesTest.swift in Sources */,
39C081A8278D95ED00C1DBD6 /* ActionManagerTest.swift in Sources */,
6A07FDA52811AF3800995BE3 /* ContentMergerTest.swift in Sources */,
075AB12A2684AAD9007CA1BD /* LPRequestSenderTimerTest.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//
// WrapperTest.swift
// CTWrapperTest.swift
// LeanplumSDKTests
//
// Created by Nikola Zagorchev on 5.10.22.
//
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation
import XCTest
@testable import Leanplum

class WrapperTest: XCTestCase {
class CTWrapperTest: XCTestCase {

static let attributeMappings = ["lpName": "ctName", "lpName2": "ctName2"]
let wrapper = CTWrapper(accountId: "", accountToken: "", accountRegion: "", userId: "", deviceId: "", callbacks: [])
Expand Down Expand Up @@ -118,4 +118,8 @@ class WrapperTest: XCTestCase {
XCTAssertTrue(actual.isEqual(expected))
XCTAssertTrue(actualWithNil.isEqual(expected))
}

func testIdentityDefaultValue() {
XCTAssertEqual(MigrationManager.shared.identityKeys, ["Identity"])
}
}
Loading