Skip to content

Commit

Permalink
Merge pull request #17 from a-givertzman/ApiQuery-&-ApiRequest-refact…
Browse files Browse the repository at this point in the history
…oring

ApiQuery & ApiRequest refactoring
  • Loading branch information
a-givertzman authored Dec 25, 2023
2 parents 8ffa7dc + 0d7da4b commit 7f06331
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 50 deletions.
6 changes: 3 additions & 3 deletions lib/src/api_client/access/sql_read.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:ext_rw/ext_rw.dart';
import 'package:ext_rw/src/api_client/access/sql_builder.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_log.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
Expand Down Expand Up @@ -46,14 +45,15 @@ class SqlRead<T extends SchemaEntry, P> implements SchemaRead<T, P> {
Future<Result<List<T>, Failure>> _fetch(Sql sql) {
final request = ApiRequest(
address: _address,
authToken: _authToken,
debug: _debug,
query: SqlQuery(
authToken: _authToken,
database: _database,
sql: sql.build(),
keepAlive: _keepAlive,
debug: _debug,
),
);
_log.debug("._fetch | request: $request");
return request.fetch().then((result) {
return switch (result) {
Ok(value :final reply) => () {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/api_client/access/sql_write.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:ext_rw/ext_rw.dart';
import 'package:ext_rw/src/api_client/access/sql_builder.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_log.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
Expand Down Expand Up @@ -131,14 +130,15 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
Future<Result<List<T>, Failure>> _fetchWith(Sql sql) {
final request = ApiRequest(
address: _address,
authToken: _authToken,
debug: _debug,
query: SqlQuery(
authToken: _authToken,
database: _database,
sql: sql.build(),
keepAlive: _keepAlive,
debug: _debug,
),
);
_log.debug("._fetchWith | request: $request");
return request.fetch().then((result) {
return switch (result) {
Ok(:final value) => () {
Expand Down
4 changes: 1 addition & 3 deletions lib/src/api_client/query/api_query_type.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
abstract class ApiQueryType {
bool valid();
///
String buildJson();
///
String get authToken;
String buildJson({String authToken = '', bool debug = false});
///
String get id;
}
17 changes: 4 additions & 13 deletions lib/src/api_client/query/executable_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ import 'package:ext_rw/src/api_client/query/api_query_type.dart';
import 'package:uuid/uuid.dart';

class ExecutableQuery implements ApiQueryType {
final String _authToken;
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,
_keepAlive = keepAlive,
_debug = debug;
_keepAlive = keepAlive;
///
@override
bool valid() {
Expand All @@ -32,13 +26,13 @@ class ExecutableQuery implements ApiQueryType {
}
///
@override
String buildJson() {
String buildJson({String authToken = '', bool debug = false}) {
_id = const Uuid().v1();
final jsonString = json.encode({
'authToken': _authToken,
'authToken': authToken,
'id': _id,
'keepAlive': _keepAlive,
'debug': _debug,
'debug': debug,
'executable': {
'script': _script,
'params': _params,
Expand All @@ -48,9 +42,6 @@ class ExecutableQuery implements ApiQueryType {
}
///
@override
String get authToken => _authToken;
///
@override
String get id => _id;
///
String get script => _script;
Expand Down
17 changes: 4 additions & 13 deletions lib/src/api_client/query/python_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ import 'package:ext_rw/src/api_client/query/api_query_type.dart';
import 'package:uuid/uuid.dart';

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,
_keepAlive = keepAlive,
_debug = debug;
_keepAlive = keepAlive;
///
@override
bool valid() {
Expand All @@ -32,13 +26,13 @@ class PythonQuery implements ApiQueryType {
}
///
@override
String buildJson() {
String buildJson({String authToken = '', bool debug = false}) {
_id = const Uuid().v1();
final jsonString = json.encode({
'authToken': _authToken,
'authToken': authToken,
'id': _id,
'keepAlive': _keepAlive,
'debug': _debug,
'debug': debug,
'python': {
'script': _script,
'params': _params,
Expand All @@ -48,9 +42,6 @@ class PythonQuery implements ApiQueryType {
}
///
@override
String get authToken => _authToken;
///
@override
String get id => _id;
///
String get script => _script;
Expand Down
17 changes: 4 additions & 13 deletions lib/src/api_client/query/sql_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ import 'package:ext_rw/src/api_client/query/api_query_type.dart';
import 'package:uuid/uuid.dart';

class SqlQuery implements ApiQueryType {
final String _authToken;
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,
_keepAlive = keepAlive,
_debug = debug;
_keepAlive = keepAlive;
///
@override
bool valid() {
Expand All @@ -32,13 +26,13 @@ class SqlQuery implements ApiQueryType {
}
///
@override
String buildJson() {
String buildJson({String authToken = '', bool debug = false}) {
_id = const Uuid().v1();
final jsonString = json.encode({
'authToken': _authToken,
'authToken': authToken,
'id': _id,
'keepAlive': _keepAlive,
'debug': _debug,
'debug': debug,
'sql': {
'database': _database,
'sql': _sql,
Expand All @@ -48,9 +42,6 @@ class SqlQuery implements ApiQueryType {
}
///
@override
String get authToken => _authToken;
///
@override
String get id => _id;
///
String get database => _database;
Expand Down
12 changes: 10 additions & 2 deletions lib/src/api_client/request/api_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ import 'package:hmi_core/hmi_core_result_new.dart';
class ApiRequest {
static final _log = const Log('ApiRequest')..level = LogLevel.info;
final ApiAddress _address;
final String _authToken;
final ApiQueryType _query;
final bool _debug;
///
ApiRequest({
required String authToken,
required ApiAddress address,
required ApiQueryType query,
bool debug = false,
}) :
_authToken = authToken,
_address = address,
_query = query;
_query = query,
_debug = debug;
///
String get authToken => _authToken;
///
/// sends created request to the remote
/// returns reply if exists
Future<Result<ApiReply, Failure>> fetch() async {
final query = _query.buildJson();
final query = _query.buildJson(authToken: _authToken, debug: _debug);
final bytes = utf8.encode(query);
if (kIsWeb) {
return _fetchWebSocket(bytes);
Expand Down
1 change: 1 addition & 0 deletions lib/src/table_schema/table_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TableSchema<T extends SchemaEntry, P> implements TableSchemaAbstract<T, P>
Future<Result<List<T>, Failure>> fetch(P params) async {
final read = _read;
return read.fetch(params).then((result) {
_log.debug('.fetch | result: $result');
return switch(result) {
Ok<List<T>, Failure>(:final value) => () {
_entries.clear();
Expand Down

0 comments on commit 7f06331

Please sign in to comment.