From 6030a47c3c883ebea3d7529f9b5fc1fb9f0f5029 Mon Sep 17 00:00:00 2001 From: Oscar Bazaldua <511911+oscb@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:01:55 -0700 Subject: [PATCH 1/2] fix: use apiHost from settings when present --- packages/core/lib/plugins/segment_destination.dart | 13 ++++++++++++- packages/core/lib/state.dart | 4 ++-- packages/core/lib/utils/http_client.dart | 9 ++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/core/lib/plugins/segment_destination.dart b/packages/core/lib/plugins/segment_destination.dart index cd44413..f01d60c 100644 --- a/packages/core/lib/plugins/segment_destination.dart +++ b/packages/core/lib/plugins/segment_destination.dart @@ -12,6 +12,7 @@ const segmentDestinationKey = 'Segment.io'; class SegmentDestination extends DestinationPlugin with Flushable { late final QueueFlushingPlugin _queuePlugin; + String? _apiHost; SegmentDestination() : super(segmentDestinationKey) { _queuePlugin = QueueFlushingPlugin(_sendEvents); @@ -32,7 +33,8 @@ class SegmentDestination extends DestinationPlugin with Flushable { await Future.forEach(chunkedEvents, (batch) async { try { final succeeded = await analytics?.httpClient.startBatchUpload( - analytics!.state.configuration.state.writeKey, batch); + analytics!.state.configuration.state.writeKey, batch, + host: _apiHost); if (succeeded == null || !succeeded) { numFailedEvents += batch.length; } @@ -64,6 +66,15 @@ class SegmentDestination extends DestinationPlugin with Flushable { add(_queuePlugin); } + @override + void update(Map settings, ContextUpdateType type) { + super.update(settings, type); + if (settings[segmentDestinationKey] != null && + settings[segmentDestinationKey]['apiHost'] != null) { + _apiHost = '${settings[segmentDestinationKey]['apiHost']}'; + } + } + @override flush() { return _queuePlugin.flush(); diff --git a/packages/core/lib/state.dart b/packages/core/lib/state.dart index e1118fa..aaf7e26 100644 --- a/packages/core/lib/state.dart +++ b/packages/core/lib/state.dart @@ -516,7 +516,7 @@ class Configuration { final int? maxBatchSize; final Map? defaultIntegrationSettings; final bool autoAddSegmentDestination; - final String apiHost; + final String? apiHost; final String cdnHost; final RequestFactory? requestFactory; @@ -524,7 +524,7 @@ class Configuration { final ErrorHandler? errorHandler; Configuration(this.writeKey, - {this.apiHost = HTTPClient.defaultAPIHost, + {this.apiHost, this.autoAddSegmentDestination = true, this.collectDeviceId = false, this.cdnHost = HTTPClient.defaultCDNHost, diff --git a/packages/core/lib/utils/http_client.dart b/packages/core/lib/utils/http_client.dart index 701aad4..e7efd61 100644 --- a/packages/core/lib/utils/http_client.dart +++ b/packages/core/lib/utils/http_client.dart @@ -27,9 +27,12 @@ class HTTPClient { /// - key: The write key the events are assocaited with. /// - batch: The array of the events, considered a batch of events. /// - completion: The closure executed when done. Passes if the task should be retried or not if failed. - Future startBatchUpload(String writeKey, List batch) async { - Uri uploadURL = - _url(_analytics.target!.state.configuration.state.apiHost, "/b"); + Future startBatchUpload(String writeKey, List batch, + {String? host = null}) async { + final apihost = _analytics.target!.state.configuration.state.apiHost ?? + host ?? + defaultAPIHost; + Uri uploadURL = _url(apihost, "/b"); try { var urlRequest = _configuredRequest(uploadURL, "POST", From fc15b189c6c3e022b8801116193b943acfdf92a3 Mon Sep 17 00:00:00 2001 From: Oscar Bazaldua <511911+oscb@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:02:41 -0700 Subject: [PATCH 2/2] fix: handle reseting apiHost to null --- packages/core/lib/plugins/segment_destination.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/core/lib/plugins/segment_destination.dart b/packages/core/lib/plugins/segment_destination.dart index f01d60c..0970bfc 100644 --- a/packages/core/lib/plugins/segment_destination.dart +++ b/packages/core/lib/plugins/segment_destination.dart @@ -69,10 +69,7 @@ class SegmentDestination extends DestinationPlugin with Flushable { @override void update(Map settings, ContextUpdateType type) { super.update(settings, type); - if (settings[segmentDestinationKey] != null && - settings[segmentDestinationKey]['apiHost'] != null) { - _apiHost = '${settings[segmentDestinationKey]['apiHost']}'; - } + _apiHost = settings[segmentDestinationKey]?['apiHost']; } @override