Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't wait for logging + Cache internet connection status #853

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/core/network/certificate_check_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class CertificateCheckInterceptor extends InterceptorContract {
@override
Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
/// Don't check for certificate if there is no internet connection
if (await getIt<NetworkInfo>().isConnected == false) {
if (getIt<NetworkInfo>().isConnected == false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid redundant null check for isConnected.

The internet connection check uses == false which is redundant and can be simplified.

Simplify the internet connection check.

-    if (getIt<NetworkInfo>().isConnected == false) {
+    if (!getIt<NetworkInfo>().isConnected) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (getIt<NetworkInfo>().isConnected == false) {
if (!getIt<NetworkInfo>().isConnected) {

return request;
}

final apiURL = request.url.host;
// do not check certificate for the certs endpoint
if (apiURL.contains('certs')) {
Expand All @@ -30,7 +31,6 @@ class CertificateCheckInterceptor extends InterceptorContract {
allowedSHAFingerprints: [storedSHAFigerprint],
timeout: 50,
);

if (!secure.contains('CONNECTION_SECURE')) {
await LoggingInfo.instance.error(
request.url.toString(),
Expand Down
18 changes: 14 additions & 4 deletions lib/core/network/network_info.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';

mixin NetworkInfo {
Future<bool> get isConnected;
bool get isConnected;
}

class NetworkInfoImpl implements NetworkInfo {
NetworkInfoImpl(this.connectionChecker);
NetworkInfoImpl(this.connectionChecker) {
// First check if there is an internet connection
connectionChecker.hasInternetAccess.then((value) {
isConnected = value;
});

final InternetConnection connectionChecker;
// Listen for changes in the internet connection status
connectionChecker.onStatusChange.listen((status) {
isConnected = status == InternetStatus.connected;
});
}

@override
Future<bool> get isConnected => connectionChecker.hasInternetAccess;
bool isConnected = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid redundant initialization of isConnected.

The isConnected variable is initialized to true but is immediately updated based on the internet connection status.

Consider initializing isConnected to false and updating it based on the initial check.

-  bool isConnected = true;
+  bool isConnected = false;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
bool isConnected = true;
bool isConnected = false;


final InternetConnection connectionChecker;
}
20 changes: 13 additions & 7 deletions lib/core/network/token_interceptor.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:developer';

Expand Down Expand Up @@ -37,17 +38,22 @@ class TokenInterceptor implements InterceptorContract {
request.headers['Authorization'] = 'Bearer ${session.accessToken}';
}
} catch (e, stackTrace) {
await LoggingInfo.instance.error(
e.toString(),
methodName: stackTrace.toString(),
unawaited(
LoggingInfo.instance.error(
e.toString(),
methodName: stackTrace.toString(),
),
);
}

await LoggingInfo.instance.logRequest(
request.method,
request.url.toString(),
correlationId,
unawaited(
LoggingInfo.instance.logRequest(
"API - " + request.method,
request.url.toString(),
correlationId,
),
);

return request;
}

Expand Down