Skip to content

Fixed Configuration.apiHost not being honored before retrieving /settings #168

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
Oct 31, 2022
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
9 changes: 0 additions & 9 deletions Sources/Segment/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 26 additions & 7 deletions Sources/Segment/Plugins/SegmentDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Segment/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 4 additions & 18 deletions Sources/Segment/Utilities/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down