Skip to content

Commit

Permalink
chore!(api): migrate API category type definitions (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Sheppard committed Sep 14, 2022
1 parent 60210e7 commit 0759726
Show file tree
Hide file tree
Showing 19 changed files with 611 additions and 425 deletions.
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,24 +19,16 @@ 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(
super.message, {
super.recoverySuggestion,
super.underlyingException,
this.httpStatusCode,
});

/// Constructor for down casting an AmplifyException to this exception
ApiException._private(
AmplifyException exception,
this.httpStatusCode,
) : super(
exception.message,
recoverySuggestion: exception.recoverySuggestion,
Expand All @@ -53,7 +45,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

0 comments on commit 0759726

Please sign in to comment.