Skip to content

Commit ad367f8

Browse files
committed
feat(firehose): integrate amplify_foundation_dart v3 types
- Replace amplify_core credential/exception types with foundation v3 equivalents - Exceptions extend AmplifyException from amplify_foundation_dart - Barrel exports re-export AWSCredentials, AWSCredentialsProvider, StaticCredentials, TemporaryCredentials, Logger - Add _CredentialsProviderAdapter in sdk_bridge to convert foundation provider to amplify_core for Smithy SDK - Add V2CredentialsProviderBridge for V2→V3 credential migration - Options include optional Logger parameter - Update tests and mocks for v3 types
1 parent 3efeb68 commit ad367f8

File tree

12 files changed

+226
-87
lines changed

12 files changed

+226
-87
lines changed

packages/kinesis/aws_amazon_firehose/lib/aws_amazon_firehose.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
/// Amplify Amazon Data Firehose client for Dart.
55
library;
66

7-
// TODO(v3): Replace with V3 credentials provider type.
8-
export 'package:amplify_core/amplify_core.dart'
9-
show AWSCredentials, AWSCredentialsProvider;
7+
export 'package:amplify_foundation_dart/amplify_foundation_dart.dart'
8+
show
9+
AWSCredentials,
10+
AWSCredentialsProvider,
11+
StaticCredentials,
12+
TemporaryCredentials,
13+
Logger;
1014

1115
// Options
1216
export 'src/amazon_data_firehose_options.dart';
1317
// Main client
1418
export 'src/amplify_firehose_client.dart';
19+
// V2 → V3 credentials bridge
20+
export 'src/credentials/credentials_bridge.dart';
1521
// Exceptions
1622
export 'src/exception/amplify_firehose_exception.dart';
1723
// Flush strategies

packages/kinesis/aws_amazon_firehose/lib/src/amazon_data_firehose_options.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'package:amplify_foundation_dart/amplify_foundation_dart.dart';
45
import 'package:aws_amazon_firehose/src/flush_strategy/flush_strategy.dart';
56

6-
// TODO(v3): Import V3 Logger type once available.
7-
// import 'package:amplify_v3_core/amplify_v3_core.dart' show Logger;
8-
97
/// {@template aws_amazon_firehose.amplify_firehose_client_options}
108
/// Configuration options for [AmplifyFirehoseClient].
119
///
@@ -27,8 +25,7 @@ class AmplifyFirehoseClientOptions {
2725
this.maxRecords = 500,
2826
this.maxRetries = 5,
2927
this.flushStrategy = const AmazonDataFirehoseInterval(),
30-
// TODO(v3): Add logger parameter once V3 Logger type is available.
31-
// this.logger,
28+
this.logger,
3229
});
3330

3431
/// Maximum size of the local cache in bytes.
@@ -56,9 +53,8 @@ class AmplifyFirehoseClientOptions {
5653
/// Defaults to [AmazonDataFirehoseInterval] with a 30-second interval.
5754
final AmazonDataFirehoseFlushStrategy flushStrategy;
5855

59-
// TODO(v3): Uncomment once V3 Logger type is available.
60-
// /// Optional logger for debug and error output.
61-
// final Logger? logger;
56+
/// Optional logger for debug and error output.
57+
final Logger? logger;
6258
}
6359

6460
/// Deprecated: Use [AmplifyFirehoseClientOptions] instead.

packages/kinesis/aws_amazon_firehose/lib/src/amplify_firehose_client.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import 'dart:typed_data';
55

6-
import 'package:amplify_core/amplify_core.dart';
6+
import 'package:amplify_foundation_dart/amplify_foundation_dart.dart';
77
import 'package:aws_amazon_firehose/src/amazon_data_firehose_options.dart';
88
import 'package:aws_amazon_firehose/src/db/firehose_record_database.dart';
99
import 'package:aws_amazon_firehose/src/impl/auto_flush_scheduler.dart';
@@ -15,9 +15,6 @@ import 'package:aws_amazon_firehose/src/model/clear_cache_data.dart';
1515
import 'package:aws_amazon_firehose/src/model/flush_data.dart';
1616
import 'package:aws_amazon_firehose/src/sdk/firehose.dart';
1717

18-
// TODO(v3): Update credentialsProvider type to V3
19-
// `AWSCredentialsProvider<AWSCredentials>` once available.
20-
2118
/// {@template aws_amazon_firehose.amplify_firehose_client}
2219
/// Client for recording and streaming data to Amazon Data Firehose.
2320
///
@@ -50,7 +47,7 @@ class AmplifyFirehoseClient {
5047
/// {@macro aws_amazon_firehose.amplify_firehose_client}
5148
AmplifyFirehoseClient({
5249
required String region,
53-
required AWSCredentialsProvider credentialsProvider,
50+
required AWSCredentialsProvider<AWSCredentials> credentialsProvider,
5451
AmplifyFirehoseClientOptions options = const AmplifyFirehoseClientOptions(),
5552
String? storagePath,
5653
}) : this._internal(
@@ -62,7 +59,7 @@ class AmplifyFirehoseClient {
6259

6360
AmplifyFirehoseClient._internal({
6461
required String region,
65-
required AWSCredentialsProvider credentialsProvider,
62+
required AWSCredentialsProvider<AWSCredentials> credentialsProvider,
6663
required AmplifyFirehoseClientOptions options,
6764
required String storagePath,
6865
}) : _region = region,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:amplify_core/amplify_core.dart' as v2;
5+
import 'package:amplify_foundation_dart/amplify_foundation_dart.dart' as v3;
6+
7+
/// {@template aws_amazon_firehose.v2_credentials_provider_bridge}
8+
/// Bridges a V2 [v2.AWSCredentialsProvider] (from `amplify_core`) to a V3
9+
/// [v3.AWSCredentialsProvider] (from `amplify_foundation_dart`).
10+
///
11+
/// This allows users who obtain credentials from the existing Amplify Auth
12+
/// plugin (V2) to pass them directly to the Firehose client without
13+
/// boilerplate.
14+
///
15+
/// ## Usage
16+
///
17+
/// ```dart
18+
/// import 'package:amplify_core/amplify_core.dart' as v2;
19+
/// import 'package:aws_amazon_firehose/aws_amazon_firehose.dart';
20+
///
21+
/// // Obtain V2 credentials from Amplify Auth
22+
/// final session = await Amplify.Auth.fetchAuthSession();
23+
/// final v2Provider = v2.AWSCredentialsProvider(session.credentials);
24+
///
25+
/// // Bridge to V3 and pass to the Firehose client
26+
/// final client = AmplifyFirehoseClient(
27+
/// region: 'us-east-1',
28+
/// credentialsProvider: V2CredentialsProviderBridge(v2Provider),
29+
/// );
30+
/// ```
31+
/// {@endtemplate}
32+
class V2CredentialsProviderBridge
33+
implements v3.AWSCredentialsProvider<v3.AWSCredentials> {
34+
/// {@macro aws_amazon_firehose.v2_credentials_provider_bridge}
35+
const V2CredentialsProviderBridge(this._v2Provider);
36+
37+
final v2.AWSCredentialsProvider _v2Provider;
38+
39+
@override
40+
Future<v3.AWSCredentials> resolve() async {
41+
final creds = await _v2Provider.retrieve();
42+
if (creds.sessionToken != null) {
43+
return v3.TemporaryCredentials(
44+
creds.accessKeyId,
45+
creds.secretAccessKey,
46+
creds.sessionToken!,
47+
creds.expiration ?? DateTime.now().add(const Duration(hours: 1)),
48+
);
49+
}
50+
return v3.StaticCredentials(creds.accessKeyId, creds.secretAccessKey);
51+
}
52+
}
Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,105 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// TODO(v3): Replace with `extends AmplifyException` once V3 foundational
5-
// library is available. For now we implement Exception directly.
4+
import 'package:amplify_foundation_dart/amplify_foundation_dart.dart';
65

6+
/// {@template aws_amazon_firehose.amplify_firehose_exception}
77
/// Base exception for Amplify Firehose errors.
8-
sealed class AmplifyFirehoseException implements Exception {
9-
const AmplifyFirehoseException(this.message, {this.recoverySuggestion, this.underlyingException});
10-
final String message;
11-
final String? recoverySuggestion;
12-
final Object? underlyingException;
8+
/// {@endtemplate}
9+
sealed class AmplifyFirehoseException extends AmplifyException {
10+
/// {@macro aws_amazon_firehose.amplify_firehose_exception}
11+
AmplifyFirehoseException({
12+
required super.message,
13+
required super.recoverySuggestion,
14+
super.cause,
15+
});
16+
1317
@override
1418
String toString() {
15-
final buf = StringBuffer('$runtimeType: $message');
16-
if (recoverySuggestion != null) buf.write('\nRecovery suggestion: $recoverySuggestion');
17-
if (underlyingException != null) buf.write('\nCaused by: $underlyingException');
19+
final buf = StringBuffer('AmplifyFirehoseException: $message');
20+
if (recoverySuggestion.isNotEmpty) {
21+
buf.write('\nRecovery suggestion: $recoverySuggestion');
22+
}
23+
if (cause != null) buf.write('\nCaused by: $cause');
1824
return buf.toString();
1925
}
2026
}
2127

28+
/// {@template aws_amazon_firehose.firehose_storage_exception}
2229
/// Thrown when a local cache/database error occurs.
30+
/// {@endtemplate}
2331
class FirehoseStorageException extends AmplifyFirehoseException {
24-
const FirehoseStorageException(super.message, {super.underlyingException}) : super(recoverySuggestion: 'Try clearing the cache or reinitializing.');
32+
/// {@macro aws_amazon_firehose.firehose_storage_exception}
33+
FirehoseStorageException(String message, {Object? cause})
34+
: super(
35+
message: message,
36+
recoverySuggestion: 'Try clearing the cache or reinitializing.',
37+
cause: cause,
38+
);
2539
}
2640

41+
/// {@template aws_amazon_firehose.firehose_service_exception}
2742
/// Thrown when a Firehose SDK/API error occurs. Inspect [sdkException] for details.
43+
/// {@endtemplate}
2844
class FirehoseServiceException extends AmplifyFirehoseException {
29-
const FirehoseServiceException(super.message, {this.sdkException, super.underlyingException}) : super(recoverySuggestion: 'Check sdkException for details.');
45+
/// {@macro aws_amazon_firehose.firehose_service_exception}
46+
FirehoseServiceException(String message, {this.sdkException, Object? cause})
47+
: super(
48+
message: message,
49+
recoverySuggestion: 'Check sdkException for details.',
50+
cause: cause,
51+
);
52+
53+
/// The underlying SDK exception, if any.
3054
final Object? sdkException;
3155
}
3256

57+
/// {@template aws_amazon_firehose.firehose_limit_exceeded_exception}
3358
/// Thrown when the local cache is full.
59+
/// {@endtemplate}
3460
class FirehoseLimitExceededException extends AmplifyFirehoseException {
35-
const FirehoseLimitExceededException() : super('Cache is full', recoverySuggestion: 'Call flush() or clearCache().');
61+
/// {@macro aws_amazon_firehose.firehose_limit_exceeded_exception}
62+
FirehoseLimitExceededException()
63+
: super(
64+
message: 'Cache is full',
65+
recoverySuggestion: 'Call flush() or clearCache().',
66+
);
3667
}
3768

69+
/// {@template aws_amazon_firehose.firehose_unknown_exception}
3870
/// Catch-all for unexpected errors.
71+
/// {@endtemplate}
3972
class FirehoseUnknownException extends AmplifyFirehoseException {
40-
const FirehoseUnknownException(super.message, {super.underlyingException}) : super(recoverySuggestion: 'Unexpected error. Please file a bug.');
73+
/// {@macro aws_amazon_firehose.firehose_unknown_exception}
74+
FirehoseUnknownException(String message, {Object? cause})
75+
: super(
76+
message: message,
77+
recoverySuggestion: 'Unexpected error. Please file a bug.',
78+
cause: cause,
79+
);
4180
}
4281

82+
/// {@template aws_amazon_firehose.firehose_network_exception}
4383
/// Thrown when a network error prevents communication with Firehose.
84+
/// {@endtemplate}
4485
class FirehoseNetworkException extends AmplifyFirehoseException {
45-
const FirehoseNetworkException(super.message, {super.underlyingException}) : super(recoverySuggestion: 'Check network connectivity and try again.');
86+
/// {@macro aws_amazon_firehose.firehose_network_exception}
87+
FirehoseNetworkException(String message, {Object? cause})
88+
: super(
89+
message: message,
90+
recoverySuggestion: 'Check network connectivity and try again.',
91+
cause: cause,
92+
);
4693
}
4794

95+
/// {@template aws_amazon_firehose.client_closed_exception}
4896
/// Thrown when an operation is attempted on a closed client.
97+
/// {@endtemplate}
4998
class ClientClosedException extends AmplifyFirehoseException {
50-
const ClientClosedException() : super('Client has been closed', recoverySuggestion: 'Create a new AmplifyFirehoseClient instance.');
99+
/// {@macro aws_amazon_firehose.client_closed_exception}
100+
ClientClosedException()
101+
: super(
102+
message: 'Client has been closed',
103+
recoverySuggestion: 'Create a new AmplifyFirehoseClient instance.',
104+
);
51105
}

packages/kinesis/aws_amazon_firehose/lib/src/impl/firehose_sender.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import 'dart:io';
55
import 'dart:typed_data';
66

7-
import 'package:amplify_core/amplify_core.dart';
7+
import 'package:amplify_foundation_dart/amplify_foundation_dart.dart'
8+
as foundation;
89
import 'package:aws_amazon_firehose/src/exception/amplify_firehose_exception.dart';
910
import 'package:aws_amazon_firehose/src/sdk/firehose.dart';
1011
import 'package:aws_amazon_firehose/src/sdk/sdk_bridge.dart';
12+
import 'package:aws_common/aws_common.dart';
1113
import 'package:smithy/smithy.dart';
1214

1315
/// Result of a PutRecordBatch operation.
@@ -47,7 +49,8 @@ class FirehoseSender {
4749
/// {@macro aws_amazon_firehose.firehose_sender}
4850
FirehoseSender({
4951
required String region,
50-
required AWSCredentialsProvider credentialsProvider,
52+
required foundation.AWSCredentialsProvider<foundation.AWSCredentials>
53+
credentialsProvider,
5154
AWSHttpClient? httpClient,
5255
}) : _firehoseClient = WrappedFirehoseClient(
5356
region: region,
@@ -118,13 +121,13 @@ class FirehoseSender {
118121
// Network-level errors (DNS, connection refused, etc.)
119122
throw FirehoseNetworkException(
120123
'Failed to connect to Firehose: $e',
121-
underlyingException: e,
124+
cause: e,
122125
);
123126
} on SocketException catch (e) {
124127
// Socket-level errors (no internet, connection reset, etc.)
125128
throw FirehoseNetworkException(
126129
'Network error: ${e.message}',
127-
underlyingException: e,
130+
cause: e,
128131
);
129132
}
130133
}

packages/kinesis/aws_amazon_firehose/lib/src/impl/record_client.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ class RecordClient {
5454
/// Throws [ClientClosedException] if the client has been closed.
5555
/// Throws [FirehoseLimitExceededException] if the cache is full.
5656
Future<void> record(FirehoseDataRecord record) async {
57-
if (_closed) throw const ClientClosedException();
57+
if (_closed) throw ClientClosedException();
5858
if (!_enabled) return;
5959

6060
final currentSize = await _storage.getCurrentCacheSize();
6161
if (currentSize + record.dataSize > _storage.maxCacheBytes) {
62-
throw const FirehoseLimitExceededException();
62+
throw FirehoseLimitExceededException();
6363
}
6464

6565
await _storage.saveRecord(record);
@@ -71,7 +71,7 @@ class RecordClient {
7171
///
7272
/// Throws [ClientClosedException] if the client has been closed.
7373
Future<FlushData> flush() async {
74-
if (_closed) throw const ClientClosedException();
74+
if (_closed) throw ClientClosedException();
7575
if (!_enabled) return const FlushData();
7676

7777
if (_flushing) return const FlushData(flushInProgress: true);

0 commit comments

Comments
 (0)