Skip to content

Commit

Permalink
Merge branch 'null-safety'
Browse files Browse the repository at this point in the history
  • Loading branch information
assemmarwan committed Jul 2, 2021
2 parents da2657d + b7c1742 commit 5347881
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.0
- Fully migrated to Null Safety

## 2.0.0-nullsafety.0
- Migration to Null Safety


## 1.0.3
- Added validation for empty body ([#1](https://github.com/assemmarwan/dio_http_formatter/pull/1))

Expand Down
52 changes: 27 additions & 25 deletions lib/src/dio_http_formatter_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HttpFormatter extends Interceptor {
_includeResponseBody;

/// Optionally add a filter that will log if the function returns true
final HttpLoggerFilter _httpLoggerFilter;
final HttpLoggerFilter? _httpLoggerFilter;

/// Optionally can add custom [LogPrinter]
HttpFormatter(
Expand All @@ -26,8 +26,8 @@ class HttpFormatter extends Interceptor {
bool includeResponse = true,
bool includeResponseHeaders = true,
bool includeResponseBody = true,
Logger logger,
HttpLoggerFilter httpLoggerFilter})
Logger? logger,
HttpLoggerFilter? httpLoggerFilter})
: _includeRequest = includeRequest,
_includeRequestHeaders = includeRequestHeaders,
_includeRequestBody = includeRequestBody,
Expand All @@ -44,38 +44,39 @@ class HttpFormatter extends Interceptor {
_httpLoggerFilter = httpLoggerFilter;

@override
Future onRequest(RequestOptions options) {
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
options.extra = <String, dynamic>{
'start_time': DateTime.now().millisecondsSinceEpoch
};
return super.onRequest(options);
super.onRequest(options, handler);
}

@override
Future onResponse(Response response) async {
if (_httpLoggerFilter == null || _httpLoggerFilter()) {
final message = _prepareLog(response.request, response);
if (message != null && message != '') {
void onResponse(Response response, ResponseInterceptorHandler handler) async {
if (_httpLoggerFilter == null || _httpLoggerFilter!()) {
final message = _prepareLog(response.requestOptions, response);
if (message != '') {
_logger.i(message);
}
}
return super.onResponse(response);
super.onResponse(response, handler);
}

@override
Future onError(DioError err) {
if (_httpLoggerFilter == null || _httpLoggerFilter()) {
final message = _prepareLog(err.request, err.response);
if (message != null && message != '') {
void onError(DioError err, ErrorInterceptorHandler handler) {
if (_httpLoggerFilter == null || _httpLoggerFilter!()) {
final message = _prepareLog(err.requestOptions, err.response);
if (message != '') {
_logger.e(message);
}
}
return super.onError(err);
return super.onError(err, handler);
}

/// Whether to pretty print a JSON or return as regular String
String _getBody(dynamic data, String contentType) {
if (contentType.toLowerCase().contains('application/json')) {
String _getBody(dynamic data, String? contentType) {
if (contentType != null &&
contentType.toLowerCase().contains('application/json')) {
final encoder = JsonEncoder.withIndent(' ');
Map jsonBody;
if (data is String) {
Expand All @@ -90,25 +91,26 @@ class HttpFormatter extends Interceptor {
}

/// Extracts the headers and body (if any) from the request and response
String _prepareLog(RequestOptions requestOptions, Response response) {
String _prepareLog(RequestOptions? requestOptions, Response? response) {
var requestString = '', responseString = '';

if (_includeRequest) {
requestString = '⤴ REQUEST ⤴\n\n';

requestString += '${requestOptions.method} ${requestOptions.path}\n';
requestString +=
'${requestOptions?.method ?? ''} ${requestOptions?.path ?? ''}\n';

if (_includeRequestHeaders) {
for (final header in requestOptions.headers.entries) {
for (final header in (requestOptions?.headers ?? {}).entries) {
requestString += '${header.key}: ${header.value}\n';
}
}

if (_includeRequestBody &&
requestOptions.data != null &&
requestOptions.data.isNotEmpty) {
requestString +=
'\n\n' + _getBody(requestOptions.data, requestOptions.contentType);
requestOptions?.data != null &&
requestOptions?.data.isNotEmpty) {
requestString += '\n\n' +
_getBody(requestOptions?.data, requestOptions?.contentType);
}

requestString += '\n\n';
Expand All @@ -117,7 +119,7 @@ class HttpFormatter extends Interceptor {
if (_includeResponse && response != null) {
responseString =
'⤵ RESPONSE [${response.statusCode}/${response.statusMessage}] '
'${requestOptions.extra['start_time'] != null ? '[Time elapsed: ${DateTime.now().millisecondsSinceEpoch - requestOptions.extra['start_time']} ms]' : ''}'
'${requestOptions?.extra['start_time'] != null ? '[Time elapsed: ${DateTime.now().millisecondsSinceEpoch - requestOptions?.extra['start_time']} ms]' : ''}'
'⤵\n\n';

if (_includeResponseHeaders) {
Expand Down
12 changes: 6 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: dio_http_formatter
description: A Dio interceptor for pretty printing the HTTP request/response to be printed in the console for easier debugging
version: 1.0.3
version: 2.0.0
repository: https://github.com/assemmarwan/dio_http_formatter
homepage: https://github.com/assemmarwan/dio_http_formatter

environment:
sdk: '>=2.7.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
dio: ^3.0.9
logger: ^0.9.1
dio: ^4.0.0
logger: ^1.0.0

dev_dependencies:
pedantic: ^1.9.0
test: ^1.14.4
pedantic: ^1.11.1
test: ^1.16.5

0 comments on commit 5347881

Please sign in to comment.