@@ -20,21 +20,33 @@ import android.os.Handler
20
20
import android.os.Looper
21
21
import androidx.annotation.NonNull
22
22
import androidx.annotation.VisibleForTesting
23
+ import com.amazonaws.amplify.amplify_api.auth.FlutterAuthProviders
23
24
import com.amazonaws.amplify.amplify_api.rest_api.FlutterRestApi
25
+ import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedUnrecognizedError
26
+ import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException
27
+ import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.postExceptionToFlutterChannel
28
+ import com.amplifyframework.api.ApiException
24
29
import com.amplifyframework.api.aws.AWSApiPlugin
30
+ import com.amplifyframework.api.aws.AuthorizationType
25
31
import com.amplifyframework.core.Amplify
26
32
import io.flutter.embedding.engine.plugins.FlutterPlugin
27
33
import io.flutter.plugin.common.EventChannel
28
34
import io.flutter.plugin.common.MethodCall
29
35
import io.flutter.plugin.common.MethodChannel
30
36
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
31
37
import io.flutter.plugin.common.MethodChannel.Result
32
- import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedUnrecognizedError
33
- import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException
34
- import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.postExceptionToFlutterChannel
35
38
36
39
/* * AmplifyApiPlugin */
37
40
class AmplifyApiPlugin : FlutterPlugin , MethodCallHandler {
41
+ companion object {
42
+ /* *
43
+ * Thrown when [tokenType] is used but is not a valid [AuthorizationType].
44
+ */
45
+ private fun invalidTokenType (tokenType : String? = null) = ApiException .ApiAuthException (
46
+ " Invalid arguments" ,
47
+ " Invalid token type: $tokenType "
48
+ )
49
+ }
38
50
39
51
private lateinit var channel: MethodChannel
40
52
private lateinit var eventchannel: EventChannel
@@ -50,26 +62,35 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
50
62
constructor (eventHandler: GraphQLSubscriptionStreamHandler ) {
51
63
graphqlSubscriptionStreamHandler = eventHandler
52
64
}
65
+
53
66
private val handler = Handler (Looper .getMainLooper())
54
67
55
68
override fun onAttachedToEngine (@NonNull flutterPluginBinding : FlutterPlugin .FlutterPluginBinding ) {
56
69
channel = MethodChannel (flutterPluginBinding.binaryMessenger, " com.amazonaws.amplify/api" )
57
70
channel.setMethodCallHandler(this )
58
- eventchannel = EventChannel (flutterPluginBinding.binaryMessenger, " com.amazonaws.amplify/api_observe_events" )
71
+ eventchannel = EventChannel (
72
+ flutterPluginBinding.binaryMessenger,
73
+ " com.amazonaws.amplify/api_observe_events"
74
+ )
59
75
eventchannel.setStreamHandler(graphqlSubscriptionStreamHandler)
60
76
context = flutterPluginBinding.applicationContext
61
77
}
62
78
79
+ @Suppress(" UNCHECKED_CAST" )
63
80
override fun onMethodCall (call : MethodCall , result : Result ) {
64
- var methodName = call.method
81
+ val methodName = call.method
65
82
66
- if (methodName == " cancel" ){
83
+ if (methodName == " cancel" ) {
67
84
onCancel(result, (call.arguments as String ))
68
85
return
69
- }
70
- else if (methodName == " addPlugin" ){
86
+ } else if (methodName == " addPlugin" ) {
71
87
try {
72
- Amplify .addPlugin(AWSApiPlugin ())
88
+ Amplify .addPlugin(
89
+ AWSApiPlugin
90
+ .builder()
91
+ .apiAuthProviders(FlutterAuthProviders .factory)
92
+ .build()
93
+ )
73
94
LOG .info(" Added API plugin" )
74
95
result.success(null )
75
96
} catch (e: Exception ) {
@@ -79,7 +100,22 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
79
100
}
80
101
81
102
try {
82
- var arguments : Map <String , Any > = call.arguments as Map <String ,Any >
103
+ val arguments: Map <String , Any > = call.arguments as Map <String , Any >
104
+
105
+ // Update tokens if included with request
106
+ val tokens = arguments[" tokens" ] as ? List <* >
107
+ if (tokens != null && tokens.isNotEmpty()) {
108
+ for (authToken in tokens as List <Map <* , * >>) {
109
+ val token = authToken[" token" ] as ? String?
110
+ val tokenType = authToken[" type" ] as ? String ? : throw invalidTokenType()
111
+ val authType: AuthorizationType = try {
112
+ AuthorizationType .from(tokenType)
113
+ } catch (e: Exception ) {
114
+ throw invalidTokenType(tokenType)
115
+ }
116
+ FlutterAuthProviders .setToken(authType, token)
117
+ }
118
+ }
83
119
84
120
when (call.method) {
85
121
" get" -> FlutterRestApi .get(result, arguments)
@@ -90,28 +126,36 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
90
126
" patch" -> FlutterRestApi .patch(result, arguments)
91
127
" query" -> FlutterGraphQLApi .query(result, arguments)
92
128
" mutate" -> FlutterGraphQLApi .mutate(result, arguments)
93
- " subscribe" -> FlutterGraphQLApi .subscribe(result, arguments, graphqlSubscriptionStreamHandler)
129
+ " subscribe" -> FlutterGraphQLApi .subscribe(
130
+ result,
131
+ arguments,
132
+ graphqlSubscriptionStreamHandler
133
+ )
94
134
else -> result.notImplemented()
95
135
}
96
136
} catch (e: Exception ) {
97
137
handler.post {
98
- postExceptionToFlutterChannel(result, " ApiException" ,
99
- createSerializedUnrecognizedError(e))
138
+ postExceptionToFlutterChannel(
139
+ result, " ApiException" ,
140
+ createSerializedUnrecognizedError(e)
141
+ )
100
142
}
101
143
}
102
144
}
103
145
104
146
fun onCancel (
105
- flutterResult : Result ,
106
- cancelToken : String ) {
107
- if (OperationsManager .containsOperation(cancelToken)) {
147
+ flutterResult : Result ,
148
+ cancelToken : String
149
+ ) {
150
+ if (OperationsManager .containsOperation(cancelToken)) {
108
151
OperationsManager .cancelOperation(cancelToken)
109
152
flutterResult.success(" Operation Canceled" )
110
153
} else {
111
154
flutterResult.error(
112
- " AmplifyAPI-CancelError" ,
113
- " The Operation may have already been completed or expired and cannot be canceled anymore" ,
114
- " Operation does not exist" )
155
+ " AmplifyAPI-CancelError" ,
156
+ " The Operation may have already been completed or expired and cannot be canceled anymore" ,
157
+ " Operation does not exist"
158
+ )
115
159
}
116
160
}
117
161
0 commit comments