Skip to content

refact(odp): Allows support for ODP testing using FSC. #476

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

Merged
merged 2 commits into from
Feb 8, 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
4 changes: 3 additions & 1 deletion Sources/Extensions/OptimizelyClient+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019-2021, Optimizely, Inc. and contributors
// Copyright 2019-2021,2023 Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,6 +62,7 @@ extension OptimizelyClient {
eventDispatcher: OPTEventDispatcher? = nil,
datafileHandler: OPTDatafileHandler? = nil,
userProfileService: OPTUserProfileService? = nil,
odpManager: OdpManager? = nil,
periodicDownloadInterval: Int?,
defaultLogLevel: OptimizelyLogLevel? = nil,
defaultDecideOptions: [OptimizelyDecideOption]? = nil,
Expand All @@ -72,6 +73,7 @@ extension OptimizelyClient {
eventDispatcher: eventDispatcher,
datafileHandler: datafileHandler,
userProfileService: userProfileService,
odpManager: odpManager,
defaultLogLevel: defaultLogLevel,
defaultDecideOptions: defaultDecideOptions,
settings: settings)
Expand Down
10 changes: 5 additions & 5 deletions Sources/ODP/OdpEvent.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022, Optimizely, Inc. and contributors
// Copyright 2022-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@

import Foundation

struct OdpEvent: Codable {
public struct OdpEvent: Codable {
let type: String
let action: String
let identifiers: [String: String]
Expand All @@ -25,7 +25,7 @@ struct OdpEvent: Codable {
let data: [String: Any?]
let dataSerial: Data

init(type: String, action: String, identifiers: [String: String], data: [String: Any?]) {
public init(type: String, action: String, identifiers: [String: String], data: [String: Any?]) {
self.type = type
self.action = action
self.identifiers = identifiers
Expand All @@ -44,7 +44,7 @@ struct OdpEvent: Codable {
case dataSerial
}

init(from decoder: Decoder) throws {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

self.type = try values.decode(String.self, forKey: .type)
Expand All @@ -57,7 +57,7 @@ struct OdpEvent: Codable {

// For JSON encoding (POST request body)

var dict: [String: Any] {
public var dict: [String: Any] {
return [
"type": type,
"action": action,
Expand Down
6 changes: 3 additions & 3 deletions Sources/ODP/OdpEventApiManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022, Optimizely, Inc. and contributors
// Copyright 2022-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,13 +30,13 @@ import Foundation
{"title":"Accepted","status":202,"timestamp":"2022-06-30T20:59:52.046Z"}
*/

class OdpEventApiManager {
public class OdpEventApiManager {
let resourceTimeoutInSecs: Int?

/// OdpEventApiManager init
/// - Parameters:
/// - timeout: timeout for segment fetch
init(timeout: Int? = nil) {
public init(timeout: Int? = nil) {
self.resourceTimeoutInSecs = timeout
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/ODP/OdpEventManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022-2023, Optimizely, Inc. and contributors
// Copyright 2022-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
import Foundation
import UIKit

class OdpEventManager {
open class OdpEventManager {
var odpConfig = OdpConfig()
var apiMgr: OdpEventApiManager

Expand All @@ -33,9 +33,9 @@ class OdpEventManager {
/// - sdkKey: datafile sdkKey
/// - apiManager: OdpEventApiManager
/// - resourceTimeoutInSecs: timeout for event dispatch
init(sdkKey: String,
apiManager: OdpEventApiManager? = nil,
resourceTimeoutInSecs: Int? = nil) {
public init(sdkKey: String,
apiManager: OdpEventApiManager? = nil,
resourceTimeoutInSecs: Int? = nil) {
self.apiMgr = apiManager ?? OdpEventApiManager(timeout: resourceTimeoutInSecs)

self.queueLock = DispatchQueue(label: "event")
Expand Down Expand Up @@ -101,7 +101,7 @@ class OdpEventManager {

// MARK: - dispatch

func dispatch(_ event: OdpEvent) {
open func dispatch(_ event: OdpEvent) {
if eventQueue.count < maxQueueSize {
eventQueue.save(item: event)
} else {
Expand Down
18 changes: 9 additions & 9 deletions Sources/ODP/OdpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import Foundation

class OdpManager {
public class OdpManager {
var enabled: Bool
var vuidManager: OdpVuidManager

Expand All @@ -40,14 +40,14 @@ class OdpManager {
/// - timeoutForEventDispatchInSecs: timeout for event dispatch
/// - segmentManager: ODPSegmentManager
/// - eventManager: ODPEventManager
init(sdkKey: String,
disable: Bool,
cacheSize: Int,
cacheTimeoutInSecs: Int,
timeoutForSegmentFetchInSecs: Int? = nil,
timeoutForEventDispatchInSecs: Int? = nil,
segmentManager: OdpSegmentManager? = nil,
eventManager: OdpEventManager? = nil) {
public init(sdkKey: String,
disable: Bool,
cacheSize: Int,
cacheTimeoutInSecs: Int,
timeoutForSegmentFetchInSecs: Int? = nil,
timeoutForEventDispatchInSecs: Int? = nil,
segmentManager: OdpSegmentManager? = nil,
eventManager: OdpEventManager? = nil) {

self.enabled = !disable
self.vuidManager = OdpVuidManager.shared
Expand Down
18 changes: 9 additions & 9 deletions Sources/ODP/OdpSegmentApiManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022, Optimizely, Inc. and contributors
// Copyright 2022-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -95,23 +95,23 @@ import Foundation
}
*/

class OdpSegmentApiManager {
open class OdpSegmentApiManager {
let logger = OPTLoggerFactory.getLogger()
let resourceTimeoutInSecs: Int?

/// OdpSegmentApiManager init
/// - Parameters:
/// - timeout: timeout for segment fetch
init(timeout: Int? = nil) {
public init(timeout: Int? = nil) {
self.resourceTimeoutInSecs = timeout
}

func fetchSegments(apiKey: String,
apiHost: String,
userKey: String,
userValue: String,
segmentsToCheck: [String],
completionHandler: @escaping ([String]?, OptimizelyError?) -> Void) {
open func fetchSegments(apiKey: String,
apiHost: String,
userKey: String,
userValue: String,
segmentsToCheck: [String],
completionHandler: @escaping ([String]?, OptimizelyError?) -> Void) {

let query = makeQuery(userKey: userKey, userValue: userValue, segmentsToCheck: segmentsToCheck)
guard let httpBody = try? JSONSerialization.data(withJSONObject: query) else {
Expand Down
11 changes: 6 additions & 5 deletions Sources/ODP/OdpSegmentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import Foundation

class OdpSegmentManager {
public class OdpSegmentManager {
var odpConfig = OdpConfig()
var segmentsCache: LruCache<String, [String]>
var apiMgr: OdpSegmentApiManager
Expand All @@ -29,10 +29,11 @@ class OdpSegmentManager {
/// - cacheTimeoutInSecs: segment cache timeout
/// - apiManager: OdpSegmentApiManager
/// - resourceTimeoutInSecs: timeout for segment fetch
init(cacheSize: Int,
cacheTimeoutInSecs: Int,
apiManager: OdpSegmentApiManager? = nil,
resourceTimeoutInSecs: Int? = nil) {
public init(cacheSize: Int,
cacheTimeoutInSecs: Int,
apiManager: OdpSegmentApiManager? = nil,
resourceTimeoutInSecs: Int? = nil) {
self.odpConfig = odpConfig ?? OdpConfig()
self.apiMgr = apiManager ?? OdpSegmentApiManager(timeout: resourceTimeoutInSecs)

self.segmentsCache = LruCache<String, [String]>(size: cacheSize,
Expand Down
20 changes: 10 additions & 10 deletions Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019-2022, Optimizely, Inc. and contributors
// Copyright 2019-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ open class OptimizelyClient: NSObject {

var decisionService: OPTDecisionService!
public var notificationCenter: OPTNotificationCenter?
var odpManager: OdpManager
var odpManager: OdpManager!
let sdkSettings: OptimizelySdkSettings

// MARK: - Public interfaces
Expand All @@ -80,23 +80,23 @@ open class OptimizelyClient: NSObject {
eventDispatcher: OPTEventDispatcher? = nil,
datafileHandler: OPTDatafileHandler? = nil,
userProfileService: OPTUserProfileService? = nil,
odpManager: OdpManager? = nil,
defaultLogLevel: OptimizelyLogLevel? = nil,
defaultDecideOptions: [OptimizelyDecideOption]? = nil,
settings: OptimizelySdkSettings? = nil) {

self.sdkKey = sdkKey
self.sdkSettings = settings ?? OptimizelySdkSettings()
self.defaultDecideOptions = defaultDecideOptions ?? []

self.odpManager = OdpManager(sdkKey: sdkKey,
disable: sdkSettings.disableOdp,
cacheSize: sdkSettings.segmentsCacheSize,
cacheTimeoutInSecs: sdkSettings.segmentsCacheTimeoutInSecs,
timeoutForSegmentFetchInSecs: sdkSettings.timeoutForSegmentFetchInSecs,
timeoutForEventDispatchInSecs: sdkSettings.timeoutForOdpEventInSecs)


super.init()

self.odpManager = odpManager ?? OdpManager(sdkKey: sdkKey,
disable: sdkSettings.disableOdp,
cacheSize: sdkSettings.segmentsCacheSize,
cacheTimeoutInSecs: sdkSettings.segmentsCacheTimeoutInSecs,
timeoutForSegmentFetchInSecs: sdkSettings.timeoutForSegmentFetchInSecs,
timeoutForEventDispatchInSecs: sdkSettings.timeoutForOdpEventInSecs)
let userProfileService = userProfileService ?? DefaultUserProfileService()
let logger = logger ?? DefaultLogger()
type(of: logger).logLevel = defaultLogLevel ?? .info
Expand Down
4 changes: 3 additions & 1 deletion Tests/OptimizelyTests-Common/DecisionListenerTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019-2021, Optimizely, Inc. and contributors
// Copyright 2019-2021,2023 Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1212,6 +1212,7 @@ class FakeManager: OptimizelyClient {
eventDispatcher: OPTEventDispatcher? = nil,
datafileHandler: OPTDatafileHandler? = nil,
userProfileService: OPTUserProfileService? = nil,
odpManager: OdpManager? = nil,
defaultLogLevel: OptimizelyLogLevel? = nil,
defaultDecideOptions: [OptimizelyDecideOption]? = nil,
settings: OptimizelySdkSettings? = nil) {
Expand All @@ -1224,6 +1225,7 @@ class FakeManager: OptimizelyClient {
eventDispatcher: eventDispatcher,
datafileHandler: datafileHandler,
userProfileService: userProfileService,
odpManager: odpManager,
defaultLogLevel: defaultLogLevel,
defaultDecideOptions: defaultDecideOptions,
settings: settings)
Expand Down