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(api): REST methods in dart with auth mode none #1783

Merged
merged 36 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
03113a3
chore!(api): migrate API category type definitions
May 26, 2022
5f57bdc
deprecate rest exception
May 27, 2022
774d48e
change deprecation message
May 27, 2022
5198f9b
change constuctor
May 27, 2022
a63f126
Merge branch 'next' into chore/migrate-api-interface
May 31, 2022
3f1472a
change types for qp
May 31, 2022
c2aa31b
cleanup
May 31, 2022
f23b403
more cleanup
May 31, 2022
6fa8ded
fix response headers, other PT changes
Jun 1, 2022
5e993cb
Merge branch 'next' into chore/migrate-api-interface
Jun 1, 2022
1c9e8cb
change example app
Jun 2, 2022
780c654
Merge branch 'next' into chore/migrate-api-interface
Jun 2, 2022
6034d6b
feat(api): REST API in dart, auth mode NONE
Jun 2, 2022
5b8a8bd
Update packages/amplify_core/lib/src/types/api/rest/http_payload.dart
ragingsquirrel3 Jun 9, 2022
cfb1c4e
address some comments
Jun 9, 2022
828bde5
Merge branch 'next' into chore/migrate-api-interface
Jun 9, 2022
f756a17
address some comments
Jun 10, 2022
7dfad32
fix tests
Jun 13, 2022
fbaf480
Merge branch 'chore/migrate-api-interface' into feat/api-rest2
Jun 13, 2022
3f08e79
correct merge
Jun 13, 2022
72f6e4b
address comments, json constructor
Jun 15, 2022
d983026
tweak json encoding
Jun 15, 2022
2ffa106
Merge branch 'next' into chore/migrate-api-interface
Jun 15, 2022
b66ef3a
Merge branch 'chore/migrate-api-interface' into feat/api-rest2
Jun 15, 2022
e5d4239
chore!(api): migrate API category type definitions (#1640)
Jun 15, 2022
909bb4f
Merge branch 'feat/api-next' into feat/api-rest2
Jun 21, 2022
c1fc4f5
add tests
Jun 21, 2022
66704de
correct qp types
Jun 21, 2022
8f9c6d8
chore(api): API Native Bridge for .addPlugin() (#1756)
Equartey Jun 23, 2022
da4d327
Merge branch 'feat/api-next' into feat/api-rest2
Jun 24, 2022
fb338eb
address PR comments
Jun 27, 2022
ff2f0ba
Merge branch 'feat/api-next' into feat/api-rest2
Jun 27, 2022
6d823db
correct some merge mistakes
Jun 27, 2022
24a9926
correct another merge error
Jun 27, 2022
50404c9
correct merge error
Jun 27, 2022
467a792
Update packages/api/amplify_api/lib/src/util.dart
ragingsquirrel3 Jun 27, 2022
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
1 change: 1 addition & 0 deletions packages/amplify/amplify_flutter/lib/src/hybrid_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AmplifyHybridImpl extends AmplifyClassImpl {
);
await Future.wait(
[
...API.plugins,
...Auth.plugins,
].map((p) => p.configure(config: amplifyConfig)),
eagerError: true,
Expand Down
134 changes: 83 additions & 51 deletions packages/amplify_core/lib/src/category/amplify_api_category.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* 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.
Expand All @@ -21,17 +21,13 @@ class APICategory extends AmplifyCategory<APIPluginInterface> {
Category get category => Category.api;

// ====== GraphQL =======
GraphQLOperation<T> query<T>({required GraphQLRequest<T> request}) {
return plugins.length == 1
? plugins[0].query(request: request)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<GraphQLResponse<T>> query<T>(
{required GraphQLRequest<T> request}) =>
defaultPlugin.query(request: request);

GraphQLOperation<T> mutate<T>({required GraphQLRequest<T> request}) {
return plugins.length == 1
? plugins[0].mutate(request: request)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<GraphQLResponse<T>> mutate<T>(
{required GraphQLRequest<T> request}) =>
defaultPlugin.mutate(request: request);

/// Subscribes to the given [request] and returns the stream of response events.
/// An optional [onEstablished] callback can be used to be alerted when the
Expand All @@ -42,52 +38,88 @@ class APICategory extends AmplifyCategory<APIPluginInterface> {
Stream<GraphQLResponse<T>> subscribe<T>(
GraphQLRequest<T> request, {
void Function()? onEstablished,
}) {
return plugins.length == 1
? plugins[0].subscribe(request, onEstablished: onEstablished)
: throw _pluginNotAddedException('Api');
}
}) =>
defaultPlugin.subscribe(request, onEstablished: onEstablished);

// ====== RestAPI ======
void cancelRequest(String cancelToken) {
return plugins.length == 1
? plugins[0].cancelRequest(cancelToken)
: throw _pluginNotAddedException('Api');
}

RestOperation get({required RestOptions restOptions}) {
return plugins.length == 1
? plugins[0].get(restOptions: restOptions)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<AWSStreamedHttpResponse> delete(
String path, {
Map<String, String>? headers,
HttpPayload? body,
Map<String, String>? queryParameters,
String? apiName,
}) =>
defaultPlugin.delete(
path,
headers: headers,
body: body,
apiName: apiName,
);

RestOperation put({required RestOptions restOptions}) {
return plugins.length == 1
? plugins[0].put(restOptions: restOptions)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<AWSStreamedHttpResponse> get(
String path, {
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) =>
defaultPlugin.get(
path,
headers: headers,
apiName: apiName,
);

RestOperation post({required RestOptions restOptions}) {
return plugins.length == 1
? plugins[0].post(restOptions: restOptions)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<AWSStreamedHttpResponse> head(
String path, {
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) =>
defaultPlugin.head(
path,
headers: headers,
apiName: apiName,
);

RestOperation delete({required RestOptions restOptions}) {
return plugins.length == 1
? plugins[0].delete(restOptions: restOptions)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<AWSStreamedHttpResponse> patch(
String path, {
Map<String, String>? headers,
HttpPayload? body,
Map<String, String>? queryParameters,
String? apiName,
}) =>
defaultPlugin.patch(
path,
headers: headers,
body: body,
apiName: apiName,
);

RestOperation head({required RestOptions restOptions}) {
return plugins.length == 1
? plugins[0].head(restOptions: restOptions)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<AWSStreamedHttpResponse> post(
String path, {
Map<String, String>? headers,
HttpPayload? body,
Map<String, String>? queryParameters,
String? apiName,
}) =>
defaultPlugin.post(
path,
headers: headers,
body: body,
apiName: apiName,
);

RestOperation patch({required RestOptions restOptions}) {
return plugins.length == 1
? plugins[0].patch(restOptions: restOptions)
: throw _pluginNotAddedException('Api');
}
CancelableOperation<AWSStreamedHttpResponse> put(
String path, {
Map<String, String>? headers,
HttpPayload? body,
Map<String, String>? queryParameters,
String? apiName,
}) =>
defaultPlugin.put(
path,
headers: headers,
body: body,
apiName: apiName,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ library amplify_interface;
import 'dart:async';

import 'package:amplify_core/amplify_core.dart';
import 'package:async/async.dart';
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* 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.
Expand All @@ -14,6 +14,7 @@
*/

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

abstract class APIPluginInterface extends AmplifyPluginInterface {
Expand All @@ -25,11 +26,13 @@ abstract class APIPluginInterface extends AmplifyPluginInterface {
ModelProviderInterface? get modelProvider => throw UnimplementedError();

// ====== GraphQL =======
GraphQLOperation<T> query<T>({required GraphQLRequest<T> request}) {
CancelableOperation<GraphQLResponse<T>> query<T>(
{required GraphQLRequest<T> request}) {
throw UnimplementedError('query() has not been implemented.');
}

GraphQLOperation<T> mutate<T>({required GraphQLRequest<T> request}) {
CancelableOperation<GraphQLResponse<T>> mutate<T>(
{required GraphQLRequest<T> request}) {
throw UnimplementedError('mutate() has not been implemented.');
}

Expand All @@ -50,31 +53,64 @@ abstract class APIPluginInterface extends AmplifyPluginInterface {
void registerAuthProvider(APIAuthProvider authProvider);

// ====== RestAPI ======
void cancelRequest(String cancelToken) {
throw UnimplementedError('cancelRequest has not been implemented.');
}

RestOperation get({required RestOptions restOptions}) {
throw UnimplementedError('get has not been implemented.');
CancelableOperation<AWSStreamedHttpResponse> delete(
String path, {
HttpPayload? body,
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) {
throw UnimplementedError('delete() has not been implemented');
}

RestOperation put({required RestOptions restOptions}) {
throw UnimplementedError('put has not been implemented.');
/// Uses Amplify configuration to authorize request to [path] and returns
/// [CancelableOperation] which resolves to standard HTTP
/// [Response](https://pub.dev/documentation/http/latest/http/Response-class.html).
CancelableOperation<AWSStreamedHttpResponse> get(
String path, {
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) {
throw UnimplementedError('get() has not been implemented');
}

RestOperation post({required RestOptions restOptions}) {
throw UnimplementedError('post has not been implemented.');
CancelableOperation<AWSStreamedHttpResponse> head(
String path, {
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) {
throw UnimplementedError('head() has not been implemented');
}

RestOperation delete({required RestOptions restOptions}) {
throw UnimplementedError('delete has not been implemented.');
CancelableOperation<AWSStreamedHttpResponse> patch(
String path, {
HttpPayload? body,
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) {
throw UnimplementedError('patch() has not been implemented');
}

RestOperation head({required RestOptions restOptions}) {
throw UnimplementedError('head has not been implemented.');
CancelableOperation<AWSStreamedHttpResponse> post(
String path, {
HttpPayload? body,
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) {
throw UnimplementedError('post() has not been implemented');
}

RestOperation patch({required RestOptions restOptions}) {
throw UnimplementedError('patch has not been implemented.');
CancelableOperation<AWSStreamedHttpResponse> put(
String path, {
HttpPayload? body,
Map<String, String>? headers,
Map<String, String>? queryParameters,
String? apiName,
}) {
throw UnimplementedError('put() has not been implemented');
}
}
2 changes: 1 addition & 1 deletion packages/amplify_core/lib/src/types/api/api_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export 'graphql/graphql_response.dart';
export 'graphql/graphql_response_error.dart';
export 'graphql/graphql_subscription_operation.dart';

export 'rest/http_payload.dart';
export 'rest/rest_exception.dart';
export 'rest/rest_operation.dart';
export 'rest/rest_options.dart';
export 'rest/rest_response.dart';

export 'types/pagination/paginated_model_type.dart';
export 'types/pagination/paginated_result.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ import 'package:amplify_core/amplify_core.dart';
/// Exception thrown from the API Category.
/// {@endtemplate}
class ApiException extends AmplifyException {
/// HTTP status of response, only available if error
@Deprecated(
'Use RestException instead to retrieve the HTTP response. Existing uses of '
'ApiException for handling REST errors can be safely replaced with RestException')
final int? httpStatusCode;

/// {@macro api_exception}
const ApiException(
String message, {
String? recoverySuggestion,
String? underlyingException,
this.httpStatusCode,
}) : super(
message,
recoverySuggestion: recoverySuggestion,
Expand All @@ -40,7 +33,6 @@ class ApiException extends AmplifyException {
/// Constructor for down casting an AmplifyException to this exception
ApiException._private(
AmplifyException exception,
this.httpStatusCode,
) : super(
exception.message,
recoverySuggestion: exception.recoverySuggestion,
Expand All @@ -57,7 +49,6 @@ class ApiException extends AmplifyException {
}
return ApiException._private(
AmplifyException.fromMap(serializedException),
statusCode,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* 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.
Expand All @@ -13,11 +13,14 @@
* permissions and limitations under the License.
*/

import 'package:amplify_core/amplify_core.dart';
import 'package:async/async.dart';

class GraphQLOperation<T> {
final Function cancel;
final Future<GraphQLResponse<T>> response;
import 'graphql_response.dart';

const GraphQLOperation({required this.response, required this.cancel});
/// Allows callers to synchronously get the unstreamed response with decoded body.
extension GraphQLOperation<T> on CancelableOperation<GraphQLResponse<T>> {
@Deprecated('use .value instead')
Future<GraphQLResponse<T>> get response {
return value;
}
}
Loading