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

Print deprecation warning from Kuzzle #75

Merged
merged 16 commits into from
Apr 21, 2021
1 change: 1 addition & 0 deletions doc/2/core-classes/kuzzle/constructor/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Kuzzle(
queueTTL,
queueMaxSize = 500,
replayInterval,
deprecationWarnings = true,
globalVolatile,
})
```
Expand Down
11 changes: 9 additions & 2 deletions lib/src/kuzzle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'kuzzle/request.dart';
import 'kuzzle/response.dart';
import 'protocols/abstract.dart';
import 'protocols/events.dart';
import 'utils/deprecation.dart';

enum OfflineMode { manual, auto }

Expand All @@ -31,11 +32,13 @@ class _KuzzleQueuedRequest {
}

class Kuzzle extends KuzzleEventEmitter {

Kuzzle(
this.protocol, {
this.autoQueue = false,
this.autoReplay = false,
this.autoResubscribe = true,
bool deprecationWarnings = true,
this.eventTimeout = 200,
this.offlineMode = OfflineMode.manual,
this.offlineQueueLoader,
Expand All @@ -44,7 +47,10 @@ class Kuzzle extends KuzzleEventEmitter {
this.queueMaxSize = 500,
this.replayInterval,
this.globalVolatile,
}) {
}) :
deprecationHandler =
DeprecationHandler(deprecationWarning: deprecationWarnings)
{
if (offlineMode == OfflineMode.auto) {
autoQueue = true;
autoReplay = true;
Expand Down Expand Up @@ -128,6 +134,7 @@ class Kuzzle extends KuzzleEventEmitter {
final OfflineMode offlineMode;
final Function offlineQueueLoader;
final Function queueFilter;
final DeprecationHandler deprecationHandler;

/// Automatically queue all requests during offline mode
bool autoQueue;
Expand Down Expand Up @@ -395,7 +402,7 @@ class Kuzzle extends KuzzleEventEmitter {

_requests.add(request.requestId);
// todo: implement query options
return protocol.query(request);
return protocol.query(request).then(deprecationHandler.logDeprecation);
}

KuzzleController operator [](String accessor) => _controllers[accessor];
Expand Down
9 changes: 8 additions & 1 deletion lib/src/kuzzle/response.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:kuzzle/src/utils/deprecation.dart';

import 'errors.dart';

class KuzzleResponse {
KuzzleResponse({
this.action,
this.collection,
this.controller,
this.deprecations,
this.error,
this.index,
this.room,
Expand All @@ -17,6 +20,7 @@ class KuzzleResponse {
action = json['action'] as String;
collection = json['collection'] as String;
controller = json['controller'] as String;
deprecations = Deprecation.fromJsonList(json);
error = json['error'] == null ? null : KuzzleError.fromJson(json);
index = json['index'] as String;
protocol = json['protocol'] as String;
Expand All @@ -43,10 +47,12 @@ class KuzzleResponse {
if (controller != null) {
map['controller'] = controller;
}
if (deprecations != null) {
map['deprecations'] = Deprecation.toJsonList(deprecations);
}
if (error != null) {
map['error'] = error.toJson();
}
String users;
if (index != null) {
map['index'] = index;
}
Expand Down Expand Up @@ -90,6 +96,7 @@ class KuzzleResponse {
String action;
String collection;
String controller;
List<Deprecation> deprecations;
KuzzleError error;
String index;
String protocol;
Expand Down
62 changes: 62 additions & 0 deletions lib/src/utils/deprecation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'dart:convert';
import 'dart:developer';
import 'package:kuzzle/src/kuzzle/response.dart';

class DeprecationHandler {

DeprecationHandler({ this.deprecationWarning = true });

final bool deprecationWarning;

KuzzleResponse logDeprecation(KuzzleResponse response) {
if (deprecationWarning && response.deprecations.isNotEmpty) {
for (final deprecation in response.deprecations) {
log(
'${response.action}: ${deprecation.message}',
name: 'Deprecation Warning',
level: 900 // Warning
);
}
}
return response;
}
}

class Deprecation {
Deprecation(this.message, this.version);

factory Deprecation.fromJson(Map<String, dynamic> json) =>
Deprecation(json['message'] as String, json['version'] as String);

static List<Deprecation> fromJsonList(Map<String, dynamic> json) {
if (json['deprecations'] == null || json['deprecations'] is! List<Object>) {
return [];
}
final list = <Deprecation>[];
for (final deprecation in json['deprecations']) {
list.add(
Deprecation.fromJson(
deprecation as Map<String, dynamic>
)
);
}
return list;
}

final String message;
final String version;

Map<String, dynamic> toJson() => {
'message': message,
'version': version,
};

static String toJsonList(List<Deprecation> list) {
final jsonList = <Map<String, dynamic>>[];
for (final deprecation in list) {
jsonList.add(deprecation.toJson());
}
return jsonEncode(jsonList);
}

}