15
15
16
16
import 'dart:async' ;
17
17
import 'dart:convert' ;
18
+
18
19
import 'package:amplify_flutter/amplify_flutter.dart' ;
19
- import 'package:graphql/client .dart' ;
20
+ import 'package:http/http .dart' as http ;
20
21
import 'package:stream_transform/stream_transform.dart' ;
21
22
22
23
import 'types/confirm_sign_up_response.dart' ;
23
24
24
25
/// A GraphQL document used by the [deleteUser] test utility method.
25
- const deleteDocument = '''mutation DeleteUser(\ $ Username: String!) {
26
- deleteUser(Username: \ $ Username) {
26
+ const deleteDocument = r '''mutation DeleteUser($Username: String!) {
27
+ deleteUser(Username: $Username) {
27
28
error
28
29
success
29
30
}
30
31
}''' ;
31
32
33
+ final _client = http.Client ();
34
+
35
+ Future <Map <String , Object ?>> _graphQL (
36
+ String document, {
37
+ Map <String , Object ?>? variables,
38
+ }) async {
39
+ final config = await Amplify .asyncConfig;
40
+ final api = config.api! .awsPlugin! .default$! ;
41
+ final response = await _client.post (
42
+ Uri .parse (api.endpoint).replace (path: '/graphql' ),
43
+ headers: {
44
+ 'x-api-key' : api.apiKey! ,
45
+ },
46
+ body: jsonEncode ({
47
+ 'query' : document,
48
+ if (variables != null ) 'variables' : variables,
49
+ }),
50
+ );
51
+ if (response.statusCode != 200 ) {
52
+ throw Exception ('${response .statusCode }: ${response .body }' );
53
+ }
54
+ final responseJson = jsonDecode (response.body) as Map <String , Object ?>;
55
+ final result = GraphQLResponse (
56
+ data: responseJson['data' ] as Map <String , Object ?>? ,
57
+ errors: (responseJson['errors' ] as List ? )
58
+ ? .cast <Map <String , Object ?>>()
59
+ .map (GraphQLResponseError .fromJson)
60
+ .toList () ??
61
+ const [],
62
+ );
63
+ if (result.errors.isNotEmpty) {
64
+ throw Exception (result.errors);
65
+ }
66
+ return result.data! ;
67
+ }
68
+
32
69
/// Deletes a Cognito user in backend infrastructure.
33
70
///
34
71
/// This method differs from the Auth.deleteUser API in that
35
72
/// an access token is not required.
36
73
Future <void > deleteUser (String username) async {
37
- final config = await Amplify .asyncConfig;
38
- final url = config.auth? .awsPlugin? .appSync? .default$? .apiUrl;
39
- final key = config.auth? .awsPlugin? .appSync? .default$? .apiKey;
40
- final client = GraphQLClient (
41
- cache: GraphQLCache (),
42
- link: HttpLink (
43
- url! ,
44
- defaultHeaders: {
45
- 'x-api-key' : key! ,
46
- },
47
- ),
74
+ final result = await _graphQL (
75
+ deleteDocument,
76
+ variables: {
77
+ 'Username' : username,
78
+ },
48
79
);
49
80
50
- final options = MutationOptions (
51
- document: gql (
52
- r'''
53
- mutation DeleteUser(
54
- $Username: String!
55
- ) {
56
- deleteUser(Username: $Username) {
57
- success
58
- exception
59
- }
60
- }
61
- ''' ,
62
- ),
63
- variables: < String , dynamic > {
64
- 'Username' : username,
65
- });
66
-
67
- final QueryResult result = await client.mutate (options);
68
- if (result.data? ['deleteUser' ]? ['exception' ] != null ) {
69
- throw Exception (result.data? ['deleteUser' ]? ['exception' ]);
81
+ final exception = (result['deleteUser' ] as Map ? )? ['exception' ] as String ? ;
82
+ if (exception != null ) {
83
+ throw Exception (exception);
70
84
}
71
85
}
72
86
73
87
/// Creates a Cognito user in backend infrastructure. This documention describes
74
88
/// how each parameter is expected to be used in the backend .
75
89
///
76
- /// Throws [GraphQLResponseErrors ] if present in the response.
90
+ /// Throws an [Exception ] if there are errors present in the response.
77
91
///
78
92
/// The [username] parameter can be plain text or a phone_number or email,
79
93
/// depending on the backend configuration.
80
- /// The [password] is used as the temporary password if [autoconfirm ] is true.
81
- /// The [autoconfirm ] flag will mark the user as confirmed and give them a permanent password.
82
- /// The [enableMFA ] flag will opt-in the user to using SMS MFA.
94
+ /// The [password] is used as the temporary password if [autoConfirm ] is true.
95
+ /// The [autoConfirm ] flag will mark the user as confirmed and give them a permanent password.
96
+ /// The [enableMfa ] flag will opt-in the user to using SMS MFA.
83
97
/// The [verifyAttributes] flag will verify the email and phone, and should be used
84
98
/// if tests need to bypass the verification step.
85
99
/// The [attributes] list passes additional attributes.
@@ -91,22 +105,8 @@ Future<void> adminCreateUser(
91
105
bool verifyAttributes = false ,
92
106
List <AuthUserAttribute > attributes = const [],
93
107
}) async {
94
- final config = await Amplify .asyncConfig;
95
- final url = config.auth? .awsPlugin? .appSync? .default$? .apiUrl;
96
- final key = config.auth? .awsPlugin? .appSync? .default$? .apiKey;
97
- final client = GraphQLClient (
98
- cache: GraphQLCache (),
99
- link: HttpLink (
100
- url! ,
101
- defaultHeaders: {
102
- 'x-api-key' : key! ,
103
- },
104
- ),
105
- );
106
-
107
- final options = MutationOptions (
108
- document: gql (
109
- r'''
108
+ final result = await _graphQL (
109
+ r'''
110
110
mutation AdminCreateUser(
111
111
$AutoConfirm: Boolean,
112
112
$Email:String,
@@ -133,48 +133,48 @@ Future<void> adminCreateUser(
133
133
}
134
134
}
135
135
''' ,
136
- ),
137
- variables: < String , dynamic > {
138
- 'AutoConfirm' : autoConfirm,
139
- 'Email' : attributes
140
- .firstWhere (
141
- (el) => el.userAttributeKey == CognitoUserAttributeKey .email,
142
- orElse: () => const AuthUserAttribute (
143
- userAttributeKey: CognitoUserAttributeKey .email,
144
-
145
- .value,
146
- 'EnabledMFA' : enableMfa,
147
- 'Given_Name' : attributes
148
- .firstWhere (
149
- (el) =>
150
- el.userAttributeKey == CognitoUserAttributeKey .givenName,
151
- orElse: () => const AuthUserAttribute (
152
- userAttributeKey: CognitoUserAttributeKey .givenName,
153
- value: 'default_given_name' ))
154
- .value,
155
- 'Name' : attributes
156
- .firstWhere (
157
- (el) => el.userAttributeKey == CognitoUserAttributeKey .name,
158
- orElse: () => const AuthUserAttribute (
159
- userAttributeKey: CognitoUserAttributeKey .name,
160
- value: 'default_name' ))
161
- .value,
162
- 'Password' : password,
163
- 'Phone_Number' : attributes
164
- .firstWhere (
165
- (el) =>
166
- el.userAttributeKey == CognitoUserAttributeKey .phoneNumber,
167
- orElse: () => const AuthUserAttribute (
168
- userAttributeKey: CognitoUserAttributeKey .phoneNumber,
169
- value: '+15555555' ))
170
- .value,
171
- 'Username' : username,
172
- 'VerifyAttributes' : verifyAttributes
173
- });
136
+ variables: {
137
+ 'AutoConfirm' : autoConfirm,
138
+ 'Email' : attributes
139
+ .firstWhere (
140
+ (el) => el.userAttributeKey == CognitoUserAttributeKey .email,
141
+ orElse: () => const AuthUserAttribute (
142
+ userAttributeKey: CognitoUserAttributeKey .email,
143
+
144
+ .value,
145
+ 'EnabledMFA' : enableMfa,
146
+ 'Given_Name' : attributes
147
+ .firstWhere (
148
+ (el) => el.userAttributeKey == CognitoUserAttributeKey .givenName,
149
+ orElse: () => const AuthUserAttribute (
150
+ userAttributeKey: CognitoUserAttributeKey .givenName,
151
+ value: 'default_given_name' ))
152
+ .value,
153
+ 'Name' : attributes
154
+ .firstWhere (
155
+ (el) => el.userAttributeKey == CognitoUserAttributeKey .name,
156
+ orElse: () => const AuthUserAttribute (
157
+ userAttributeKey: CognitoUserAttributeKey .name,
158
+ value: 'default_name' ))
159
+ .value,
160
+ 'Password' : password,
161
+ 'Phone_Number' : attributes
162
+ .firstWhere (
163
+ (el) =>
164
+ el.userAttributeKey == CognitoUserAttributeKey .phoneNumber,
165
+ orElse: () => const AuthUserAttribute (
166
+ userAttributeKey: CognitoUserAttributeKey .phoneNumber,
167
+ value: '+15555555' ))
168
+ .value,
169
+ 'Username' : username,
170
+ 'VerifyAttributes' : verifyAttributes
171
+ },
172
+ );
174
173
175
- final QueryResult result = await client.mutate (options);
176
- if (result.data? ['adminCreateUser' ]? ['exception' ] != null ) {
177
- throw Exception (result.data? ['adminCreateUser' ]? ['exception' ]);
174
+ final exception =
175
+ (result['adminCreateUser' ] as Map ? )? ['exception' ] as String ? ;
176
+ if (exception != null ) {
177
+ throw Exception (exception);
178
178
}
179
179
}
180
180
0 commit comments