Skip to content

Commit

Permalink
rename zaius to apiManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeopt committed Oct 7, 2022
1 parent bb7ed98 commit b0d7fcd
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 177 deletions.
5 changes: 3 additions & 2 deletions DemoSwiftApp/Samples/SamplesForAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ class SamplesForAPI {

static func checkAudienceSegments(optimizely: OptimizelyClient) {
// override the default handler if cache size and timeout need to be customized
let optimizely = OptimizelyClient(sdkKey: "FCnSegiEkRry9rhVMroit4",
periodicDownloadInterval: 60)
let optimizely = OptimizelyClient(sdkKey: "VivZyCGPHY369D4z8T9yG", // odp-test
periodicDownloadInterval: 60,
defaultLogLevel: .debug)
optimizely.start { result in
if case .failure(let error) = result {
print("[AudienceSegments] SDK initialization failed: \(error)")
Expand Down
162 changes: 81 additions & 81 deletions OptimizelySwiftSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Foundation
{"title":"Accepted","status":202,"timestamp":"2022-06-30T20:59:52.046Z"}
*/

class ZaiusRestApiManager {
class OdpEventApiManager {

func sendOdpEvents(apiKey: String,
apiHost: String,
Expand Down
36 changes: 18 additions & 18 deletions Sources/ODP/OdpEventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import UIKit

class OdpEventManager {
var odpConfig: OdpConfig
var zaiusMgr: ZaiusRestApiManager
var apiMgr: OdpEventApiManager

var maxQueueSize = 100
let maxBatchEvents = 10
let queueLock: DispatchQueue
let eventQueue: DataStoreQueueStackImpl<OdpEvent>

let logger = OPTLoggerFactory.getLogger()

init(sdkKey: String, odpConfig: OdpConfig? = nil, apiManager: ZaiusRestApiManager? = nil) {
init(sdkKey: String, odpConfig: OdpConfig? = nil, apiManager: OdpEventApiManager? = nil) {
self.odpConfig = odpConfig ?? OdpConfig()
self.zaiusMgr = apiManager ?? ZaiusRestApiManager()
self.apiMgr = apiManager ?? OdpEventApiManager()

self.queueLock = DispatchQueue(label: "event")

Expand Down Expand Up @@ -60,7 +60,7 @@ class OdpEventManager {
],
data: [:])
}

func sendEvent(type: String, action: String, identifiers: [String: String], data: [String: Any?]) {
let event = OdpEvent(type: type,
action: action,
Expand All @@ -82,15 +82,15 @@ class OdpEventManager {
"os_version": Utils.osVersion, // "13.2", ...
"device_type": Utils.deviceType, // fixed set = ("Phone", "Tablet", "Smart TV", "Watch", “PC”, "Other")
"model": Utils.deviceModel // ("iPhone 12", "iPad 2", "Pixel 2", "SM-A515F", ...)

// [optional]
// "data_source_instance": <sub>, // if need subtypes of data_source
]

data.merge(customData) { (_, custom) in custom } // keep custom data if conflicts
return data
}

// MARK: - dispatch

func dispatch(_ event: OdpEvent) {
Expand All @@ -100,7 +100,7 @@ class OdpEventManager {
let error = OptimizelyError.eventDispatchFailed("ODP EventQueue is full")
self.logger.e(error)
}

flush()
}

Expand All @@ -110,11 +110,11 @@ class OdpEventManager {
reset()
return
}

guard let odpApiKey = odpConfig.apiKey, let odpApiHost = odpConfig.apiHost else {
return
}

queueLock.async {
func removeStoredEvents(num: Int) {
if let removedItem = self.eventQueue.removeFirstItems(count: num), removedItem.count > 0 {
Expand All @@ -128,20 +128,20 @@ class OdpEventManager {
// sync group used to ensure that the sendEvent is synchronous.
// used in flushEvents
let sync = DispatchGroup()

while let events: [OdpEvent] = self.eventQueue.getFirstItems(count: self.maxBatchEvents) {
let numEvents = events.count

// multiple auto-retries are disabled for now
// - this may be too much since they'll be retried any way when next events arrive.
// - also, no guarantee on success after multiple retries, so it helps minimal with extra complexity.

var odpError: OptimizelyError?

sync.enter() // make the send event synchronous. enter our notify
self.zaiusMgr.sendOdpEvents(apiKey: odpApiKey,
apiHost: odpApiHost,
events: events) { error in
self.apiMgr.sendOdpEvents(apiKey: odpApiKey,
apiHost: odpApiHost,
events: events) { error in
odpError = error
sync.leave() // our send is done.
}
Expand All @@ -161,7 +161,7 @@ class OdpEventManager {
}
}
}

removeStoredEvents(num: numEvents)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ import Foundation
}
*/

class ZaiusGraphQLApiManager {
class OdpSegmentApiManager {
let logger = OPTLoggerFactory.getLogger()

func fetchSegments(apiKey: String,
Expand Down
22 changes: 11 additions & 11 deletions Sources/ODP/OdpSegmentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import Foundation
class OdpSegmentManager {
var odpConfig: OdpConfig
var segmentsCache: LruCache<String, [String]>
var zaiusMgr: ZaiusGraphQLApiManager

var apiMgr: OdpSegmentApiManager
let logger = OPTLoggerFactory.getLogger()

init(cacheSize: Int,
cacheTimeoutInSecs: Int,
odpConfig: OdpConfig? = nil,
apiManager: ZaiusGraphQLApiManager? = nil) {
apiManager: OdpSegmentApiManager? = nil) {
self.odpConfig = odpConfig ?? OdpConfig()
self.zaiusMgr = apiManager ?? ZaiusGraphQLApiManager()
self.apiMgr = apiManager ?? OdpSegmentApiManager()

self.segmentsCache = LruCache<String, [String]>(size: cacheSize,
timeoutInSecs: cacheTimeoutInSecs)
Expand All @@ -51,7 +51,7 @@ class OdpSegmentManager {
}

let cacheKey = makeCacheKey(userKey, userValue)

let ignoreCache = options.contains(.ignoreCache)
let resetCache = options.contains(.resetCache)

Expand All @@ -66,11 +66,11 @@ class OdpSegmentManager {
}
}

zaiusMgr.fetchSegments(apiKey: odpApiKey,
apiHost: odpApiHost,
userKey: userKey,
userValue: userValue,
segmentsToCheck: segmentsToCheck) { segments, err in
apiMgr.fetchSegments(apiKey: odpApiKey,
apiHost: odpApiHost,
userKey: userKey,
userValue: userValue,
segmentsToCheck: segmentsToCheck) { segments, err in
if err == nil, let segments = segments {
if !ignoreCache {
self.segmentsCache.save(key: cacheKey, value: segments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import XCTest

class ZaiusRestApiManagerTests: XCTestCase {
class OdpEventApiManagerTests: XCTestCase {
let userKey = "vuid"
let userValue = "test-user-value"
let apiKey = "test-api-key"
Expand All @@ -30,17 +30,17 @@ class ZaiusRestApiManagerTests: XCTestCase {
// MARK: - success

func testSendOdpEvents_validRequest() {
let session = MockZaiusUrlSession(statusCode: 200, responseData: MockZaiusUrlSession.successResponseData)
let api = MockZaiusApiManager(session)

let session = MockOdpUrlSession(statusCode: 200, responseData: MockOdpUrlSession.successResponseData)
let api = MockOdpEventApiManager(session)
let sem = DispatchSemaphore(value: 0)
api.sendOdpEvents(apiKey: apiKey, apiHost: apiHost, events: events) { _ in
sem.signal()
}
XCTAssertEqual(.success, sem.wait(timeout: .now() + .seconds(1)))

let request = session.receivedApiRequest!

XCTAssertEqual(apiHost + "/v3/events", request.url?.absoluteString)
XCTAssertEqual("POST", request.httpMethod)
XCTAssertEqual("application/json", request.value(forHTTPHeaderField: "Content-Type"))
Expand All @@ -53,10 +53,10 @@ class ZaiusRestApiManagerTests: XCTestCase {
XCTAssert(OTUtils.compareDictionaries(expectedArray[i], bodyArray[i]))
}
}

func testSendOdpEvents_success() {
let api = MockZaiusApiManager(MockZaiusUrlSession(statusCode: 200,
responseData: MockZaiusUrlSession.successResponseData))
let api = MockOdpEventApiManager(MockOdpUrlSession(statusCode: 200,
responseData: MockOdpUrlSession.successResponseData))
let sem = DispatchSemaphore(value: 0)
api.sendOdpEvents(apiKey: apiKey, apiHost: apiHost, events: events) { error in
XCTAssertNil(error)
Expand All @@ -68,7 +68,7 @@ class ZaiusRestApiManagerTests: XCTestCase {
// MARK: - errors

func testSendOdpEvents_networkError_retry() {
let api = MockZaiusApiManager(MockZaiusUrlSession(withError: true))
let api = MockOdpEventApiManager(MockOdpUrlSession(withError: true))

let sem = DispatchSemaphore(value: 0)
api.sendOdpEvents(apiKey: apiKey, apiHost: apiHost, events: events) { error in
Expand All @@ -81,10 +81,10 @@ class ZaiusRestApiManagerTests: XCTestCase {
}
XCTAssertEqual(.success, sem.wait(timeout: .now() + .seconds(1)))
}

func testSendOdpEvents_400_noRetry() {
let api = MockZaiusApiManager(MockZaiusUrlSession(statusCode: 400, responseData: MockZaiusUrlSession.failureResponseData))

let api = MockOdpEventApiManager(MockOdpUrlSession(statusCode: 400, responseData: MockOdpUrlSession.failureResponseData))
let sem = DispatchSemaphore(value: 0)
api.sendOdpEvents(apiKey: apiKey, apiHost: apiHost, events: events) { error in
if case .odpEventFailed(_, let canRetry) = error {
Expand All @@ -98,8 +98,8 @@ class ZaiusRestApiManagerTests: XCTestCase {
}

func testSendOdpEvents_500_retry() {
let api = MockZaiusApiManager(MockZaiusUrlSession(statusCode: 500, responseData: "server error"))

let api = MockOdpEventApiManager(MockOdpUrlSession(statusCode: 500, responseData: "server error"))
let sem = DispatchSemaphore(value: 0)
api.sendOdpEvents(apiKey: apiKey, apiHost: apiHost, events: events) { error in
if case .odpEventFailed(_, let canRetry) = error {
Expand All @@ -120,16 +120,16 @@ class ZaiusRestApiManagerTests: XCTestCase {
// - validate both for nil and NSNull
OdpEvent(type: "t1", action: "a1", identifiers: ["id1": "value1"], data: ["key1": "value1", "key2": nil, "key3": NSNull()]),
]

let session = MockZaiusUrlSession(statusCode: 200, responseData: MockZaiusUrlSession.successResponseData)
let api = MockZaiusApiManager(session)

let session = MockOdpUrlSession(statusCode: 200, responseData: MockOdpUrlSession.successResponseData)
let api = MockOdpEventApiManager(session)
let sem = DispatchSemaphore(value: 0)
api.sendOdpEvents(apiKey: apiKey, apiHost: apiHost, events: events) { _ in
sem.signal()
}
XCTAssertEqual(.success, sem.wait(timeout: .now() + .seconds(1)))

let request = session.receivedApiRequest!

let jsonDispatched = String(bytes: request.httpBody!, encoding: .utf8)!
Expand All @@ -144,14 +144,14 @@ class ZaiusRestApiManagerTests: XCTestCase {
// tests below will be skipped in CI (travis/actions) since they use the live ODP server.
#if DEBUG

extension ZaiusRestApiManagerTests {
extension OdpEventApiManagerTests {

var odpApiKey: String { return "W4WzcEs-ABgXorzY7h1LCQ" }
var odpApiHost: String { return "https://api.zaius.com" }
var odpValidUserId: String { return "tester-101"}

func testLiveOdpRest() {
let manager = ZaiusRestApiManager()
let manager = OdpEventApiManager()

let sem = DispatchSemaphore(value: 0)
manager.sendOdpEvents(apiKey: odpApiKey,
Expand All @@ -167,7 +167,7 @@ extension ZaiusRestApiManagerTests {
}

func testLiveOdpRest_error() {
let manager = ZaiusRestApiManager()
let manager = OdpEventApiManager()

let sem = DispatchSemaphore(value: 0)
manager.sendOdpEvents(apiKey: odpApiKey,
Expand All @@ -185,11 +185,11 @@ extension ZaiusRestApiManagerTests {

#endif

// MARK: - MockZaiusApiManager
// MARK: - MockOdpEventApiManager

extension ZaiusRestApiManagerTests {
extension OdpEventApiManagerTests {

class MockZaiusApiManager: ZaiusRestApiManager {
class MockOdpEventApiManager: OdpEventApiManager {
let mockUrlSession: URLSession

init(_ urlSession: URLSession) {
Expand All @@ -201,7 +201,7 @@ extension ZaiusRestApiManagerTests {
}
}

class MockZaiusUrlSession: URLSession {
class MockOdpUrlSession: URLSession {
static var validSessions = 0
var statusCode: Int
var withError: Bool
Expand All @@ -224,7 +224,7 @@ extension ZaiusRestApiManagerTests {
Self.validSessions += 1
self.statusCode = statusCode
self.withError = withError
self.responseData = responseData ?? MockZaiusUrlSession.successResponseData
self.responseData = responseData ?? MockOdpUrlSession.successResponseData
}

override func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
Expand Down
10 changes: 5 additions & 5 deletions Tests/OptimizelyTests-Common/OdpEventManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import XCTest
class OdpEventManagerTests: XCTestCase {
var manager: OdpEventManager!
var odpConfig: OdpConfig!
var apiManager = MockZaiusApiManager()
var apiManager = MockOdpEventApiManager()

var options = [OptimizelySegmentOption]()

Expand Down Expand Up @@ -284,8 +284,8 @@ class OdpEventManagerTests: XCTestCase {
// MARK: - multiple skdKeys

func testMultipleSdkKeys_doNotInterfere() {
let apiManager1 = MockZaiusApiManager()
let apiManager2 = MockZaiusApiManager()
let apiManager1 = MockOdpEventApiManager()
let apiManager2 = MockOdpEventApiManager()
let odpConfig1 = OdpConfig()
let odpConfig2 = OdpConfig()

Expand Down Expand Up @@ -450,9 +450,9 @@ class OdpEventManagerTests: XCTestCase {
}
}

// MARK: - MockZaiusApiManager
// MARK: - MockOdpEventApiManager

class MockZaiusApiManager: ZaiusRestApiManager {
class MockOdpEventApiManager: OdpEventApiManager {
var receivedApiKey: String!
var receivedApiHost: String!
var dispatchedBatchEvents = [[OdpEvent]]()
Expand Down
Loading

0 comments on commit b0d7fcd

Please sign in to comment.