Skip to content

Commit

Permalink
feat: add apiProxyURL to DevCycleOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathannorris committed Aug 10, 2023
1 parent 00b1314 commit 7ebcd5d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DevCycle/DevCycleClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DevCycleClient {

self.config = DVCConfig(sdkKey: sdkKey, user: user)

let service = DevCycleService(config: self.config!, cacheService: self.cacheService)
let service = DevCycleService(config: self.config!, cacheService: self.cacheService, options: self.options)

self.initialize(service: service, callback: callback)
}
Expand Down
9 changes: 7 additions & 2 deletions DevCycle/Models/DevCycleOptions.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//
// DVCOptions.swift
// DevCycleOptions.swift
// DevCycle
//
//

import Foundation

Expand All @@ -16,6 +15,7 @@ public class DevCycleOptions {
var disableRealtimeUpdates: Bool = false
var disableAutomaticEventLogging: Bool = false
var disableCustomEventLogging: Bool = false
var apiProxyURL: String?

public class OptionsBuilder {
var options: DevCycleOptions
Expand Down Expand Up @@ -70,6 +70,11 @@ public class DevCycleOptions {
return self
}

public func apiProxyURL(_ proxyURL: String) -> OptionsBuilder {
self.options.apiProxyURL = proxyURL
return self
}

public func build() -> DevCycleOptions {
let result = self.options
self.options = DevCycleOptions()
Expand Down
22 changes: 18 additions & 4 deletions DevCycle/Networking/DevCycleService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ protocol DevCycleServiceProtocol {
class DevCycleService: DevCycleServiceProtocol {
var session: URLSession
var config: DVCConfig
var options: DevCycleOptions?

var cacheService: CacheServiceProtocol
var requestConsolidator: RequestConsolidator!

private var newUser: DevCycleUser?

init(config: DVCConfig, cacheService: CacheServiceProtocol) {
init(config: DVCConfig, cacheService: CacheServiceProtocol, options: DevCycleOptions? = nil) {
let sessionConfig = URLSessionConfiguration.default
self.session = URLSession(configuration: sessionConfig)
self.config = config
self.options = options
self.cacheService = cacheService
self.requestConsolidator = RequestConsolidator(service: self, cacheService: cacheService)
}
Expand Down Expand Up @@ -243,18 +245,30 @@ class DevCycleService: DevCycleServiceProtocol {

switch(type) {
case "event":
url = NetworkingConstants.eventsUrl + NetworkingConstants.hostUrl
if let proxyUrl = self.options?.apiProxyURL {
url = proxyUrl
} else {
url = NetworkingConstants.eventsUrl + NetworkingConstants.hostUrl
}
url.append("\(NetworkingConstants.Version.v1)")
url.append("\(NetworkingConstants.UrlPaths.events)")
case "edgeDB":
url = NetworkingConstants.sdkUrl + NetworkingConstants.hostUrl
if let proxyUrl = self.options?.apiProxyURL {
url = proxyUrl
} else {
url = NetworkingConstants.sdkUrl + NetworkingConstants.hostUrl
}
url.append("\(NetworkingConstants.Version.v1)")
url.append("\(NetworkingConstants.UrlPaths.edgeDB)")
if let userId = config.user.userId {
url.append("/\(userId)")
}
default:
url = NetworkingConstants.sdkUrl + NetworkingConstants.hostUrl
if let proxyUrl = self.options?.apiProxyURL {
url = proxyUrl
} else {
url = NetworkingConstants.sdkUrl + NetworkingConstants.hostUrl
}
url.append("\(NetworkingConstants.Version.v1)")
url.append("\(NetworkingConstants.UrlPaths.config)")
querySpecificItems.append(URLQueryItem(name: "sdkKey", value: config.sdkKey))
Expand Down
18 changes: 18 additions & 0 deletions DevCycle/ObjC/ObjCDevCycleOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ObjCDevCycleOptions: NSObject {
@objc public var disableConfigCache: NSNumber?
@objc public var configCacheTTL: NSNumber?
@objc public var disableRealtimeUpdates: NSNumber?
@objc public var disableAutomaticEventLogging: NSNumber?
@objc public var disableCustomEventLogging: NSNumber?
@objc public var apiProxyURL: NSString?

func buildDevCycleOptions() -> DevCycleOptions {
var optionsBuilder = DevCycleOptions.builder()
Expand Down Expand Up @@ -54,6 +57,21 @@ public class ObjCDevCycleOptions: NSObject {
optionsBuilder = optionsBuilder.disableRealtimeUpdates(disable)
}

if let disableAutomaticEventLogging = self.disableAutomaticEventLogging,
let disable = disableAutomaticEventLogging as? Bool {
optionsBuilder = optionsBuilder.disableAutomaticEventLogging(disable)
}

if let disableCustomEventLogging = self.disableCustomEventLogging,
let disable = disableCustomEventLogging as? Bool {
optionsBuilder = optionsBuilder.disableCustomEventLogging(disable)
}

if let apiProxyURL = self.apiProxyURL,
let proxyURL = apiProxyURL as? String {
optionsBuilder = optionsBuilder.apiProxyURL(proxyURL)
}

if let logLevel = self.logLevel,
let level = logLevel as? Int {
var setLogLevel = LogLevel.error
Expand Down
6 changes: 6 additions & 0 deletions DevCycleTests/Models/DevCycleOptionsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class DevCycleOptionsTest: XCTestCase {
.configCacheTTL(172800000)
.disableConfigCache(true)
.disableRealtimeUpdates(true)
.disableCustomEventLogging(true)
.disableAutomaticEventLogging(true)
.apiProxyURL("localhost:4000")
.build()
XCTAssertNotNil(options)
XCTAssert(options.flushEventsIntervalMs == 1000)
Expand All @@ -29,6 +32,9 @@ class DevCycleOptionsTest: XCTestCase {
XCTAssert(options.configCacheTTL == 172800000)
XCTAssert(options.disableConfigCache)
XCTAssert(options.disableRealtimeUpdates)
XCTAssert(options.disableCustomEventLogging)
XCTAssert(options.disableAutomaticEventLogging)
XCTAssert(options.apiProxyURL == "localhost:4000")
}

func testBuilderReturnsOptionsAndSomeAreNil() {
Expand Down
26 changes: 24 additions & 2 deletions DevCycleTests/Networking/DevCycleServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class DevCycleServiceTests: XCTestCase {
XCTAssert(url!.contains("user_id=my_user"))
}

func testProxyConfigURL() throws {
let options = DevCycleOptions.builder().apiProxyURL("localhost:4000").build()
let url = getService(options).createConfigRequest(user: getTestUser(), enableEdgeDB: false).url?.absoluteString
XCTAssert(url!.contains("localhost:4000/v1/mobileSDKConfig"))
XCTAssert(url!.contains("sdkKey=my_sdk_key"))
XCTAssert(url!.contains("user_id=my_user"))
}

func testCreateConfigURLRequestWithEdgeDB() throws {
let url = getService().createConfigRequest(user: getTestUser(), enableEdgeDB: true).url?.absoluteString
XCTAssert(url!.contains("https://sdk-api.devcycle.com/v1/mobileSDKConfig"))
Expand All @@ -29,12 +37,26 @@ class DevCycleServiceTests: XCTestCase {
XCTAssertFalse(url!.contains("user_id=my_user"))
}

func testProxyEventUrl() throws {
let options = DevCycleOptions.builder().apiProxyURL("localhost:4000").build()
let url = getService(options).createEventsRequest().url?.absoluteString
XCTAssert(url!.contains("localhost:4000/v1/events"))
XCTAssertFalse(url!.contains("user_id=my_user"))
}

func testCreateSaveEntityRequest() throws {
let url = getService().createSaveEntityRequest().url?.absoluteString
XCTAssert(url!.contains("https://sdk-api.devcycle.com/v1/edgedb"))
XCTAssert(url!.contains("my_user"))
}

func testProxyEntityUrl() throws {
let options = DevCycleOptions.builder().apiProxyURL("localhost:4000").build()
let url = getService(options).createSaveEntityRequest().url?.absoluteString
XCTAssert(url!.contains("localhost:4000/v1/edgedb"))
XCTAssert(url!.contains("my_user"))
}

func testProcessConfigReturnsNilIfMissingProperties() throws {
let data = "{\"config\":\"key\"}".data(using: .utf8)
let config = processConfig(data)
Expand Down Expand Up @@ -81,10 +103,10 @@ extension DevCycleServiceTests {
}
}

func getService() -> DevCycleService {
func getService(_ options: DevCycleOptions? = nil) -> DevCycleService {
let user = getTestUser()
let config = DVCConfig(sdkKey: "my_sdk_key", user: user)
return DevCycleService(config: config, cacheService: MockCacheService())
return DevCycleService(config: config, cacheService: MockCacheService(), options: options)
}

func getTestUser() -> DevCycleUser {
Expand Down

0 comments on commit 7ebcd5d

Please sign in to comment.