Skip to content

Commit

Permalink
ApiReply | extended message, keepAlive mode, debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed Nov 1, 2023
1 parent 874f106 commit 9a44904
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
10 changes: 9 additions & 1 deletion lib/src/core/api_query_type/executable_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ class ExecutableQuery implements ApiQueryType {
late String _id;
final String _script;
final Map<String, dynamic> _params;
final bool _keepAlive;
final bool _debug;
///
/// Prapares query for some executable
ExecutableQuery({
required String authToken,
required String script,
required Map<String, dynamic> params,
bool keepAlive = false,
bool debug = false,
}) :
_authToken = authToken,
_script = script,
_params = params;
_params = params,
_keepAlive = keepAlive,
_debug = debug;
///
@override
bool valid() {
Expand All @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions lib/src/core/api_query_type/python_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ class PythonQuery implements ApiQueryType {
final String _authToken;
late String _id;
final String _script;
final bool _keepAlive;
final bool _debug;
final Map<String, dynamic> _params;
///
/// Prapares query for some python script
PythonQuery({
required String authToken,
required String script,
required Map<String, dynamic> params,
bool keepAlive = false,
bool debug = false,
}) :
_authToken = authToken,
_script = script,
_params = params;
///
_params = params,
_keepAlive = keepAlive,
_debug = debug;
///
@override
bool valid() {
return true;
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion lib/src/core/api_query_type/sql_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions lib/src/reply/api_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

// import 'package:logging/logging.dart';

class ApiError {
// final _log = Logger('ApiError');
final Map<String, dynamic>? _errors;
///
ApiError({
required Map<String, dynamic>? 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}''';
}
}
36 changes: 18 additions & 18 deletions lib/src/reply/api_reply.dart
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
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<String, String> _sql;
late List<Map<String, dynamic>> _data;
late List<String> _errors;
late final String _authToken;
late final String _id;
late final Map<String, dynamic> _query;
late final List<Map<String, dynamic>> _data;
late final ApiError _error;
///
ApiReply({
required String authToken,
required String id,
required Map<String, String> sql,
required Map<String, dynamic> query,
required List<Map<String, dynamic>> data,
required List<String> 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');
final jsonMap = json.decode(jsonString);
_log.fine('.fromJson | jsonMap: $jsonMap');
_authToken = jsonMap['auth_token'];
_id = jsonMap['id'];
_sql = jsonMap['sql'] ?? {};
_query = jsonMap['query'] ?? {};
_data = (jsonMap['data'] as List<dynamic>).map((e) {
return (e as Map<dynamic, dynamic>).map((key, value) => MapEntry(key.toString(), value));
// final key = (e as MapEntry).key;
// final value = (e as MapEntry).value;
// return MapEntry<String, dynamic>(key.toString(), value);
}).toList();
_errors = (jsonMap['errors'] as List<dynamic>).map((e) => '$e').toList();
_error = ApiError(errors: jsonMap['error']);
}
///
String get authToken => _authToken;
///
String get id => _id;
///
Map<String, String> get sql => _sql;
Map<String, dynamic> get sql => _query;
///
List<Map<String, dynamic>> get data => _data;
///
List<String> 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}''';
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9a44904

Please sign in to comment.