-
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.
Merge branch 'feat/logging/default-remote-config' of https://github.c…
…om/aws-amplify/amplify-flutter into feat/logging/default-remote-config
- Loading branch information
Showing
19 changed files
with
736 additions
and
64 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...gging_cloudwatch/amplify_logging_cloudwatch/lib/src/amplify_cloudwatch_logger_plugin.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,19 @@ | ||
import 'package:amplify_logging_cloudwatch/src/amplify_log_stream_name_provider.dart'; | ||
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart'; | ||
|
||
/// {@macro aws_logging_cloudwatch.cloudwatch_logger_plugin} | ||
class AmplifyCloudWatchLoggerPlugin extends CloudWatchLoggerPlugin { | ||
/// {@macro aws_logging_cloudwatch.cloudwatch_logger_plugin} | ||
AmplifyCloudWatchLoggerPlugin({ | ||
required super.credentialsProvider, | ||
required super.pluginConfig, | ||
}) : super( | ||
logStreamProvider: DefaultCloudWatchLogStreamProvider( | ||
credentialsProvider: credentialsProvider, | ||
region: pluginConfig.region, | ||
logGroupName: pluginConfig.logGroupName, | ||
defaultLogStreamNameProvider: | ||
AmplifyLogStreamNameProvider().defaultLogStreamName, | ||
), | ||
); | ||
} |
39 changes: 39 additions & 0 deletions
39
...gging_cloudwatch/amplify_logging_cloudwatch/lib/src/amplify_log_stream_name_provider.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,39 @@ | ||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:amplify_logging_cloudwatch/src/device_info/device_info.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
const _guestUserId = 'guest'; | ||
|
||
/// {@template amplify_logging_cloudwatch.amplify_log_stream_name_provider} | ||
/// It uses {mm-dd-yyyy}.{deviceId}.{userId|guest} format for log stream name. | ||
/// | ||
/// It gets deviceId from platform inforamtion or uuid if it is `null`. | ||
/// It gets userId from `Amplify.Auth.getCurrentUser()).userId` or uses `guest` | ||
/// if userId is not available. | ||
/// {@endtemplate} | ||
class AmplifyLogStreamNameProvider { | ||
/// {@macro amplify_logging_cloudwatch.amplify_log_stream_name_provider} | ||
AmplifyLogStreamNameProvider(); | ||
static final DateFormat _dateFormat = DateFormat('yyyy-MM-dd'); | ||
String? _deviceID; | ||
|
||
/// Returns log stream name in `{mm-dd-yyyy}.{deviceId}.{userId|guest}` format | ||
Future<String> defaultLogStreamName() async { | ||
_deviceID ??= await getDeviceId() ?? UUID.getUUID(); | ||
String userId; | ||
userId = await _getUserId(); | ||
return '${_dateFormat.format(DateTime.timestamp())}.$_deviceID.$userId'; | ||
} | ||
|
||
Future<String> _getUserId() async { | ||
String userId; | ||
try { | ||
userId = (await Amplify.Auth.getCurrentUser()).userId; | ||
} on Error { | ||
userId = _guestUserId; | ||
} on Exception { | ||
userId = _guestUserId; | ||
} | ||
return userId; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/device_info/device_info.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,3 @@ | ||
export 'device_info.stub.dart' | ||
if (dart.library.io) 'device_info.vm.dart' | ||
if (dart.library.html) 'device_info.web.dart'; |
7 changes: 7 additions & 0 deletions
7
...s/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/device_info/device_info.stub.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,7 @@ | ||
/// {@template amplify_logging_cloudwatch.device_info} | ||
/// Returns device Id from platform information on `vm`. | ||
/// Returns a UUID across browser sessions for web. | ||
/// {@endtemplate} | ||
Future<String?> getDeviceId() { | ||
throw UnimplementedError('getDeviceId() has not been implemented.'); | ||
} |
30 changes: 30 additions & 0 deletions
30
...ges/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/device_info/device_info.vm.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,30 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:device_info_plus/device_info_plus.dart'; | ||
|
||
/// {@macro amplify_logging_cloudwatch.device_info} | ||
Future<String?> getDeviceId() async { | ||
final deviceInfo = DeviceInfoPlugin(); | ||
String? deviceID; | ||
try { | ||
if (Platform.isAndroid) { | ||
final androidInfo = await deviceInfo.androidInfo; | ||
deviceID = androidInfo.id; | ||
} else if (Platform.isIOS) { | ||
final iosInfo = await deviceInfo.iosInfo; | ||
deviceID = iosInfo.identifierForVendor ?? ''; | ||
} else if (Platform.isLinux) { | ||
final linuxInfo = await deviceInfo.linuxInfo; | ||
deviceID = linuxInfo.machineId ?? ''; | ||
} else if (Platform.isMacOS) { | ||
final macInfo = await deviceInfo.macOsInfo; | ||
deviceID = macInfo.systemGUID ?? ''; | ||
} else if (Platform.isWindows) { | ||
final windowsInfo = await deviceInfo.windowsInfo; | ||
deviceID = windowsInfo.deviceId; | ||
} | ||
} on Exception { | ||
return null; | ||
} | ||
return deviceID; | ||
} |
16 changes: 16 additions & 0 deletions
16
...es/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/device_info/device_info.web.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,16 @@ | ||
import 'dart:html'; | ||
|
||
import 'package:amplify_core/amplify_core.dart'; | ||
|
||
const _localStorageKey = 'amplify-cloudwatch-logger-device-id'; | ||
|
||
/// {@macro amplify_logging_cloudwatch.device_info} | ||
Future<String?> getDeviceId() async { | ||
var deviceID = window.localStorage[_localStorageKey]; | ||
if (deviceID != null) { | ||
return deviceID; | ||
} | ||
deviceID = UUID.getUUID(); | ||
window.localStorage.putIfAbsent(_localStorageKey, () => deviceID!); | ||
return deviceID; | ||
} |
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
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
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
21 changes: 21 additions & 0 deletions
21
.../logging_cloudwatch/amplify_logging_cloudwatch/test/amplify_log_stream_provider_test.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,21 @@ | ||
@TestOn('vm') | ||
|
||
import 'package:amplify_logging_cloudwatch/src/amplify_log_stream_name_provider.dart'; | ||
import 'package:flutter_test/flutter_test.dart' as flutter; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
flutter.TestWidgetsFlutterBinding.ensureInitialized(); | ||
test('it uses uuid and guest when their values are not provided', () async { | ||
final logStreamNameProvider = AmplifyLogStreamNameProvider(); | ||
await expectLater(logStreamNameProvider.defaultLogStreamName(), completes); | ||
}); | ||
|
||
test('it caches the device Id', () async { | ||
final logStreamNameProvider = AmplifyLogStreamNameProvider(); | ||
final logStreamName1 = await logStreamNameProvider.defaultLogStreamName(); | ||
final logStreamName2 = await logStreamNameProvider.defaultLogStreamName(); | ||
|
||
expect(logStreamName1, logStreamName2); | ||
}); | ||
} |
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.