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

Fix connectTimeout and receiveTimeout #32

Merged
merged 3 commits into from
Feb 13, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
pubspec.lock
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Changelog

## [1.3.1]

- Add recommended lints
- Fix connectTimeout and receiveTimeout

## [1.3.0+1]
- Update readme file badge

## [1.3.0]

- support dio >= 5.0.0
Expand Down
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
40 changes: 26 additions & 14 deletions lib/pretty_dio_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class PrettyDioLogger extends Interceptor {
requestHeaders['contentType'] = options.contentType?.toString();
requestHeaders['responseType'] = options.responseType.toString();
requestHeaders['followRedirects'] = options.followRedirects;
requestHeaders['connectTimeout'] = options.connectTimeout;
requestHeaders['receiveTimeout'] = options.receiveTimeout;
requestHeaders['connectTimeout'] = options.connectTimeout?.toString();
requestHeaders['receiveTimeout'] = options.receiveTimeout?.toString();
_printMapAsTable(requestHeaders, header: 'Headers');
_printMapAsTable(options.extra, header: 'Extras');
}
Expand All @@ -93,7 +93,8 @@ class PrettyDioLogger extends Interceptor {
if (err.type == DioErrorType.badResponse) {
final uri = err.response?.requestOptions.uri;
_printBoxed(
header: 'DioError ║ Status: ${err.response?.statusCode} ${err.response?.statusMessage}',
header:
'DioError ║ Status: ${err.response?.statusCode} ${err.response?.statusMessage}',
text: uri.toString());
if (err.response != null && err.response?.data != null) {
logPrint('╔ ${err.type.toString()}');
Expand All @@ -113,7 +114,8 @@ class PrettyDioLogger extends Interceptor {
_printResponseHeader(response);
if (responseHeader) {
final responseHeaders = <String, String>{};
response.headers.forEach((k, list) => responseHeaders[k] = list.toString());
response.headers
.forEach((k, list) => responseHeaders[k] = list.toString());
_printMapAsTable(responseHeaders, header: 'Headers');
}

Expand Down Expand Up @@ -156,7 +158,9 @@ class PrettyDioLogger extends Interceptor {
final uri = response.requestOptions.uri;
final method = response.requestOptions.method;
_printBoxed(
header: 'Response ║ $method ║ Status: ${response.statusCode} ${response.statusMessage}', text: uri.toString());
header:
'Response ║ $method ║ Status: ${response.statusCode} ${response.statusMessage}',
text: uri.toString());
}

void _printRequestHeader(RequestOptions options) {
Expand All @@ -165,7 +169,8 @@ class PrettyDioLogger extends Interceptor {
_printBoxed(header: 'Request ║ $method ', text: uri.toString());
}

void _printLine([String pre = '', String suf = '╝']) => logPrint('$pre${'═' * maxWidth}$suf');
void _printLine([String pre = '', String suf = '╝']) =>
logPrint('$pre${'═' * maxWidth}$suf');

void _printKV(String? key, Object? v) {
final pre = '╟ $key: ';
Expand All @@ -182,7 +187,9 @@ class PrettyDioLogger extends Interceptor {
void _printBlock(String msg) {
final lines = (msg.length / maxWidth).ceil();
for (var i = 0; i < lines; ++i) {
logPrint((i >= 0 ? '║ ' : '') + msg.substring(i * maxWidth, math.min<int>(i * maxWidth + maxWidth, msg.length)));
logPrint((i >= 0 ? '║ ' : '') +
msg.substring(i * maxWidth,
math.min<int>(i * maxWidth + maxWidth, msg.length)));
}
}

Expand All @@ -205,7 +212,7 @@ class PrettyDioLogger extends Interceptor {
final isLast = index == data.length - 1;
dynamic value = data[key];
if (value is String) {
value = '"${value.toString().replaceAll(RegExp(r'(\r|\n)+'), " ")}"';
value = '"${value.toString().replaceAll(RegExp(r'([\r\n])+'), " ")}"';
}
if (value is Map) {
if (compact && _canFlattenMap(value)) {
Expand Down Expand Up @@ -260,16 +267,20 @@ class PrettyDioLogger extends Interceptor {
var chunks = [];
for (var i = 0; i < list.length; i += chunkSize) {
chunks.add(
list.sublist(i, i + chunkSize > list.length ? list.length : i + chunkSize),
list.sublist(
i, i + chunkSize > list.length ? list.length : i + chunkSize),
);
}
chunks.forEach((element) => logPrint(
'║${_indent(tabs)} ${element.join(", ")}',
));
for (var element in chunks) {
logPrint('║${_indent(tabs)} ${element.join(", ")}');
}
}

bool _canFlattenMap(Map map) {
return map.values.where((dynamic val) => val is Map || val is List).isEmpty && map.toString().length < maxWidth;
return map.values
.where((dynamic val) => val is Map || val is List)
.isEmpty &&
map.toString().length < maxWidth;
}

bool _canFlattenList(List list) {
Expand All @@ -279,7 +290,8 @@ class PrettyDioLogger extends Interceptor {
void _printMapAsTable(Map? map, {String? header}) {
if (map == null || map.isEmpty) return;
logPrint('╔ $header ');
map.forEach((dynamic key, dynamic value) => _printKV(key.toString(), value));
map.forEach(
(dynamic key, dynamic value) => _printKV(key.toString(), value));
_printLine('╚');
}
}
77 changes: 0 additions & 77 deletions pubspec.lock

This file was deleted.

5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
name: pretty_dio_logger
description: Pretty Dio logger is a Dio interceptor that logs network calls in a pretty, easy to read format.
version: 1.3.0+1
version: 1.3.1
homepage: https://github.com/Milad-Akarie/pretty_dio_logger

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
dio: ^5.0.0

dev_dependencies:
lints: ^2.0.1