Skip to content

Commit

Permalink
fix: use apiHost from settings when present
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb authored Apr 10, 2024
2 parents f2226ad + fc15b18 commit d14a949
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 9 additions & 1 deletion packages/core/lib/plugins/segment_destination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -64,6 +66,12 @@ class SegmentDestination extends DestinationPlugin with Flushable {
add(_queuePlugin);
}

@override
void update(Map<String, dynamic> settings, ContextUpdateType type) {
super.update(settings, type);
_apiHost = settings[segmentDestinationKey]?['apiHost'];
}

@override
flush() {
return _queuePlugin.flush();
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,15 @@ class Configuration {
final int? maxBatchSize;
final Map<String, dynamic>? defaultIntegrationSettings;
final bool autoAddSegmentDestination;
final String apiHost;
final String? apiHost;
final String cdnHost;

final RequestFactory? requestFactory;
final StreamSubscription<AppStatus> Function()? appStateStream;
final ErrorHandler? errorHandler;

Configuration(this.writeKey,
{this.apiHost = HTTPClient.defaultAPIHost,
{this.apiHost,
this.autoAddSegmentDestination = true,
this.collectDeviceId = false,
this.cdnHost = HTTPClient.defaultCDNHost,
Expand Down
9 changes: 6 additions & 3 deletions packages/core/lib/utils/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> startBatchUpload(String writeKey, List<RawEvent> batch) async {
Uri uploadURL =
_url(_analytics.target!.state.configuration.state.apiHost, "/b");
Future<bool> startBatchUpload(String writeKey, List<RawEvent> 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",
Expand Down

0 comments on commit d14a949

Please sign in to comment.