Skip to content

Commit

Permalink
Merge pull request #1 from jtans/master
Browse files Browse the repository at this point in the history
Support null-safety
  • Loading branch information
themisir authored Apr 9, 2021
2 parents 7e8683f + 42d5120 commit 5de66a7
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 132 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 26 additions & 26 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ packages:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.3"
version: "1.2.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.14.13"
version: "1.15.0"
dio:
dependency: "direct main"
description:
name: dio
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.10"
version: "4.0.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -38,16 +38,16 @@ packages:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.1.4"
version: "4.0.0"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.8"
version: "1.3.0"
network_logger:
dependency: "direct main"
description:
Expand All @@ -59,9 +59,9 @@ packages:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.7.0"
version: "1.8.0"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -71,37 +71,37 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.7.0"
version: "1.8.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.5"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
version: "1.2.0"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
version: "2.1.0"
sdks:
dart: ">=2.9.0-14.0.dev <3.0.0"
flutter: ">=1.17.0 <2.0.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=2.0.1"
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ publish_to: 'none'
version: 1.0.0+1

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

dependencies:
flutter:
sdk: flutter

dio: ^3.0.0
dio: ^4.0.0

network_logger:
path: ..
Expand Down
34 changes: 20 additions & 14 deletions lib/src/impl/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,52 @@ class DioNetworkLogger extends dio.Interceptor {
final NetworkEventList eventList;
final _requests = <dio.RequestOptions, NetworkEvent>{};

DioNetworkLogger({NetworkEventList eventList})
DioNetworkLogger({NetworkEventList? eventList})
: this.eventList = eventList ?? NetworkLogger.instance;

@override
Future onRequest(dio.RequestOptions options) {
Future<void> onRequest(
dio.RequestOptions options, dio.RequestInterceptorHandler handler) async {
eventList.add(_requests[options] = NetworkEvent.now(
request: options.toRequest(),
error: null,
response: null,
));
return Future.value(options);
}

@override
Future onResponse(dio.Response response) {
var event = _requests[response.request];
void onResponse(
dio.Response response,
dio.ResponseInterceptorHandler handler,
) {
final req = response.requestOptions.toRequest();
var event = _requests[req];
if (event != null) {
_requests.remove(response.request);
_requests.remove(req);
eventList.updated(event..response = response.toResponse());
} else {
eventList.add(NetworkEvent.now(
request: response.request.toRequest(),
request: req,
response: response.toResponse(),
));
}
return Future.value(response);
}

@override
Future onError(dio.DioError err) {
var event = _requests[err.request];
void onError(dio.DioError err, dio.ErrorInterceptorHandler handler) {
final req = err.requestOptions.toRequest();
var event = _requests[req];
if (event != null) {
_requests.remove(err.request);
_requests.remove(req);
eventList.updated(event..error = err.toNetworkError());
} else {
eventList.add(NetworkEvent.now(
request: err.request.toRequest(),
request: req,
response: err.response?.toResponse(),
error: err.toNetworkError(),
));
}
return Future.value(err);
}
}

Expand All @@ -64,8 +70,8 @@ extension _RequestOptionsX on dio.RequestOptions {
extension _ResponseX on dio.Response {
Response toResponse() => Response(
data: data,
statusCode: statusCode,
statusMessage: statusMessage,
statusCode: statusCode ?? -1,
statusMessage: statusMessage ?? 'unkown',
headers: Headers(
headers.map.entries.fold<List<MapEntry<String, String>>>(
[],
Expand Down
26 changes: 13 additions & 13 deletions lib/src/network_event.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/// Network event log entry.
class NetworkEvent {
NetworkEvent({this.request, this.response, this.error, this.timestamp});
NetworkEvent({this.request, this.response, this.error, this.timestamp});
NetworkEvent.now({this.request, this.response, this.error})
: this.timestamp = DateTime.now();

Request request;
Response response;
NetworkError error;
DateTime timestamp;
Request? request;
Response? response;
NetworkError? error;
DateTime? timestamp;
}

/// Used for storing [Request] and [Response] headers.
class Headers {
Headers(Iterable<MapEntry<String, String>> entries)
: this.entries = entries.toList();
Headers.fromMap(Map<String, String> map) : this.entries = map.entries;
Headers.fromMap(Map<String, String> map) : this.entries = map.entries as List<MapEntry<String, String>>;

final List<MapEntry<String, String>> entries;

Expand All @@ -28,9 +28,9 @@ class Headers {
/// Http request details.
class Request {
Request({
this.uri,
this.method,
this.headers,
required this.uri,
required this.method,
required this.headers,
this.data,
});

Expand All @@ -43,9 +43,9 @@ class Request {
/// Http response details.
class Response {
Response({
this.headers,
this.statusCode,
this.statusMessage,
required this.headers,
required this.statusCode,
required this.statusMessage,
this.data,
});

Expand All @@ -57,7 +57,7 @@ class Response {

/// Network error details.
class NetworkError {
NetworkError({this.message});
NetworkError({required this.message});

final String message;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/network_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UpdateEvent {
const UpdateEvent(this.event);
const UpdateEvent.clear() : this.event = null;

final NetworkEvent event;
final NetworkEvent? event;
}

/// Network logger interface.
Expand Down
Loading

0 comments on commit 5de66a7

Please sign in to comment.