From 9a449043e2eb0e50ce9740092b0f16476573fff6 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 1 Nov 2023 14:07:43 +0300 Subject: [PATCH] ApiReply | extended message, keepAlive mode, debug mode --- .../core/api_query_type/executable_query.dart | 10 +++++- lib/src/core/api_query_type/python_query.dart | 12 +++++-- lib/src/core/api_query_type/sql_query.dart | 10 +++++- lib/src/reply/api_error.dart | 32 +++++++++++++++++ lib/src/reply/api_reply.dart | 36 +++++++++---------- pubspec.yaml | 2 +- 6 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 lib/src/reply/api_error.dart diff --git a/lib/src/core/api_query_type/executable_query.dart b/lib/src/core/api_query_type/executable_query.dart index f17cee1..8819a26 100644 --- a/lib/src/core/api_query_type/executable_query.dart +++ b/lib/src/core/api_query_type/executable_query.dart @@ -8,16 +8,22 @@ class ExecutableQuery implements ApiQueryType { late String _id; final String _script; final Map _params; + final bool _keepAlive; + final bool _debug; /// /// Prapares query for some executable ExecutableQuery({ required String authToken, required String script, required Map params, + bool keepAlive = false, + bool debug = false, }) : _authToken = authToken, _script = script, - _params = params; + _params = params, + _keepAlive = keepAlive, + _debug = debug; /// @override bool valid() { @@ -31,6 +37,8 @@ class ExecutableQuery implements ApiQueryType { final jsonString = json.encode({ 'auth_token': _authToken, 'id': _id, + 'keep-alive': _keepAlive, + 'debug': _debug, 'executable': { 'script': _script, 'params': _params, diff --git a/lib/src/core/api_query_type/python_query.dart b/lib/src/core/api_query_type/python_query.dart index c096560..11ea644 100644 --- a/lib/src/core/api_query_type/python_query.dart +++ b/lib/src/core/api_query_type/python_query.dart @@ -7,6 +7,8 @@ class PythonQuery implements ApiQueryType { final String _authToken; late String _id; final String _script; + final bool _keepAlive; + final bool _debug; final Map _params; /// /// Prapares query for some python script @@ -14,11 +16,15 @@ class PythonQuery implements ApiQueryType { required String authToken, required String script, required Map params, + bool keepAlive = false, + bool debug = false, }) : _authToken = authToken, _script = script, - _params = params; - /// + _params = params, + _keepAlive = keepAlive, + _debug = debug; +/// @override bool valid() { return true; @@ -31,6 +37,8 @@ class PythonQuery implements ApiQueryType { final jsonString = json.encode({ 'auth_token': _authToken, 'id': _id, + 'keep-alive': _keepAlive, + 'debug': _debug, 'python': { 'script': _script, 'params': _params, diff --git a/lib/src/core/api_query_type/sql_query.dart b/lib/src/core/api_query_type/sql_query.dart index e81481c..c1ce80a 100644 --- a/lib/src/core/api_query_type/sql_query.dart +++ b/lib/src/core/api_query_type/sql_query.dart @@ -8,16 +8,22 @@ class SqlQuery implements ApiQueryType { late String _id; final String _database; final String _sql; + final bool _keepAlive; + final bool _debug; /// /// Prapares sql for some database SqlQuery({ required String authToken, required String database, required String sql, + bool keepAlive = false, + bool debug = false, }) : _authToken = authToken, _database = database, - _sql = sql; + _sql = sql, + _keepAlive = keepAlive, + _debug = debug; /// @override bool valid() { @@ -31,6 +37,8 @@ class SqlQuery implements ApiQueryType { final jsonString = json.encode({ 'auth_token': _authToken, 'id': _id, + 'keep-alive': _keepAlive, + 'debug': _debug, 'sql': { 'database': _database, 'sql': _sql, diff --git a/lib/src/reply/api_error.dart b/lib/src/reply/api_error.dart new file mode 100644 index 0000000..476631b --- /dev/null +++ b/lib/src/reply/api_error.dart @@ -0,0 +1,32 @@ + +// import 'package:logging/logging.dart'; + +class ApiError { + // final _log = Logger('ApiError'); + final Map? _errors; + /// + ApiError({ + required Map? errors, + }) : + _errors = errors; + /// + String get message => _errors?['message'] ?? ''; + /// + String get details => _errors?['details'] ?? ''; + /// + bool get isEmpty { + return message.isEmpty && details.isEmpty; + } + /// + bool get isNotEmpty { + return !isEmpty; + } + /// + @override + String toString() { + return '''$ApiError { +\t\tmessage: $message; +\t\tdetails: $details; +\t}'''; + } +} diff --git a/lib/src/reply/api_reply.dart b/lib/src/reply/api_reply.dart index 0fa6a01..9ed2cd6 100644 --- a/lib/src/reply/api_reply.dart +++ b/lib/src/reply/api_reply.dart @@ -1,27 +1,28 @@ import 'dart:convert'; +import 'package:dart_api_client/src/reply/api_error.dart'; import 'package:logging/logging.dart'; class ApiReply { final _log = Logger('ApiReply'); - late String _authToken; - late String _id; - late Map _sql; - late List> _data; - late List _errors; + late final String _authToken; + late final String _id; + late final Map _query; + late final List> _data; + late final ApiError _error; /// ApiReply({ required String authToken, required String id, - required Map sql, + required Map query, required List> data, - required List errors, + required ApiError errors, }) : _authToken = authToken, _id = id, - _sql = sql, + _query = query, _data = data, - _errors = errors; + _error = errors; /// ApiReply.fromJson(String jsonString) { // _log.fine('.fromJson | jsonString: $jsonString'); @@ -29,34 +30,33 @@ class ApiReply { _log.fine('.fromJson | jsonMap: $jsonMap'); _authToken = jsonMap['auth_token']; _id = jsonMap['id']; - _sql = jsonMap['sql'] ?? {}; + _query = jsonMap['query'] ?? {}; _data = (jsonMap['data'] as List).map((e) { return (e as Map).map((key, value) => MapEntry(key.toString(), value)); - // final key = (e as MapEntry).key; - // final value = (e as MapEntry).value; - // return MapEntry(key.toString(), value); }).toList(); - _errors = (jsonMap['errors'] as List).map((e) => '$e').toList(); + _error = ApiError(errors: jsonMap['error']); } /// String get authToken => _authToken; /// String get id => _id; /// - Map get sql => _sql; + Map get sql => _query; /// List> get data => _data; /// - List get errors => _errors; + ApiError get error => _error; + /// + bool get hasError => _error.isNotEmpty; /// @override String toString() { return '''$ApiReply { \t\tauthToken: $_authToken; \t\tid: $_id; -\t\tsql: $_sql; +\t\tquery: $_query; \t\tdata: $_data; -\t\terrors: $_errors; +\t\terror: $_error; \t}'''; } } diff --git a/pubspec.yaml b/pubspec.yaml index c5ef4a9..d94442e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ environment: dependencies: flutter: sdk: flutter - # logging: ^1.1.1 + logging: ^1.1.1 # shared_preferences: ^2.0.20 hmi_core: git: