From f23904a67befc3644dbd0928b928ff10f7b3329c Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Sat, 29 Oct 2022 12:42:26 -0700 Subject: [PATCH 1/2] Fixed Configuration.apiHost not being honored before retrieving /settings --- .../Segment/Plugins/SegmentDestination.swift | 33 +++++++++++++++---- Sources/Segment/Settings.swift | 2 +- Sources/Segment/Utilities/HTTPClient.swift | 22 +++---------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Sources/Segment/Plugins/SegmentDestination.swift b/Sources/Segment/Plugins/SegmentDestination.swift index bb2c1a14..d5087a97 100644 --- a/Sources/Segment/Plugins/SegmentDestination.swift +++ b/Sources/Segment/Plugins/SegmentDestination.swift @@ -45,9 +45,6 @@ public class SegmentDestination: DestinationPlugin, Subscriber { private let uploadsQueue = DispatchQueue(label: "uploadsQueue.segment.com") private var storage: Storage? - private var apiKey: String? = nil - private var apiHost: String? = nil - @Atomic internal var eventCount: Int = 0 internal var flushAt: Int = 0 internal var flushTimer: QueueTimer? = nil @@ -71,11 +68,33 @@ public class SegmentDestination: DestinationPlugin, Subscriber { } public func update(settings: Settings, type: UpdateType) { + guard let analytics = analytics else { return } let segmentInfo = settings.integrationSettings(forKey: self.key) - apiKey = segmentInfo?[Self.Constants.apiKey.rawValue] as? String - apiHost = segmentInfo?[Self.Constants.apiHost.rawValue] as? String - if (apiHost != nil && apiKey != nil), let analytics = self.analytics { - httpClient = HTTPClient(analytics: analytics, apiKey: apiKey, apiHost: apiHost) + // if customer cycles out a writekey at app.segment.com, this is necessary. + /* + This actually works differently than anticipated. It was thought that when a writeKey was + revoked, it's old writekey would redirect to the new, but it doesn't work this way. As a result + it doesn't appear writekey can be changed remotely. Leaving this here in case that changes in the + near future (written on 10/29/2022). + */ + /* + if let key = segmentInfo?[Self.Constants.apiKey.rawValue] as? String, key.isEmpty == false { + if key != analytics.configuration.values.writeKey { + /* + - would need to flush. + - would need to change the writeKey across the system. + - would need to re-init storage. + - probably other things too ... + */ + } + } + */ + // if customer specifies a different apiHost (ie: eu1.segmentapis.com) at app.segment.com ... + if let host = segmentInfo?[Self.Constants.apiHost.rawValue] as? String, host.isEmpty == false { + if host != analytics.configuration.values.writeKey { + analytics.configuration.values.apiHost = host + httpClient = HTTPClient(analytics: analytics) + } } } diff --git a/Sources/Segment/Settings.swift b/Sources/Segment/Settings.swift index b9e8f576..05d7d52a 100644 --- a/Sources/Segment/Settings.swift +++ b/Sources/Segment/Settings.swift @@ -124,7 +124,7 @@ extension Analytics { #endif let writeKey = self.configuration.values.writeKey - let httpClient = HTTPClient(analytics: self, cdnHost: configuration.values.cdnHost) + let httpClient = HTTPClient(analytics: self) let systemState: System? = store.currentState() let hasSettings = (systemState?.settings?.integrations != nil && systemState?.settings?.plan != nil) let updateType = (hasSettings ? UpdateType.refresh : UpdateType.initial) diff --git a/Sources/Segment/Utilities/HTTPClient.swift b/Sources/Segment/Utilities/HTTPClient.swift index 2c7c6287..19795c8a 100644 --- a/Sources/Segment/Utilities/HTTPClient.swift +++ b/Sources/Segment/Utilities/HTTPClient.swift @@ -27,26 +27,12 @@ public class HTTPClient { private weak var analytics: Analytics? - init(analytics: Analytics, apiKey: String? = nil, apiHost: String? = nil, cdnHost: String? = nil) { + init(analytics: Analytics) { self.analytics = analytics - if let apiKey = apiKey { - self.apiKey = apiKey - } else { - self.apiKey = analytics.configuration.values.writeKey - } - - if let apiHost = apiHost { - self.apiHost = apiHost - } else { - self.apiHost = Self.defaultAPIHost - } - - if let cdnHost = cdnHost { - self.cdnHost = cdnHost - } else { - self.cdnHost = Self.defaultCDNHost - } + self.apiKey = analytics.configuration.values.writeKey + self.apiHost = analytics.configuration.values.apiHost + self.cdnHost = analytics.configuration.values.cdnHost self.session = Self.configuredSession(for: self.apiKey) } From 7f7992372d34005cecd1decf4ba025216235deda Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Sat, 29 Oct 2022 12:42:38 -0700 Subject: [PATCH 2/2] Removed unnecessary stuff. --- Sources/Segment/Configuration.swift | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Sources/Segment/Configuration.swift b/Sources/Segment/Configuration.swift index b6f9ad9f..113aeaed 100644 --- a/Sources/Segment/Configuration.swift +++ b/Sources/Segment/Configuration.swift @@ -10,17 +10,8 @@ import Foundation import FoundationNetworking #endif -public typealias AdvertisingIdCallback = () -> String? - - // MARK: - Internal Configuration -// - IDFA handled by external plugin if desired. -// - recordingScreenViews handled by plugin? -// - trackInAppPurchases handled by plugin? -// - trackDeepLinks ?? -// - flushAt / flushInterval to be done by segment destination plugin - public class Configuration { internal struct Values { var writeKey: String