-
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.
feat(api): REST methods in dart with auth mode none (#1783)
- Loading branch information
Travis Sheppard
committed
Sep 9, 2022
1 parent
4941901
commit e780b22
Showing
10 changed files
with
554 additions
and
9 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
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,74 @@ | ||
// 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. | ||
|
||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:collection/collection.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
const _slash = '/'; | ||
|
||
@internal | ||
class EndpointConfig with AWSEquatable<EndpointConfig> { | ||
const EndpointConfig(this.name, this.config); | ||
|
||
final String name; | ||
final AWSApiConfig config; | ||
|
||
@override | ||
List<Object?> get props => [name, config]; | ||
|
||
/// Gets the host with environment path prefix from Amplify config and combines | ||
/// with [path] and [queryParameters] to return a full [Uri]. | ||
Uri getUri(String path, Map<String, dynamic>? queryParameters) { | ||
final parsed = Uri.parse(config.endpoint); | ||
// Remove leading slashes which are suggested in public documentation. | ||
// https://docs.amplify.aws/lib/restapi/getting-started/q/platform/flutter/#make-a-post-request | ||
if (path.startsWith(_slash)) { | ||
path = path.substring(1); | ||
} | ||
// Avoid URI-encoding slashes in path from caller. | ||
final pathSegmentsFromPath = path.split(_slash); | ||
return parsed.replace(pathSegments: [ | ||
...parsed.pathSegments, | ||
...pathSegmentsFromPath, | ||
], queryParameters: queryParameters); | ||
} | ||
} | ||
|
||
@internal | ||
extension AWSApiPluginConfigHelpers on AWSApiPluginConfig { | ||
EndpointConfig getEndpoint({ | ||
required EndpointType type, | ||
String? apiName, | ||
}) { | ||
final typeConfigs = | ||
entries.where((config) => config.value.endpointType == type); | ||
if (apiName != null) { | ||
final config = typeConfigs.firstWhere( | ||
(config) => config.key == apiName, | ||
orElse: () => throw ApiException( | ||
'No API endpoint found matching apiName $apiName.', | ||
), | ||
); | ||
return EndpointConfig(config.key, config.value); | ||
} | ||
final onlyConfig = typeConfigs.singleOrNull; | ||
if (onlyConfig == null) { | ||
throw const ApiException( | ||
'Multiple API endpoints defined. Pass apiName parameter to specify ' | ||
'which one to use.', | ||
); | ||
} | ||
return EndpointConfig(onlyConfig.key, onlyConfig.value); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/api/amplify_api/lib/src/amplify_authorization_rest_client.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Implementation of http [http.Client] that authorizes HTTP requests with | ||
/// Amplify. | ||
@internal | ||
class AmplifyAuthorizationRestClient extends http.BaseClient | ||
implements Closeable { | ||
/// Determines how requests with this client are authorized. | ||
final AWSApiConfig endpointConfig; | ||
final http.Client _baseClient; | ||
final bool _useDefaultBaseClient; | ||
|
||
/// Provide an [AWSApiConfig] which will determine how requests from this | ||
/// client are authorized. | ||
AmplifyAuthorizationRestClient({ | ||
required this.endpointConfig, | ||
http.Client? baseClient, | ||
}) : _useDefaultBaseClient = baseClient == null, | ||
_baseClient = baseClient ?? http.Client(); | ||
|
||
/// Implementation of [send] that authorizes any request without "Authorization" | ||
/// header already set. | ||
@override | ||
Future<http.StreamedResponse> send(http.BaseRequest request) async => | ||
_baseClient.send(_authorizeRequest(request)); | ||
|
||
@override | ||
void close() { | ||
if (_useDefaultBaseClient) _baseClient.close(); | ||
} | ||
|
||
http.BaseRequest _authorizeRequest(http.BaseRequest request) { | ||
if (!request.headers.containsKey(AWSHeaders.authorization) && | ||
endpointConfig.authorizationType != APIAuthorizationType.none) { | ||
// ignore: todo | ||
// TODO: Use auth providers from core to transform the request. | ||
} | ||
return request; | ||
} | ||
} |
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.