diff --git a/canaries/lib/main.dart b/canaries/lib/main.dart index a3bb8c641a..6f8ed388e1 100644 --- a/canaries/lib/main.dart +++ b/canaries/lib/main.dart @@ -20,8 +20,12 @@ Future 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.'); diff --git a/packages/amplify_core/lib/src/config/analytics/pinpoint_config.dart b/packages/amplify_core/lib/src/config/analytics/pinpoint_config.dart index 023dc1d7dd..e950a5ab64 100644 --- a/packages/amplify_core/lib/src/config/analytics/pinpoint_config.dart +++ b/packages/amplify_core/lib/src/config/analytics/pinpoint_config.dart @@ -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'; @@ -36,9 +35,7 @@ class PinpointPluginConfig const PinpointPluginConfig({ required this.pinpointAnalytics, required this.pinpointTargeting, - int autoFlushEventsInterval = 30, - }) : _autoFlushEventsInterval = autoFlushEventsInterval; - + }); factory PinpointPluginConfig.fromJson(Map json) => _$PinpointPluginConfigFromJson(json); @@ -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 get props => - [pinpointAnalytics, pinpointTargeting, autoFlushEventsInterval]; + List get props => [pinpointAnalytics, pinpointTargeting]; PinpointPluginConfig copyWith({ PinpointAnalytics? pinpointAnalytics, @@ -67,8 +56,6 @@ class PinpointPluginConfig return PinpointPluginConfig( pinpointAnalytics: pinpointAnalytics ?? this.pinpointAnalytics, pinpointTargeting: pinpointTargeting ?? this.pinpointTargeting, - autoFlushEventsInterval: - autoFlushEventsInterval ?? _autoFlushEventsInterval, ); } @@ -132,13 +119,3 @@ class PinpointTargeting with AWSEquatable, AWSSerializable { @override Map toJson() => _$PinpointTargetingToJson(this); } - -class _DurationConverter implements JsonConverter { - const _DurationConverter(); - - @override - Duration fromJson(int json) => Duration(seconds: json); - - @override - int toJson(Duration object) => object.inSeconds; -} diff --git a/packages/amplify_core/lib/src/config/analytics/pinpoint_config.g.dart b/packages/amplify_core/lib/src/config/analytics/pinpoint_config.g.dart index 74db915c67..e72a3e0dd8 100644 --- a/packages/amplify_core/lib/src/config/analytics/pinpoint_config.g.dart +++ b/packages/amplify_core/lib/src/config/analytics/pinpoint_config.g.dart @@ -15,7 +15,6 @@ PinpointPluginConfig _$PinpointPluginConfigFromJson( json['pinpointAnalytics'] as Map), pinpointTargeting: PinpointTargeting.fromJson( json['pinpointTargeting'] as Map), - autoFlushEventsInterval: json['autoFlushEventsInterval'] as int? ?? 30, ); Map _$PinpointPluginConfigToJson( @@ -23,8 +22,6 @@ Map _$PinpointPluginConfigToJson( { 'pinpointAnalytics': instance.pinpointAnalytics.toJson(), 'pinpointTargeting': instance.pinpointTargeting.toJson(), - 'autoFlushEventsInterval': - const _DurationConverter().toJson(instance.autoFlushEventsInterval), }; PinpointAnalytics _$PinpointAnalyticsFromJson(Map json) => diff --git a/packages/amplify_core/test/config/cli_config_test.dart b/packages/amplify_core/test/config/cli_config_test.dart index 2129d41037..b87fecfc53 100644 --- a/packages/amplify_core/test/config/cli_config_test.dart +++ b/packages/amplify_core/test/config/cli_config_test.dart @@ -39,7 +39,6 @@ const expected = { region: REGION, ), pinpointTargeting: PinpointTargeting(region: REGION), - autoFlushEventsInterval: ANALYTICS_FLUSH_INTERVAL, ), }, ), diff --git a/packages/amplify_core/test/config/testdata/cli_generated.dart b/packages/amplify_core/test/config/testdata/cli_generated.dart index 10fc7325da..67871f33bd 100644 --- a/packages/amplify_core/test/config/testdata/cli_generated.dart +++ b/packages/amplify_core/test/config/testdata/cli_generated.dart @@ -39,8 +39,7 @@ const _v4analytics = ''' }, "pinpointTargeting": { "region": "$REGION" - }, - "autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL + } } } } @@ -227,8 +226,7 @@ const _v5analytics = ''' }, "pinpointTargeting": { "region": "$REGION" - }, - "autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL + } } } } @@ -415,8 +413,7 @@ const _v6analytics = ''' }, "pinpointTargeting": { "region": "$REGION" - }, - "autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL + } } } } @@ -603,8 +600,7 @@ const _v7analytics = ''' }, "pinpointTargeting": { "region": "$REGION" - }, - "autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL + } } } } @@ -791,8 +787,7 @@ const _vlatestanalytics = ''' }, "pinpointTargeting": { "region": "$REGION" - }, - "autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL + } } } } diff --git a/packages/amplify_core/test/config/testdata/generate/generate.js b/packages/amplify_core/test/config/testdata/generate/generate.js index a6b4b19103..725a3f7749 100644 --- a/packages/amplify_core/test/config/testdata/generate/generate.js +++ b/packages/amplify_core/test/config/testdata/generate/generate.js @@ -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'; diff --git a/packages/amplify_core/test/config/testdata/test_values.dart b/packages/amplify_core/test/config/testdata/test_values.dart index 2d8232ae24..b6e2d6b096 100644 --- a/packages/amplify_core/test/config/testdata/test_values.dart +++ b/packages/amplify_core/test/config/testdata/test_values.dart @@ -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'; diff --git a/packages/amplify_datastore/example/lib/main.dart b/packages/amplify_datastore/example/lib/main.dart index f2c5cf6aac..08c6487b77 100644 --- a/packages/amplify_datastore/example/lib/main.dart +++ b/packages/amplify_datastore/example/lib/main.dart @@ -73,8 +73,10 @@ class _MyAppState extends State { 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); diff --git a/packages/amplify_datastore/lib/amplify_datastore.dart b/packages/amplify_datastore/lib/amplify_datastore.dart index 2799013f15..b4a35232dd 100644 --- a/packages/amplify_datastore/lib/amplify_datastore.dart +++ b/packages/amplify_datastore/lib/amplify_datastore.dart @@ -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'; @@ -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 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 diff --git a/packages/amplify_datastore/lib/src/datastore_plugin_options.dart b/packages/amplify_datastore/lib/src/datastore_plugin_options.dart new file mode 100644 index 0000000000..368a9aaa53 --- /dev/null +++ b/packages/amplify_datastore/lib/src/datastore_plugin_options.dart @@ -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'; + +/// {@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 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; +} diff --git a/packages/amplify_datastore/test/amplify_datastore_custom_error_handler_test.dart b/packages/amplify_datastore/test/amplify_datastore_custom_error_handler_test.dart index ed1a414b92..dd50b6b746 100644 --- a/packages/amplify_datastore/test/amplify_datastore_custom_error_handler_test.dart +++ b/packages/amplify_datastore/test/amplify_datastore_custom_error_handler_test.dart @@ -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(); }); diff --git a/packages/amplify_datastore/test/amplify_datastore_test.dart b/packages/amplify_datastore/test/amplify_datastore_test.dart index cbb297a67a..ed6bbae0f5 100644 --- a/packages/amplify_datastore/test/amplify_datastore_test.dart +++ b/packages/amplify_datastore/test/amplify_datastore_test.dart @@ -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(); diff --git a/packages/analytics/amplify_analytics_pinpoint/example/lib/main.dart b/packages/analytics/amplify_analytics_pinpoint/example/lib/main.dart index 08adabc474..5ff95d9718 100644 --- a/packages/analytics/amplify_analytics_pinpoint/example/lib/main.dart +++ b/packages/analytics/amplify_analytics_pinpoint/example/lib/main.dart @@ -54,6 +54,9 @@ class _MyAppState extends State { AmplifyAnalyticsPinpoint( // ignore: invalid_use_of_visible_for_testing_member secureStorageFactory: storageFactory, + options: const AnalyticsPinpointPluginOptions( + autoFlushEventsInterval: Duration(seconds: 10), + ), ), ]); diff --git a/packages/analytics/amplify_analytics_pinpoint/lib/src/analytics_plugin_impl.dart b/packages/analytics/amplify_analytics_pinpoint/lib/src/analytics_plugin_impl.dart index 56ce4f73c8..1115d912d5 100644 --- a/packages/analytics/amplify_analytics_pinpoint/lib/src/analytics_plugin_impl.dart +++ b/packages/analytics/amplify_analytics_pinpoint/lib/src/analytics_plugin_impl.dart @@ -19,6 +19,7 @@ class AmplifyAnalyticsPinpoint extends AmplifyAnalyticsPinpointDart { AmplifyAnalyticsPinpoint({ @visibleForTesting AppLifecycleProvider? appLifecycleProvider, @visibleForTesting SecureStorageFactory? secureStorageFactory, + super.options, }) : super( pathProvider: FlutterPathProvider(), legacyNativeDataProvider: FlutterLegacyNativeDataProvider(), diff --git a/packages/analytics/amplify_analytics_pinpoint_dart/lib/amplify_analytics_pinpoint_dart.dart b/packages/analytics/amplify_analytics_pinpoint_dart/lib/amplify_analytics_pinpoint_dart.dart index cd0653ef24..ca7b1bc381 100644 --- a/packages/analytics/amplify_analytics_pinpoint_dart/lib/amplify_analytics_pinpoint_dart.dart +++ b/packages/analytics/amplify_analytics_pinpoint_dart/lib/amplify_analytics_pinpoint_dart.dart @@ -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'; diff --git a/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_impl.dart b/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_impl.dart index 14330b3747..2ecc795bad 100644 --- a/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_impl.dart +++ b/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_impl.dart @@ -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'; @@ -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) { @@ -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); @@ -157,7 +162,7 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface { }, ); - final autoFlushEventsInterval = pinpointConfig.autoFlushEventsInterval; + final autoFlushEventsInterval = _options.autoFlushEventsInterval; if (autoFlushEventsInterval.isNegative) { throw ConfigurationError( diff --git a/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_options.dart b/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_options.dart new file mode 100644 index 0000000000..1e523e6951 --- /dev/null +++ b/packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_options.dart @@ -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} +/// 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; +} diff --git a/packages/analytics/amplify_analytics_pinpoint_dart/test/analytics_plugin_config_test.dart b/packages/analytics/amplify_analytics_pinpoint_dart/test/analytics_plugin_config_test.dart index 98b5d2369e..62863fdde8 100644 --- a/packages/analytics/amplify_analytics_pinpoint_dart/test/analytics_plugin_config_test.dart +++ b/packages/analytics/amplify_analytics_pinpoint_dart/test/analytics_plugin_config_test.dart @@ -1,4 +1,5 @@ import 'package:amplify_analytics_pinpoint_dart/src/analytics_plugin_impl.dart'; +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'; @@ -80,11 +81,12 @@ void main() { }); test('throws ConfigurationError when negative', () async { - const autoFlushInterval = -1; - final plugin = AmplifyAnalyticsPinpointDart( pathProvider: mockPathProvider, secureStorageFactory: (_) => MockSecureStorage(), + options: const AnalyticsPinpointPluginOptions( + autoFlushEventsInterval: Duration(seconds: -1), + ), ); await expectLater( @@ -100,7 +102,6 @@ void main() { pinpointTargeting: PinpointTargeting( region: region, ), - autoFlushEventsInterval: autoFlushInterval, ), }, ), @@ -116,11 +117,12 @@ void main() { }); test('disables autoFlush when 0', () async { - const autoFlushInterval = 0; - final plugin = AmplifyAnalyticsPinpointDart( pathProvider: mockPathProvider, secureStorageFactory: (_) => MockSecureStorage(), + options: const AnalyticsPinpointPluginOptions( + autoFlushEventsInterval: Duration.zero, + ), ); await plugin.configure( @@ -135,7 +137,6 @@ void main() { pinpointTargeting: PinpointTargeting( region: region, ), - autoFlushEventsInterval: autoFlushInterval, ), }, ), @@ -156,6 +157,9 @@ void main() { final plugin = AmplifyAnalyticsPinpointDart( pathProvider: mockPathProvider, secureStorageFactory: (_) => MockSecureStorage(), + options: const AnalyticsPinpointPluginOptions( + autoFlushEventsInterval: Duration(seconds: autoFlushInterval), + ), ); await plugin.configure( @@ -170,7 +174,6 @@ void main() { pinpointTargeting: PinpointTargeting( region: region, ), - autoFlushEventsInterval: autoFlushInterval, ), }, ), diff --git a/packages/api/amplify_api/example/integration_test/util.dart b/packages/api/amplify_api/example/integration_test/util.dart index eec8ae4516..a1d1647cbb 100644 --- a/packages/api/amplify_api/example/integration_test/util.dart +++ b/packages/api/amplify_api/example/integration_test/util.dart @@ -84,7 +84,9 @@ Future configureAmplify() async { macOSOptions: MacOSSecureStorageOptions(useDataProtection: false), ), ), - AmplifyAPI(modelProvider: ModelProvider.instance), + AmplifyAPI( + options: APIPluginOptions(modelProvider: ModelProvider.instance), + ), ]); await Amplify.configure(amplifyconfig); } diff --git a/packages/api/amplify_api/example/lib/main.dart b/packages/api/amplify_api/example/lib/main.dart index 1175650f60..8cac5c6403 100644 --- a/packages/api/amplify_api/example/lib/main.dart +++ b/packages/api/amplify_api/example/lib/main.dart @@ -48,7 +48,15 @@ class _MyAppState extends State { authPlugin, // FIXME: In your app, make sure to run `amplify codegen models` to generate // the models and provider - AmplifyAPI(modelProvider: ModelProvider.instance), + AmplifyAPI( + options: APIPluginOptions( + modelProvider: ModelProvider.instance, + subscriptionOptions: const GraphQLSubscriptionOptions( + pollInterval: Duration(seconds: 10), + retryOptions: RetryOptions(), + ), + ), + ), ]); try { diff --git a/packages/api/amplify_api/lib/src/api_plugin_impl.dart b/packages/api/amplify_api/lib/src/api_plugin_impl.dart index 78ac5876fd..abd05fe314 100644 --- a/packages/api/amplify_api/lib/src/api_plugin_impl.dart +++ b/packages/api/amplify_api/lib/src/api_plugin_impl.dart @@ -11,10 +11,7 @@ import 'package:amplify_core/amplify_core.dart'; class AmplifyAPI extends AmplifyAPIDart with AWSDebuggable { /// {@macro amplify_api.amplify_api} AmplifyAPI({ - super.authProviders, - super.baseHttpClient, - super.modelProvider, - super.subscriptionOptions, + super.options, }) : super( connectivity: const ConnectivityPlusPlatform(), ); diff --git a/packages/api/amplify_api_dart/lib/amplify_api_dart.dart b/packages/api/amplify_api_dart/lib/amplify_api_dart.dart index c53fc54d45..44022623ce 100644 --- a/packages/api/amplify_api_dart/lib/amplify_api_dart.dart +++ b/packages/api/amplify_api_dart/lib/amplify_api_dart.dart @@ -8,6 +8,9 @@ export 'package:amplify_core/src/types/api/api_types.dart'; export 'src/api_plugin_impl.dart'; +/// API plugin options +export 'src/api_plugin_options.dart'; + /// Model helpers export 'src/graphql/model_helpers/model_mutations.dart'; export 'src/graphql/model_helpers/model_queries.dart'; diff --git a/packages/api/amplify_api_dart/lib/src/api_plugin_impl.dart b/packages/api/amplify_api_dart/lib/src/api_plugin_impl.dart index 0bd78213ce..e3b1b9b530 100644 --- a/packages/api/amplify_api_dart/lib/src/api_plugin_impl.dart +++ b/packages/api/amplify_api_dart/lib/src/api_plugin_impl.dart @@ -3,6 +3,7 @@ import 'dart:async'; +import 'package:amplify_api_dart/src/api_plugin_options.dart'; import 'package:amplify_api_dart/src/graphql/helpers/send_graphql_request.dart'; import 'package:amplify_api_dart/src/graphql/providers/app_sync_api_key_auth_provider.dart'; import 'package:amplify_api_dart/src/graphql/providers/oidc_function_api_auth_provider.dart'; @@ -21,18 +22,15 @@ import 'package:meta/meta.dart'; class AmplifyAPIDart extends APIPluginInterface with AWSDebuggable { /// {@macro amplify_api_dart.amplify_api_dart} AmplifyAPIDart({ - List authProviders = const [], + APIPluginOptions options = const APIPluginOptions(), ConnectivityPlatform connectivity = const ConnectivityPlatform(), - AWSHttpClient? baseHttpClient, - this.modelProvider, - this.subscriptionOptions, - }) : _baseHttpClient = baseHttpClient, + }) : _options = options, _connectivity = connectivity { - authProviders.forEach(registerAuthProvider); + _options.authProviders.forEach(registerAuthProvider); } + final APIPluginOptions _options; late final AWSApiPluginConfig _apiConfig; - final AWSHttpClient? _baseHttpClient; late final AmplifyAuthProviderRepository _authProviderRepo; /// Creates a stream representing network connectivity at the hardware level. @@ -49,9 +47,6 @@ class AmplifyAPIDart extends APIPluginInterface with AWSDebuggable { final StreamController _hubEventController = StreamController.broadcast(); - /// Subscription options - final GraphQLSubscriptionOptions? subscriptionOptions; - @override Future reset() async { for (final bloc in _webSocketBlocPool.values) { @@ -156,7 +151,7 @@ class AmplifyAPIDart extends APIPluginInterface with AWSDebuggable { dependencies, baseClient: AmplifyAuthorizationRestClient( endpointConfig: endpoint.config, - baseClient: _baseHttpClient ?? dependencies.getOrCreate(), + baseClient: _options.baseHttpClient ?? dependencies.getOrCreate(), authorizationMode: authorizationMode, authProviderRepo: _authProviderRepo, ), @@ -187,8 +182,7 @@ class AmplifyAPIDart extends APIPluginInterface with AWSDebuggable { config: endpoint.config, authProviderRepo: _authProviderRepo, wsService: AmplifyWebSocketService(), - subscriptionOptions: - subscriptionOptions ?? const GraphQLSubscriptionOptions(), + subscriptionOptions: _options.subscriptionOptions, connectivity: _connectivity, ); } @@ -214,8 +208,7 @@ class AmplifyAPIDart extends APIPluginInterface with AWSDebuggable { } @override - final ModelProviderInterface? modelProvider; - + ModelProviderInterface? get modelProvider => _options.modelProvider; // ====== GraphQL ====== @override diff --git a/packages/api/amplify_api_dart/lib/src/api_plugin_options.dart b/packages/api/amplify_api_dart/lib/src/api_plugin_options.dart new file mode 100644 index 0000000000..9184a40189 --- /dev/null +++ b/packages/api/amplify_api_dart/lib/src/api_plugin_options.dart @@ -0,0 +1,29 @@ +// 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_api_dart.api_plugin_options} +/// The plugin options for the Amplify API plugin. +/// {@endtemplate} +class APIPluginOptions { + /// {@macro amplify_api_dart.api_plugin_options} + const APIPluginOptions({ + this.authProviders = const [], + this.baseHttpClient, + this.modelProvider, + this.subscriptionOptions = const GraphQLSubscriptionOptions(), + }); + + /// List of API Auth providers to be used for API category operations. + final List authProviders; + + /// The HTTP client to be used for API category operations. + final AWSHttpClient? baseHttpClient; + + /// The model provider for providing access to the data models + final ModelProviderInterface? modelProvider; + + /// {@macro amplify_core.graphql.graphql_subscription_options} + final GraphQLSubscriptionOptions subscriptionOptions; +} diff --git a/packages/api/amplify_api_dart/test/graphql_helpers_legacy_schema_test.dart b/packages/api/amplify_api_dart/test/graphql_helpers_legacy_schema_test.dart index fe79a994f1..fa09fcbd86 100644 --- a/packages/api/amplify_api_dart/test/graphql_helpers_legacy_schema_test.dart +++ b/packages/api/amplify_api_dart/test/graphql_helpers_legacy_schema_test.dart @@ -17,7 +17,7 @@ final _deepEquals = const DeepCollectionEquality().equals; class MockAmplifyAPI extends AmplifyAPIDart { MockAmplifyAPI({ - super.modelProvider, + super.options, }); @override @@ -72,7 +72,9 @@ void main() { await Amplify.reset(); await Amplify.addPlugin( // needed to fetch the schema from within the helper - MockAmplifyAPI(modelProvider: ModelProvider.instance), + MockAmplifyAPI( + options: APIPluginOptions(modelProvider: ModelProvider.instance), + ), ); }); const blogSelectionSet = 'id name createdAt updatedAt owner'; diff --git a/packages/api/amplify_api_dart/test/graphql_helpers_mtm_test.dart b/packages/api/amplify_api_dart/test/graphql_helpers_mtm_test.dart index 0082f4ce7e..6b60a119ba 100644 --- a/packages/api/amplify_api_dart/test/graphql_helpers_mtm_test.dart +++ b/packages/api/amplify_api_dart/test/graphql_helpers_mtm_test.dart @@ -13,7 +13,7 @@ final _deepEquals = const DeepCollectionEquality().equals; class MockAmplifyAPI extends AmplifyAPIDart { MockAmplifyAPI({ - super.modelProvider, + super.options, }); } @@ -23,7 +23,9 @@ void main() { await Amplify.reset(); await Amplify.addPlugin( // needed to fetch the schema from within the helper - MockAmplifyAPI(modelProvider: MtmModelProvider.instance), + MockAmplifyAPI( + options: APIPluginOptions(modelProvider: MtmModelProvider.instance), + ), ); }); diff --git a/packages/api/amplify_api_dart/test/graphql_helpers_test.dart b/packages/api/amplify_api_dart/test/graphql_helpers_test.dart index 64e286d174..a46686d500 100644 --- a/packages/api/amplify_api_dart/test/graphql_helpers_test.dart +++ b/packages/api/amplify_api_dart/test/graphql_helpers_test.dart @@ -20,7 +20,7 @@ const _exampleHeaders = {'testKey': 'testVal'}; class MockAmplifyAPI extends AmplifyAPIDart { MockAmplifyAPI({ - super.modelProvider, + super.options, }); @override @@ -50,7 +50,9 @@ void main() { await Amplify.reset(); await Amplify.addPlugin( // needed to fetch the schema from within the helper - MockAmplifyAPI(modelProvider: ModelProvider.instance), + MockAmplifyAPI( + options: APIPluginOptions(modelProvider: ModelProvider.instance), + ), ); }); const blogSelectionSet = 'id name createdAt updatedAt'; diff --git a/packages/api/amplify_api_dart/test/graphql_test.dart b/packages/api/amplify_api_dart/test/graphql_test.dart index aa3e567536..3ea1d99da8 100644 --- a/packages/api/amplify_api_dart/test/graphql_test.dart +++ b/packages/api/amplify_api_dart/test/graphql_test.dart @@ -138,9 +138,7 @@ WebSocketBloc? mockWebSocketBloc; class MockAmplifyAPI extends AmplifyAPIDart { MockAmplifyAPI({ - super.authProviders, - super.modelProvider, - super.baseHttpClient, + super.options, }); @override @@ -155,9 +153,11 @@ void main() { setUp(() async { final api = MockAmplifyAPI( - authProviders: [const CustomFunctionProvider()], - baseHttpClient: mockHttpClient, - modelProvider: ModelProvider.instance, + options: APIPluginOptions( + authProviders: [const CustomFunctionProvider()], + baseHttpClient: mockHttpClient, + modelProvider: ModelProvider.instance, + ), ); await Amplify.addPlugin(api); diff --git a/packages/api/amplify_api_dart/test/plugin_configuration_test.dart b/packages/api/amplify_api_dart/test/plugin_configuration_test.dart index 1acbfc8d32..d1c37016e8 100644 --- a/packages/api/amplify_api_dart/test/plugin_configuration_test.dart +++ b/packages/api/amplify_api_dart/test/plugin_configuration_test.dart @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import 'dart:convert'; -import 'package:amplify_api_dart/src/api_plugin_impl.dart'; +import 'package:amplify_api_dart/amplify_api_dart.dart'; import 'package:amplify_api_dart/src/graphql/providers/app_sync_api_key_auth_provider.dart'; import 'package:amplify_api_dart/src/graphql/providers/oidc_function_api_auth_provider.dart'; import 'package:amplify_core/amplify_core.dart'; @@ -99,8 +99,9 @@ void main() { test('should register an OIDC auth provider when passed to plugin', () async { - final plugin = - AmplifyAPIDart(authProviders: [const CustomOIDCProvider()]); + final plugin = AmplifyAPIDart( + options: const APIPluginOptions(authProviders: [CustomOIDCProvider()]), + ); await plugin.configure( authProviderRepo: authProviderRepo, config: config, @@ -117,8 +118,10 @@ void main() { test( 'should register a Lambda (function) auth provider when passed to plugin', () async { - final plugin = - AmplifyAPIDart(authProviders: [const CustomFunctionProvider()]); + final plugin = AmplifyAPIDart( + options: + const APIPluginOptions(authProviders: [CustomFunctionProvider()]), + ); await plugin.configure( authProviderRepo: authProviderRepo, config: config, @@ -135,7 +138,9 @@ void main() { test( 'should configure an HTTP client for GraphQL that authorizes with auth providers and adds user-agent', () async { - final plugin = AmplifyAPIDart(baseHttpClient: _mockGqlClient); + final plugin = AmplifyAPIDart( + options: APIPluginOptions(baseHttpClient: _mockGqlClient), + ); await plugin.configure( authProviderRepo: authProviderRepo, config: config, @@ -159,7 +164,9 @@ void main() { test( 'should configure an HTTP client for REST that authorizes with auth providers and adds user-agent', () async { - final plugin = AmplifyAPIDart(baseHttpClient: _mockRestClient); + final plugin = AmplifyAPIDart( + options: APIPluginOptions(baseHttpClient: _mockRestClient), + ); await plugin.configure( authProviderRepo: authProviderRepo, config: config, diff --git a/packages/api/amplify_api_dart/test/query_predicate_graphql_filter_test.dart b/packages/api/amplify_api_dart/test/query_predicate_graphql_filter_test.dart index 9a6a985896..23b0226113 100644 --- a/packages/api/amplify_api_dart/test/query_predicate_graphql_filter_test.dart +++ b/packages/api/amplify_api_dart/test/query_predicate_graphql_filter_test.dart @@ -1,6 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +import 'package:amplify_api_dart/amplify_api_dart.dart'; import 'package:amplify_core/amplify_core.dart'; import 'package:test/test.dart'; @@ -15,7 +16,9 @@ void main() { setUpAll(() async { await Amplify.addPlugin( // needed to fetch the schema from within the helper - MockAmplifyAPI(modelProvider: ModelProvider.instance), + MockAmplifyAPI( + options: APIPluginOptions(modelProvider: ModelProvider.instance), + ), ); }); diff --git a/packages/api/amplify_api_dart/test/rest_methods_test.dart b/packages/api/amplify_api_dart/test/rest_methods_test.dart index d5dae87041..82728ce9fe 100644 --- a/packages/api/amplify_api_dart/test/rest_methods_test.dart +++ b/packages/api/amplify_api_dart/test/rest_methods_test.dart @@ -31,7 +31,9 @@ final mockHttpClient = MockAWSHttpClient((request, _) async { void main() { setUpAll(() async { - final apiPlugin = AmplifyAPIDart(baseHttpClient: mockHttpClient); + final apiPlugin = AmplifyAPIDart( + options: APIPluginOptions(baseHttpClient: mockHttpClient), + ); // Register IAM auth provider like amplify_auth_cognito would do. final authProviderRepo = AmplifyAuthProviderRepository() ..registerAuthProvider( diff --git a/packages/test/amplify_auth_integration_test/lib/src/test_runner.dart b/packages/test/amplify_auth_integration_test/lib/src/test_runner.dart index 41a36a75bf..07dd503355 100644 --- a/packages/test/amplify_auth_integration_test/lib/src/test_runner.dart +++ b/packages/test/amplify_auth_integration_test/lib/src/test_runner.dart @@ -110,8 +110,10 @@ class AuthTestRunner { authPlugin, if (hasApiPlugin) AmplifyAPI( - authProviders: apiAuthProviders, - baseHttpClient: baseClient, + options: APIPluginOptions( + authProviders: apiAuthProviders, + baseHttpClient: baseClient, + ), ), ]); await Amplify.configure(config);