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

DIA-4457 use mobile-core metadata #580

Merged
merged 3 commits into from
Sep 10, 2024
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
1 change: 1 addition & 0 deletions ConsentViewController.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Pod::Spec.new do |s|
s.source = { :git => 'https://github.com/SourcePointUSA/ios-cmp-app.git', :tag => s.version.to_s }
s.swift_version = '5.1'
s.source_files = 'ConsentViewController/Classes/**/*'
s.dependency 'SPMobileCore', '0.0.3'
s.ios.deployment_target = '10.0'
s.ios.exclude_files = 'ConsentViewController/Classes/Views/tvOS'
s.tvos.deployment_target = '12.0'
Expand Down
1 change: 1 addition & 0 deletions ConsentViewController/Classes/SPConsentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import UIKit
let client = SourcePointClient(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: campaigns.environment,
timeout: Self.DefaultTimeout
)
Expand Down
2 changes: 1 addition & 1 deletion ConsentViewController/Classes/SPDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct SPDate: Codable, Equatable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
originalDateString = try container.decode(String.self)
date = Self.format.date(from: originalDateString) ?? Date()
date = Self.format.date(from: originalDateString) ?? Date() // TODO: potentially throw an error here...
}

static func now() -> SPDate {
Expand Down
56 changes: 56 additions & 0 deletions ConsentViewController/Classes/SourcePointClient/Adapters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Adapters.swift
// Pods
//
// Created by Andre Herculano on 10/9/24.
//

import Foundation
import SPMobileCore

// swiftlint:disable force_unwrapping

extension SPDate {
init(string: String) {
originalDateString = string
date = Self.format.date(from: originalDateString) ?? Date()
}
}

extension MetaDataQueryParam {
func toCore() -> MetaDataRequest.Campaigns {
.init(
gdpr: gdpr != nil ? .init(groupPmId: gdpr?.groupPmId) : nil,
usnat: usnat != nil ? .init(groupPmId: usnat?.groupPmId) : nil,
ccpa: ccpa != nil ? .init(groupPmId: ccpa?.groupPmId) : nil
)
}
}

extension SPMobileCore.MetaDataResponse {
func toNative() -> MetaDataResponse {
.init(
ccpa: ccpa != nil ? .init(
applies: ccpa!.applies,
sampleRate: ccpa!.sampleRate
) : nil,
gdpr: gdpr != nil ? .init(
additionsChangeDate: SPDate(string: gdpr!.additionsChangeDate),
legalBasisChangeDate: SPDate(string: gdpr!.legalBasisChangeDate),
vendorListId: gdpr!.vendorListId,
childPmId: gdpr!.childPmId,
applies: gdpr!.applies,
sampleRate: gdpr!.sampleRate
) : nil,
usnat: usnat != nil ? .init(
vendorListId: usnat!.vendorListId,
additionsChangeDate: SPDate(string: usnat!.additionsChangeDate),
applies: usnat!.applies,
sampleRate: usnat!.sampleRate,
applicableSections: usnat!.applicableSections.map { Int(truncating: $0) }
) : nil
)
}
}

// swiftlint:enable force_unwrapping
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

// swiftlint:disable function_parameter_count file_length
import Foundation
import SPMobileCore

typealias CoreClient = SPMobileCore.SourcepointClient

extension Result where Success == Data? {
func decoded<T: Decodable>(
Expand Down Expand Up @@ -49,7 +52,13 @@ typealias MetaDataHandler = (Result<MetaDataResponse, SPError>) -> Void
typealias ChoiceHandler = (Result<ChoiceAllResponse, SPError>) -> Void

protocol SourcePointProtocol {
init(accountId: Int, propertyName: SPPropertyName, campaignEnv: SPCampaignEnv, timeout: TimeInterval)
init(
accountId: Int,
propertyName: SPPropertyName,
propertyId: Int,
campaignEnv: SPCampaignEnv,
timeout: TimeInterval
)

func getMessages(_ params: MessagesRequest, handler: @escaping MessagesHandler)

Expand Down Expand Up @@ -170,23 +179,44 @@ A Http client for SourcePoint's endpoints
*/
class SourcePointClient: SourcePointProtocol {
let accountId: Int
let propertyId: Int
let propertyName: SPPropertyName
let campaignEnv: SPCampaignEnv
var client: HttpClient
let coreClient: CoreClient

let requestUUID = UUID()

init(accountId: Int, propertyName: SPPropertyName, campaignEnv: SPCampaignEnv, client: HttpClient) {
init(
accountId: Int,
propertyName: SPPropertyName,
propertyId: Int,
campaignEnv: SPCampaignEnv,
client: HttpClient
) {
self.accountId = accountId
self.propertyName = propertyName
self.propertyId = propertyId
self.campaignEnv = campaignEnv
self.client = client
self.coreClient = CoreClient(
accountId: Int32(accountId),
propertyId: Int32(propertyId),
propertyName: propertyName.rawValue
)
}

required convenience init(accountId: Int, propertyName: SPPropertyName, campaignEnv: SPCampaignEnv, timeout: TimeInterval) {
required convenience init(
accountId: Int,
propertyName: SPPropertyName,
propertyId: Int,
campaignEnv: SPCampaignEnv,
timeout: TimeInterval
) {
self.init(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: campaignEnv,
client: SimpleClient(timeoutAfter: timeout))
}
Expand Down Expand Up @@ -445,7 +475,7 @@ extension SourcePointClient {
return url
}

func metaData(
func fallbackMetaData(
accountId: Int,
propertyId: Int,
metadata: MetaDataQueryParam,
Expand All @@ -465,6 +495,26 @@ extension SourcePointClient {
}
}

func metaData(
accountId: Int,
propertyId: Int,
metadata: MetaDataQueryParam,
handler: @escaping MetaDataHandler
) {
coreClient.getMetaData(campaigns: metadata.toCore()) { response, error in
if error != nil || response == nil {
self.fallbackMetaData(
accountId: accountId,
propertyId: propertyId,
metadata: metadata,
handler: handler
)
} else {
handler(Result.success(response!.toNative())) // swiftlint:disable:this force_unwrapping
}
}
}

func getMessages(_ params: MessagesRequest, handler: @escaping MessagesHandler) {
guard let url = Constants.Urls.GET_MESSAGES_URL.appendQueryItems(params.stringifiedParams())
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ class SourcepointClientCoordinator: SPClientCoordinator {
self.spClient = spClient ?? SourcePointClient(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: campaigns.environment,
timeout: SPConsentManager.DefaultTimeout
)
Expand Down
2 changes: 2 additions & 0 deletions Example/ConsentViewController.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@
80A5A481B97C11AC27421C73 /* Pods_ObjC_ExampleApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ObjC_ExampleApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
815ACB69209144D8CA0F5833 /* Pods-SourcepointFirebaseDemo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SourcepointFirebaseDemo.debug.xcconfig"; path = "Target Support Files/Pods-SourcepointFirebaseDemo/Pods-SourcepointFirebaseDemo.debug.xcconfig"; sourceTree = "<group>"; };
816F9B5ABCD689D81DDBAC41 /* Pods-SourcepointFirebaseDemoUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SourcepointFirebaseDemoUITests.release.xcconfig"; path = "Target Support Files/Pods-SourcepointFirebaseDemoUITests/Pods-SourcepointFirebaseDemoUITests.release.xcconfig"; sourceTree = "<group>"; };
8203A3BE2C904C3E00BACFE7 /* SPMobileCore.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = SPMobileCore.xcframework; path = "../../mobile-core/SPMobileCore.xcframework"; sourceTree = "<group>"; };
820B08AD2B602177004E77E8 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
820B08AF2B60217E004E77E8 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/System/iOSSupport/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
820B08B12B6021A6004E77E8 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS17.2.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
Expand Down Expand Up @@ -2210,6 +2211,7 @@
EEE3CDAB8AFE80647B1E565B /* Frameworks */ = {
isa = PBXGroup;
children = (
8203A3BE2C904C3E00BACFE7 /* SPMobileCore.xcframework */,
8216E2632BB32B74006FEF11 /* ConsentViewController.framework */,
820B08B12B6021A6004E77E8 /* Foundation.framework */,
820B08AF2B60217E004E77E8 /* UIKit.framework */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ class SourcePointClientMock: SourcePointProtocol {
nonKeyedLocalState: SPJson()
)

required init(accountId: Int, propertyName: SPPropertyName, campaignEnv: SPCampaignEnv, timeout: TimeInterval) {
}
required init(
accountId: Int,
propertyName: SPPropertyName,
propertyId: Int,
campaignEnv: SPCampaignEnv,
timeout: TimeInterval
) {}

convenience init(
accountId: Int = 0,
propertyName: SPPropertyName = try! SPPropertyName(""),
propertyId: Int = 0,
campaignEnv: SPCampaignEnv = .Public,
timout: TimeInterval = 1.0
) {
self.init(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: campaignEnv,
timeout: timout
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class SPClientCoordinatorSpec: QuickSpec {
spClientMock = SourcePointClientMock(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: .Public,
timeout: 999
)
Expand Down Expand Up @@ -174,6 +175,7 @@ class SPClientCoordinatorSpec: QuickSpec {
spClientMock = SourcePointClientMock(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: .Public,
timeout: 999
)
Expand Down Expand Up @@ -765,6 +767,7 @@ class SPClientCoordinatorSpec: QuickSpec {
spClientMock = SourcePointClientMock(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: .Public,
timeout: 999
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SourcePointClientSpec: QuickSpec {
func getClient(_ client: MockHttp) -> SourcePointClient { SourcePointClient(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: .Public,
client: client
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UnmockedSourcepointClientSpec: QuickSpec {
client = SourcePointClient(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: .Public,
client: SimpleClient(timeoutAfter: TimeInterval(10))
)
Expand Down Expand Up @@ -235,16 +236,22 @@ class UnmockedSourcepointClientSpec: QuickSpec {
describe("for a property belonging to a property group") {
describe("and with a valid groupPmId") {
it("meta-data returns a childPmId") {
let propertyIdBelongingToAPropertyGroup = 24188
let groupPmId = "613056"
let childPmId = "613057"
client = SourcePointClient(
accountId: accountId,
propertyName: propertyName,
propertyId: 24188,
campaignEnv: .Public,
client: SimpleClient(timeoutAfter: TimeInterval(10))
)
waitUntil { done in
client.metaData(
accountId: accountId,
propertyId: propertyIdBelongingToAPropertyGroup,
accountId: 99,
propertyId: 99,
metadata: MetaDataQueryParam(
gdpr: .init(groupPmId: groupPmId),
ccpa: .init(groupPmId: nil),
ccpa: nil,
usnat: nil
)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CoordinatorMock: SPClientCoordinator {
var spClient: SourcePointProtocol = SourcePointClientMock(
accountId: 0,
propertyName: try! SPPropertyName(""),
propertyId: 0,
campaignEnv: .Public,
timeout: 10
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SPConsentManagerSpec: QuickSpec {
spClient: SourcePointClientMock(
accountId: accountId,
propertyName: propertyName,
propertyId: propertyId,
campaignEnv: .Public,
timeout: 999
),
Expand Down
6 changes: 5 additions & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PODS:
- ConsentViewController (7.7.1):
- Down (~> 0.11.0)
- SPMobileCore (= 0.0.3)
- Down (0.11.0)
- FirebaseAnalytics (10.23.0):
- FirebaseAnalytics/AdIdSupport (= 10.23.0)
Expand Down Expand Up @@ -89,6 +90,7 @@ PODS:
- Nimble (10.0.0)
- PromisesObjC (2.4.0)
- Quick (5.0.1)
- SPMobileCore (0.0.3)
- SwiftLint (0.51.0)
- Wormholy (1.6.6)

Expand Down Expand Up @@ -117,6 +119,7 @@ SPEC REPOS:
- Nimble
- PromisesObjC
- Quick
- SPMobileCore
- SwiftLint
- Wormholy

Expand All @@ -125,7 +128,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ConsentViewController: 43068f943fe21d8990a44dabfd4da41170ec7c8b
ConsentViewController: 13d8ce7370f1af6708e49f7e5f1b5aac01f21f6e
Down: b6ba1bc985c9d2f4e15e3b293d2207766fa12612
FirebaseAnalytics: 45f6e2e5ef8ccbb90be73ae983c3b20fa78837f7
FirebaseCore: 63efb128decaebb04c4033ed4e05fb0bb1cccef1
Expand All @@ -139,6 +142,7 @@ SPEC CHECKSUMS:
Nimble: 5316ef81a170ce87baf72dd961f22f89a602ff84
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
Quick: 749aa754fd1e7d984f2000fe051e18a3a9809179
SPMobileCore: 47632f68a87b5df421750db6ba987768c5c36011
SwiftLint: 1b7561918a19e23bfed960e40759086e70f4dba5
Wormholy: 09da0b876f9276031fd47383627cb75e194fc068

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading