Skip to content

Commit abcf89d

Browse files
authored
Fixed Configuration.apiHost not being honored before retrieving /settings (#168)
* Fixed Configuration.apiHost not being honored before retrieving /settings * Removed unnecessary stuff.
1 parent 93314b0 commit abcf89d

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

Sources/Segment/Configuration.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@ import Foundation
1010
import FoundationNetworking
1111
#endif
1212

13-
public typealias AdvertisingIdCallback = () -> String?
14-
15-
1613
// MARK: - Internal Configuration
1714

18-
// - IDFA handled by external plugin if desired.
19-
// - recordingScreenViews handled by plugin?
20-
// - trackInAppPurchases handled by plugin?
21-
// - trackDeepLinks ??
22-
// - flushAt / flushInterval to be done by segment destination plugin
23-
2415
public class Configuration {
2516
internal struct Values {
2617
var writeKey: String

Sources/Segment/Plugins/SegmentDestination.swift

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public class SegmentDestination: DestinationPlugin, Subscriber {
4545
private let uploadsQueue = DispatchQueue(label: "uploadsQueue.segment.com")
4646
private var storage: Storage?
4747

48-
private var apiKey: String? = nil
49-
private var apiHost: String? = nil
50-
5148
@Atomic internal var eventCount: Int = 0
5249
internal var flushAt: Int = 0
5350
internal var flushTimer: QueueTimer? = nil
@@ -71,11 +68,33 @@ public class SegmentDestination: DestinationPlugin, Subscriber {
7168
}
7269

7370
public func update(settings: Settings, type: UpdateType) {
71+
guard let analytics = analytics else { return }
7472
let segmentInfo = settings.integrationSettings(forKey: self.key)
75-
apiKey = segmentInfo?[Self.Constants.apiKey.rawValue] as? String
76-
apiHost = segmentInfo?[Self.Constants.apiHost.rawValue] as? String
77-
if (apiHost != nil && apiKey != nil), let analytics = self.analytics {
78-
httpClient = HTTPClient(analytics: analytics, apiKey: apiKey, apiHost: apiHost)
73+
// if customer cycles out a writekey at app.segment.com, this is necessary.
74+
/*
75+
This actually works differently than anticipated. It was thought that when a writeKey was
76+
revoked, it's old writekey would redirect to the new, but it doesn't work this way. As a result
77+
it doesn't appear writekey can be changed remotely. Leaving this here in case that changes in the
78+
near future (written on 10/29/2022).
79+
*/
80+
/*
81+
if let key = segmentInfo?[Self.Constants.apiKey.rawValue] as? String, key.isEmpty == false {
82+
if key != analytics.configuration.values.writeKey {
83+
/*
84+
- would need to flush.
85+
- would need to change the writeKey across the system.
86+
- would need to re-init storage.
87+
- probably other things too ...
88+
*/
89+
}
90+
}
91+
*/
92+
// if customer specifies a different apiHost (ie: eu1.segmentapis.com) at app.segment.com ...
93+
if let host = segmentInfo?[Self.Constants.apiHost.rawValue] as? String, host.isEmpty == false {
94+
if host != analytics.configuration.values.writeKey {
95+
analytics.configuration.values.apiHost = host
96+
httpClient = HTTPClient(analytics: analytics)
97+
}
7998
}
8099
}
81100

Sources/Segment/Settings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ extension Analytics {
124124
#endif
125125

126126
let writeKey = self.configuration.values.writeKey
127-
let httpClient = HTTPClient(analytics: self, cdnHost: configuration.values.cdnHost)
127+
let httpClient = HTTPClient(analytics: self)
128128
let systemState: System? = store.currentState()
129129
let hasSettings = (systemState?.settings?.integrations != nil && systemState?.settings?.plan != nil)
130130
let updateType = (hasSettings ? UpdateType.refresh : UpdateType.initial)

Sources/Segment/Utilities/HTTPClient.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,12 @@ public class HTTPClient {
2727

2828
private weak var analytics: Analytics?
2929

30-
init(analytics: Analytics, apiKey: String? = nil, apiHost: String? = nil, cdnHost: String? = nil) {
30+
init(analytics: Analytics) {
3131
self.analytics = analytics
3232

33-
if let apiKey = apiKey {
34-
self.apiKey = apiKey
35-
} else {
36-
self.apiKey = analytics.configuration.values.writeKey
37-
}
38-
39-
if let apiHost = apiHost {
40-
self.apiHost = apiHost
41-
} else {
42-
self.apiHost = Self.defaultAPIHost
43-
}
44-
45-
if let cdnHost = cdnHost {
46-
self.cdnHost = cdnHost
47-
} else {
48-
self.cdnHost = Self.defaultCDNHost
49-
}
33+
self.apiKey = analytics.configuration.values.writeKey
34+
self.apiHost = analytics.configuration.values.apiHost
35+
self.cdnHost = analytics.configuration.values.cdnHost
5036

5137
self.session = Self.configuredSession(for: self.apiKey)
5238
}

0 commit comments

Comments
 (0)