Skip to content
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

feat!: use plugin options for optional plugin parameters #4762

Merged
merged 3 commits into from
Apr 25, 2024
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
8 changes: 6 additions & 2 deletions canaries/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ Future<void> main() async {
AmplifyAuthCognito(),
AmplifyStorageS3(),
AmplifyAnalyticsPinpoint(),
AmplifyAPI(modelProvider: ModelProvider.instance),
AmplifyDataStore(modelProvider: ModelProvider.instance),
AmplifyAPI(
options: APIPluginOptions(modelProvider: ModelProvider.instance),
),
AmplifyDataStore(
modelProvider: ModelProvider.instance,
),
]);
await Amplify.configure(amplifyconfig);
safePrint('Successfully configured Amplify.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';

part 'pinpoint_config.g.dart';
Expand Down Expand Up @@ -36,9 +35,7 @@ class PinpointPluginConfig
const PinpointPluginConfig({
required this.pinpointAnalytics,
required this.pinpointTargeting,
int autoFlushEventsInterval = 30,
}) : _autoFlushEventsInterval = autoFlushEventsInterval;

});
factory PinpointPluginConfig.fromJson(Map<String, Object?> json) =>
_$PinpointPluginConfigFromJson(json);

Expand All @@ -48,16 +45,8 @@ class PinpointPluginConfig
final PinpointAnalytics pinpointAnalytics;
final PinpointTargeting pinpointTargeting;

final int _autoFlushEventsInterval;

/// The duration in seconds between flushing analytics events to Pinpoint.
@_DurationConverter()
Duration get autoFlushEventsInterval =>
Duration(seconds: _autoFlushEventsInterval);

@override
List<Object?> get props =>
[pinpointAnalytics, pinpointTargeting, autoFlushEventsInterval];
List<Object?> get props => [pinpointAnalytics, pinpointTargeting];

PinpointPluginConfig copyWith({
PinpointAnalytics? pinpointAnalytics,
Expand All @@ -67,8 +56,6 @@ class PinpointPluginConfig
return PinpointPluginConfig(
pinpointAnalytics: pinpointAnalytics ?? this.pinpointAnalytics,
pinpointTargeting: pinpointTargeting ?? this.pinpointTargeting,
autoFlushEventsInterval:
autoFlushEventsInterval ?? _autoFlushEventsInterval,
);
}

Expand Down Expand Up @@ -132,13 +119,3 @@ class PinpointTargeting with AWSEquatable<PinpointTargeting>, AWSSerializable {
@override
Map<String, Object?> toJson() => _$PinpointTargetingToJson(this);
}

class _DurationConverter implements JsonConverter<Duration, int> {
const _DurationConverter();

@override
Duration fromJson(int json) => Duration(seconds: json);

@override
int toJson(Duration object) => object.inSeconds;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/amplify_core/test/config/cli_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const expected = {
region: REGION,
),
pinpointTargeting: PinpointTargeting(region: REGION),
autoFlushEventsInterval: ANALYTICS_FLUSH_INTERVAL,
),
},
),
Expand Down
15 changes: 5 additions & 10 deletions packages/amplify_core/test/config/testdata/cli_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ const _v4analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the full context on these tests, but I believe the intention is to test against the output from various versions of the Amplify CLI. These files are generated using an npm script, which I think depends on different versions of the Amplify CLI. If we regenerate these files these changes will be removed.

The approach I took in the auth PR was to remove certain keys from the JSON in the test assertions. You can see that here: https://github.com/aws-amplify/amplify-flutter/pull/4764/files#diff-10ab98843576a1782a1c3212f13233440a0474bf716ee5f2b70eb2264356d8ebR37

Long term I am not sure how useful these tests are. They are testing against versions of the CLI that are pretty outdated now. I think they should be updated or removed. I think it will be easier to do that as a follow up after we get the breaking changes in though.

Copy link
Member Author

@NikaHsn NikaHsn Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the file is auto generated by running the generate script. CLI does not generate this feild, based on our docs this field must be manually added to the configuration. https://docs.amplify.aws/flutter/build-a-backend/more-features/analytics/record-events/#flush-events

}
}
}
Expand Down Expand Up @@ -227,8 +226,7 @@ const _v5analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -415,8 +413,7 @@ const _v6analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -603,8 +600,7 @@ const _v7analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -791,8 +787,7 @@ const _vlatestanalytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const child_process = require('child_process');
const REGION = '$REGION';
const API_KEY = '$API_KEY';
const ANALYTICS_APP_ID = '$ANALYTICS_APP_ID';
const ANALYTICS_FLUSH_INTERVAL = '$ANALYTICS_FLUSH_INTERVAL';
const GRAPHQL_ENDPOINT = '$GRAPHQL_ENDPOINT';
const REST_ENDPOINT = '$REST_ENDPOINT';
const BUCKET = '$BUCKET';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const API_KEY = 'MY_KEY';

/// Analytics values
const ANALYTICS_APP_ID = 'analytics_app_id';
const ANALYTICS_FLUSH_INTERVAL = 10;

/// API values
const GRAPHQL_TYPE = 'GraphQL';
Expand Down
6 changes: 4 additions & 2 deletions packages/amplify_datastore/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class _MyAppState extends State<MyApp> {
try {
datastorePlugin = AmplifyDataStore(
modelProvider: ModelProvider.instance,
errorHandler: ((error) =>
{print("Custom ErrorHandler received: " + error.toString())}),
options: DataStorePluginOptions(
errorHandler: ((error) =>
{print("Custom ErrorHandler received: " + error.toString())}),
),
);
await Amplify.addPlugin(datastorePlugin);

Expand Down
35 changes: 12 additions & 23 deletions packages/amplify_datastore/lib/amplify_datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:convert';

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_datastore/src/amplify_datastore_stream_controller.dart';
import 'package:amplify_datastore/src/datastore_plugin_options.dart';
import 'package:amplify_datastore/src/method_channel_datastore.dart';
import 'package:amplify_datastore/src/native_plugin.g.dart';
import 'package:collection/collection.dart';
Expand All @@ -15,36 +16,24 @@ import 'package:meta/meta.dart';
export 'package:amplify_core/src/types/datastore/datastore_types.dart'
hide DateTimeParse;

export 'src/datastore_plugin_options.dart';

class AmplifyDataStore extends DataStorePluginInterface
with AWSDebuggable, AmplifyLoggerMixin {
/// Constructs an AmplifyDataStore plugin with mandatory [modelProvider]
/// and optional datastore configuration properties including
///
/// [syncExpressions]: list of sync expressions to filter datastore sync against
///
/// [syncInterval]: datastore syncing interval (in seconds)
///
/// [syncMaxRecords]: max number of records to sync
///
/// [syncPageSize]: page size to sync
/// and an optional datastore plugin options [options]
AmplifyDataStore({
required ModelProviderInterface modelProvider,
Function(AmplifyException)? errorHandler,
DataStoreConflictHandler? conflictHandler,
List<DataStoreSyncExpression> syncExpressions = const [],
int? syncInterval,
int? syncMaxRecords,
int? syncPageSize,
AuthModeStrategy authModeStrategy = AuthModeStrategy.defaultStrategy,
DataStorePluginOptions options = const DataStorePluginOptions(),
}) : super(
modelProvider: modelProvider,
errorHandler: errorHandler,
conflictHandler: conflictHandler,
syncExpressions: syncExpressions,
syncInterval: syncInterval,
syncMaxRecords: syncMaxRecords,
syncPageSize: syncPageSize,
authModeStrategy: authModeStrategy,
errorHandler: options.errorHandler,
conflictHandler: options.conflictHandler,
syncExpressions: options.syncExpressions,
syncInterval: options.syncInterval,
syncMaxRecords: options.syncMaxRecords,
syncPageSize: options.syncPageSize,
authModeStrategy: options.authModeStrategy,
);

/// Internal use constructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import 'package:amplify_core/amplify_core.dart';
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import 'package:amplify_core/amplify_core.dart';


/// {@template amplify_datastore.datastore_plugin_options}
/// The plugin options for the Amplify DataStore plugin.
/// {@endtemplate}
class DataStorePluginOptions {
/// {@macro amplify_datastore.datastore_plugin_options}
const DataStorePluginOptions({
this.errorHandler,
this.conflictHandler,
this.syncExpressions = const [],
this.syncInterval,
this.syncMaxRecords,
this.syncPageSize,
this.authModeStrategy = AuthModeStrategy.defaultStrategy,
});

/// The custom error handler function that receives an [AmplifyException]
/// object when DataStore encounters an unhandled error.
final Function(AmplifyException)? errorHandler;

/// The custom conflict handler function that receives an [ConflictData]
/// object when DataStore encounters a data conflict.
final DataStoreConflictHandler? conflictHandler;

/// The list of sync expressions to filter datastore sync.
final List<DataStoreSyncExpression> syncExpressions;

/// The syncing interval in seconds.
final int? syncInterval;

/// The max number of records to sync.
final int? syncMaxRecords;

/// The page size to sync.
final int? syncPageSize;

/// The strategy for authorizing an API call.
final AuthModeStrategy authModeStrategy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void main() {
);
AmplifyDataStore dataStore = AmplifyDataStore(
modelProvider: ModelProvider.instance,
errorHandler: (exception) => {receivedException = exception});
options: DataStorePluginOptions(
errorHandler: (exception) => {receivedException = exception}));
return dataStore.configureDataStore();
});

Expand Down
15 changes: 8 additions & 7 deletions packages/amplify_datastore/test/amplify_datastore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ void main() {

AmplifyDataStore dataStore = AmplifyDataStore(
modelProvider: ModelProvider.instance,
syncExpressions: [
DataStoreSyncExpression(Blog.classType, () => Blog.NAME.eq('foo')),
DataStoreSyncExpression(Post.classType, () => Post.TITLE.eq('bar'))
],
syncInterval: mockSyncInterval,
syncMaxRecords: mockSyncMaxRecords,
syncPageSize: mockSyncPagesize);
options: DataStorePluginOptions(
syncExpressions: [
DataStoreSyncExpression(Blog.classType, () => Blog.NAME.eq('foo')),
DataStoreSyncExpression(Post.classType, () => Post.TITLE.eq('bar'))
],
syncInterval: mockSyncInterval,
syncMaxRecords: mockSyncMaxRecords,
syncPageSize: mockSyncPagesize));

final binding = TestWidgetsFlutterBinding.ensureInitialized();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class _MyAppState extends State<MyApp> {
AmplifyAnalyticsPinpoint(
// ignore: invalid_use_of_visible_for_testing_member
secureStorageFactory: storageFactory,
options: const AnalyticsPinpointPluginOptions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: We should call this change out in the migration guide

autoFlushEventsInterval: Duration(seconds: 10),
),
),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AmplifyAnalyticsPinpoint extends AmplifyAnalyticsPinpointDart {
AmplifyAnalyticsPinpoint({
@visibleForTesting AppLifecycleProvider? appLifecycleProvider,
@visibleForTesting SecureStorageFactory? secureStorageFactory,
super.options,
}) : super(
pathProvider: FlutterPathProvider(),
legacyNativeDataProvider: FlutterLegacyNativeDataProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
library amplify_analytics_pinpoint_dart;

export 'src/analytics_plugin_impl.dart';

export 'src/analytics_plugin_options.dart';
export 'src/impl/analytics_client/endpoint_client/aws_pinpoint_user_profile.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import 'dart:async';

import 'package:amplify_analytics_pinpoint_dart/src/analytics_plugin_options.dart';
import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/analytics_client.dart';
import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/endpoint_client/endpoint_client.dart';
import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/event_client/event_client.dart';
Expand Down Expand Up @@ -39,12 +40,15 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
DeviceContextInfoProvider? deviceContextInfoProvider,
AppLifecycleProvider? appLifecycleProvider,
SecureStorageFactory? secureStorageFactory,
AnalyticsPinpointPluginOptions options =
const AnalyticsPinpointPluginOptions(),
}) : _pathProvider = pathProvider,
_legacyNativeDataProvider = legacyNativeDataProvider,
_deviceContextInfoProvider = deviceContextInfoProvider,
_appLifecycleProvider = appLifecycleProvider,
_secureStorageFactory =
secureStorageFactory ?? AmplifySecureStorageWorker.factoryFrom();
secureStorageFactory ?? AmplifySecureStorageWorker.factoryFrom(),
_options = options;

void _ensureConfigured() {
if (!_isConfigured) {
Expand Down Expand Up @@ -73,6 +77,7 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
final SecureStorageFactory _secureStorageFactory;
final DeviceContextInfoProvider? _deviceContextInfoProvider;
final LegacyNativeDataProvider? _legacyNativeDataProvider;
final AnalyticsPinpointPluginOptions _options;

static final _logger = AmplifyLogger.category(Category.analytics);

Expand Down Expand Up @@ -157,7 +162,7 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
},
);

final autoFlushEventsInterval = pinpointConfig.autoFlushEventsInterval;
final autoFlushEventsInterval = _options.autoFlushEventsInterval;

if (autoFlushEventsInterval.isNegative) {
throw ConfigurationError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/// {@template amplify_analytics_pinpoint_dart.analytics_pinpoint_plugin_options}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// {@template amplify_analytics_pinpoint_dart.analytics_pinpoint_plugin_options}
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/// {@template amplify_analytics_pinpoint_dart.analytics_pinpoint_plugin_options}

/// The plugin options for the Amplify Analytics Pinpoint plugin
/// {@endtemplate}
class AnalyticsPinpointPluginOptions {
/// {@macro amplify_analytics_pinpoint_dart.analytics_pinpoint_plugin_options}
const AnalyticsPinpointPluginOptions({
this.autoFlushEventsInterval = const Duration(seconds: 30),
});

/// The duration in seconds between flushing analytics events to Pinpoint.
final Duration autoFlushEventsInterval;
}
Loading
Loading