Skip to content

Commit

Permalink
chore: refacted fileStore dart implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
khatruong2009 committed Sep 13, 2023
1 parent 6f426ee commit 5085ed1
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import 'dart:async';
import 'dart:math';

import 'package:aws_common/aws_common.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart';
import 'package:aws_logging_cloudwatch/src/sdk/cloud_watch_logs.dart';
import 'package:aws_logging_cloudwatch/src/stoppable_timer.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export 'file_storage.vm.dart' if (dart.library.html) 'file_storage.web.dart';

/// File storage interface for saving and loading constraint locally
abstract interface class FileStorage {
/// Save constraint locally to file
Future<void> saveConstraintLocally(String filename, String data);

/// Load constraint from file
Future<String?> loadConstraint(String filename);
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:convert';
import 'dart:io';

import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

/// Save constraint locally to file
Future<void> saveConstraintLocally(Map<String, dynamic> constraint) async {
final directory = await getApplicationSupportDirectory();
final path = directory.path;
final file = File(p.join(path, 'logging_constraint.json'));
await file.writeAsString(jsonEncode(constraint));
}
/// File storage implementation for saving and loading constraint locally
final class FileStorageImpl implements FileStorage {
/// File storage implementation for saving and loading constraint locally
FileStorageImpl(this.pathProvider);

/// Path provider to get the application support path
final AppPathProvider pathProvider;

/// Load constraint from file
Future<Map<String, dynamic>?> loadConstraint() async {
final directory = await getApplicationSupportDirectory();
final path = directory.path;
final file = File('$path/logging_constraint.json');
@override
Future<String?> loadConstraint(String fileName) async {
final file =
File(p.join(await pathProvider.getApplicationSupportPath(), fileName));
if (await file.exists()) {
return file.readAsString();
}
return null;
}

if (file.existsSync()) {
final content = await file.readAsString();
return jsonDecode(content) as Map<String, dynamic>;
@override
Future<void> saveConstraintLocally(String fileName, String content) async {
final file =
File(p.join(await pathProvider.getApplicationSupportPath(), fileName));
await file.writeAsString(content);
}
return null;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:convert';
import 'dart:html';

// Declare a constant global private variable for the key
const String _localStorageKey = 'aws.cloudwatch.logging_constraint';
import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart';

/// Save constraint locally to web storage
Future<void> saveConstraintLocally(Map<String, dynamic> constraint) async {
window.localStorage[_localStorageKey] = jsonEncode(constraint);
}
/// File storage implementation for saving and loading constraint locally
final class FileStorageImpl implements FileStorage {
/// File storage implementation for saving and loading constraint locally
// ignore: avoid_unused_constructor_parameters
FileStorageImpl(AppPathProvider pathProvider);

static const _prefix = 'aws.cloudwatch';

@override
Future<String?> loadConstraint(String fileName) async {
return window.localStorage['$_prefix.$fileName'];
}

/// Load constraint from web storage
Map<String, dynamic>? loadConstraint() {
final content = window.localStorage[_localStorageKey];
if (content != null) {
return jsonDecode(content) as Map<String, dynamic>;
@override
Future<void> saveConstraintLocally(String fileName, String content) async {
window.localStorage['$_prefix.$fileName'] = content;
}
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import 'package:amplify_core/amplify_core.dart';

import 'package:aws_common/aws_common.dart';
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.vm.dart'
if (dart.library.html) 'package:aws_logging_cloudwatch/src/file_storage/file_storage.web.dart'
as storage;
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart';
import 'package:aws_signature_v4/aws_signature_v4.dart';
import 'package:meta/meta.dart';

Expand All @@ -37,11 +35,15 @@ base class BaseRemoteLoggingConstraintProvider
BaseRemoteLoggingConstraintProvider({
required DefaultRemoteConfiguration config,
required AWSCredentialsProvider credentialsProvider,
}) : _config = config,
FileStorage? fileStorage,
}) : _fileStorage = fileStorage,
_config = config,
_credentialsProvider = credentialsProvider {
init();
}

final FileStorage? _fileStorage;

final DefaultRemoteConfiguration _config;
final AWSCredentialsProvider _credentialsProvider;

Expand All @@ -64,9 +66,14 @@ base class BaseRemoteLoggingConstraintProvider
/// starting the refresh timer afterwards.
Future<LoggingConstraint?> init() async {
// Check local storage first.
final localConstraint = await storage.loadConstraint();
if (localConstraint != null) {
_loggingConstraint = LoggingConstraint.fromJson(localConstraint);
if (_fileStorage != null) {
final localConstraint =
await _fileStorage!.loadConstraint('remoteloggingconstraints.json');
if (localConstraint != null) {
_loggingConstraint = LoggingConstraint.fromJson(
jsonDecode(localConstraint) as Map<String, dynamic>,
);
}
}
await _fetchAndCacheConstraintFromEndpoint();
await _refreshConstraintPeriodically();
Expand Down Expand Up @@ -98,7 +105,13 @@ base class BaseRemoteLoggingConstraintProvider
jsonDecode(body) as Map<String, dynamic>,
);
_loggingConstraint = fetchedConstraint;
await storage.saveConstraintLocally(fetchedConstraint.toJson());

if (_fileStorage != null) {
await _fileStorage!.saveConstraintLocally(
'remoteloggingconstraints.json',
jsonEncode(fetchedConstraint.toJson()),
);
}
}
} on Exception catch (exception) {
throw Exception(
Expand Down Expand Up @@ -126,9 +139,7 @@ base class BaseRemoteLoggingConstraintProvider

_timer = Timer.periodic(
_config.refreshInterval,
(Timer timer) async {
await _fetchAndCacheConstraintFromEndpoint();
},
(_) => _fetchAndCacheConstraintFromEndpoint(),
);
}
}
Expand All @@ -143,6 +154,7 @@ final class DefaultRemoteLoggingConstraintProvider
DefaultRemoteLoggingConstraintProvider({
required super.config,
required this.credentialsProvider,
super.fileStorage,
}) : super(credentialsProvider: credentialsProvider);

/// The credentials provider to use for signing the request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies:
json_annotation: ^4.8.1
meta: ^1.9.1
path: ^1.8.0
path_provider: ^2.0.1
smithy: ^0.5.0+3
smithy_aws: ^0.5.0+3

Expand Down

0 comments on commit 5085ed1

Please sign in to comment.