-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): API Native Bridge for .addPlugin() (#1756)
- Loading branch information
Showing
17 changed files
with
526 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.PHONY: pigeons | ||
pigeons: | ||
flutter pub run pigeon --input pigeons/native_api_plugin.dart | ||
flutter format --fix lib/src/native_api_plugin.dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
library amplify_api; | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:amplify_api/amplify_api.dart'; | ||
import 'package:amplify_api/src/native_api_plugin.dart'; | ||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
/// {@template amplify_api.amplify_api_dart} | ||
/// The AWS implementation of the Amplify API category. | ||
/// {@endtemplate} | ||
class AmplifyAPIDart extends AmplifyAPI { | ||
late final AWSApiPluginConfig _apiConfig; | ||
|
||
/// The registered [APIAuthProvider] instances. | ||
final Map<APIAuthorizationType, APIAuthProvider> _authProviders = {}; | ||
|
||
/// {@macro amplify_api.amplify_api_dart} | ||
AmplifyAPIDart({ | ||
List<APIAuthProvider> authProviders = const [], | ||
this.modelProvider, | ||
}) : super.protected() { | ||
authProviders.forEach(registerAuthProvider); | ||
} | ||
|
||
@override | ||
Future<void> configure({AmplifyConfig? config}) async { | ||
final apiConfig = config?.api?.awsPlugin; | ||
if (apiConfig == null) { | ||
throw const ApiException('No AWS API config found', | ||
recoverySuggestion: 'Add API from the Amplify CLI. See ' | ||
'https://docs.amplify.aws/lib/graphqlapi/getting-started/q/platform/flutter/#configure-api'); | ||
} | ||
_apiConfig = apiConfig; | ||
} | ||
|
||
@override | ||
Future<void> addPlugin() async { | ||
if (zIsWeb || !(Platform.isAndroid || Platform.isIOS)) { | ||
return; | ||
} | ||
|
||
final nativeBridge = NativeApiBridge(); | ||
try { | ||
final authProvidersList = | ||
_authProviders.keys.map((key) => key.rawValue).toList(); | ||
await nativeBridge.addPlugin(authProvidersList); | ||
} on PlatformException catch (e) { | ||
if (e.code == 'AmplifyAlreadyConfiguredException') { | ||
throw const AmplifyAlreadyConfiguredException( | ||
AmplifyExceptionMessages.alreadyConfiguredDefaultMessage, | ||
recoverySuggestion: | ||
AmplifyExceptionMessages.alreadyConfiguredDefaultSuggestion); | ||
} | ||
throw AmplifyException.fromMap((e.details as Map).cast()); | ||
} | ||
} | ||
|
||
@override | ||
final ModelProviderInterface? modelProvider; | ||
|
||
@override | ||
void registerAuthProvider(APIAuthProvider authProvider) { | ||
_authProviders[authProvider.type] = authProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// Autogenerated from Pigeon (v3.1.5), do not edit directly. | ||
// See also: https://pub.dev/packages/pigeon | ||
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name | ||
// @dart = 2.12 | ||
import 'dart:async'; | ||
import 'dart:typed_data' show Uint8List, Int32List, Int64List, Float64List; | ||
|
||
import 'package:flutter/foundation.dart' show WriteBuffer, ReadBuffer; | ||
import 'package:flutter/services.dart'; | ||
|
||
class _NativeApiBridgeCodec extends StandardMessageCodec { | ||
const _NativeApiBridgeCodec(); | ||
} | ||
|
||
class NativeApiBridge { | ||
/// Constructor for [NativeApiBridge]. The [binaryMessenger] named argument is | ||
/// available for dependency injection. If it is left null, the default | ||
/// BinaryMessenger will be used which routes to the host platform. | ||
NativeApiBridge({BinaryMessenger? binaryMessenger}) | ||
: _binaryMessenger = binaryMessenger; | ||
|
||
final BinaryMessenger? _binaryMessenger; | ||
|
||
static const MessageCodec<Object?> codec = _NativeApiBridgeCodec(); | ||
|
||
Future<void> addPlugin(List<String?> arg_authProvidersList) async { | ||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>( | ||
'dev.flutter.pigeon.NativeApiBridge.addPlugin', codec, | ||
binaryMessenger: _binaryMessenger); | ||
final Map<Object?, Object?>? replyMap = await channel | ||
.send(<Object?>[arg_authProvidersList]) as Map<Object?, Object?>?; | ||
if (replyMap == null) { | ||
throw PlatformException( | ||
code: 'channel-error', | ||
message: 'Unable to establish connection on channel.', | ||
); | ||
} else if (replyMap['error'] != null) { | ||
final Map<Object?, Object?> error = | ||
(replyMap['error'] as Map<Object?, Object?>?)!; | ||
throw PlatformException( | ||
code: (error['code'] as String?)!, | ||
message: error['message'] as String?, | ||
details: error['details'], | ||
); | ||
} else { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
|
||
// ignore_for_file: avoid_positional_boolean_parameters | ||
|
||
@ConfigurePigeon( | ||
PigeonOptions( | ||
copyrightHeader: '../../../tool/license.txt', | ||
dartOptions: DartOptions(), | ||
dartOut: 'lib/src/native_Api_plugin.dart', | ||
javaOptions: JavaOptions( | ||
className: 'NativeApiPluginBindings', | ||
package: 'com.amazonaws.amplify.amplify_api', | ||
), | ||
javaOut: | ||
'../amplify_api_android/android/src/main/kotlin/com/amazonaws/amplify/amplify_api/NativeApiPluginBindings.java', | ||
objcOptions: ObjcOptions( | ||
header: 'NativeApiPlugin.h', | ||
), | ||
objcHeaderOut: '../amplify_api_ios/ios/Classes/NativeApiPlugin.h', | ||
objcSourceOut: '../amplify_api_ios/ios/Classes/NativeApiPlugin.m', | ||
), | ||
) | ||
library native_api_plugin; | ||
|
||
import 'package:pigeon/pigeon.dart'; | ||
|
||
@HostApi() | ||
abstract class NativeApiBridge { | ||
void addPlugin(List<String> authProvidersList); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.