platform/packages/client/lib/io.dart

75 lines
1.9 KiB
Dart
Raw Normal View History

2016-06-25 18:37:49 +00:00
/// Command-line client library for the Angel framework.
library angel_client.cli;
import 'dart:async';
2016-12-09 00:24:07 +00:00
import 'package:http/http.dart' as http;
2021-09-25 06:32:32 +00:00
import 'package:belatuk_json_serializer/belatuk_json_serializer.dart' as god;
2019-01-06 02:08:31 +00:00
import 'package:path/path.dart' as p;
2021-07-15 08:11:54 +00:00
import 'package:logging/logging.dart';
2021-05-15 06:53:03 +00:00
import 'angel3_client.dart';
2016-12-09 00:24:07 +00:00
import 'base_angel_client.dart';
2021-05-15 06:53:03 +00:00
export 'angel3_client.dart';
2016-06-25 18:37:49 +00:00
/// Queries an Angel server via REST.
2016-12-09 00:24:07 +00:00
class Rest extends BaseAngelClient {
2021-07-15 08:11:54 +00:00
//final _log = Logger('REST');
2017-06-03 17:43:01 +00:00
final List<Service> _services = [];
2021-03-05 07:51:48 +00:00
Rest(String path) : super(http.Client() as http.BaseClient, path);
2016-06-25 18:37:49 +00:00
@override
Service<Id, Data> service<Id, Data>(String path,
2021-04-10 13:22:20 +00:00
{Type? type, AngelDeserializer? deserializer}) {
2019-01-06 02:08:31 +00:00
var url = baseUrl.replace(path: p.join(baseUrl.path, path));
2021-03-05 07:51:48 +00:00
var s = RestService<Id, Data>(client, this, url, type);
2017-06-03 17:43:01 +00:00
_services.add(s);
2021-04-10 13:22:20 +00:00
return s as Service<Id, Data>;
2016-06-25 18:37:49 +00:00
}
2017-06-03 17:43:01 +00:00
2017-02-28 21:56:59 +00:00
@override
2019-01-06 02:08:31 +00:00
Stream<String> authenticateViaPopup(String url,
{String eventName = 'token'}) {
2021-03-05 07:51:48 +00:00
throw UnimplementedError(
2018-08-26 22:41:01 +00:00
'Opening popup windows is not supported in the `dart:io` client.');
2017-02-28 21:56:59 +00:00
}
2017-06-03 17:43:01 +00:00
2021-03-05 07:51:48 +00:00
@override
2017-06-03 17:43:01 +00:00
Future close() async {
2019-04-20 15:21:15 +00:00
await super.close();
await Future.wait(_services.map((s) => s.close())).then((_) {
2017-06-03 17:43:01 +00:00
_services.clear();
});
}
2016-06-25 18:37:49 +00:00
}
/// Queries an Angel service via REST.
class RestService<Id, Data> extends BaseAngelService<Id, Data> {
2021-07-15 08:11:54 +00:00
final _log = Logger('RestService');
2021-04-10 13:22:20 +00:00
final Type? type;
2016-06-25 18:37:49 +00:00
2023-12-24 01:52:57 +00:00
RestService(super.client, super.app, super.url, this.type);
2016-06-25 18:37:49 +00:00
2017-06-03 17:43:01 +00:00
@override
2021-04-10 13:22:20 +00:00
Data? deserialize(x) {
2021-07-15 08:11:54 +00:00
_log.info(x);
2016-12-09 00:24:07 +00:00
if (type != null) {
2017-01-25 23:25:31 +00:00
return x.runtimeType == type
2021-04-10 13:22:20 +00:00
? x as Data?
: god.deserializeDatum(x, outputType: type) as Data?;
2016-12-09 00:24:07 +00:00
}
2021-04-10 13:22:20 +00:00
return x as Data?;
2016-06-25 18:37:49 +00:00
}
@override
2021-03-05 07:51:48 +00:00
String makeBody(x) {
2021-07-15 08:11:54 +00:00
_log.info(x);
2016-12-09 00:24:07 +00:00
if (type != null) {
return super.makeBody(god.serializeObject(x));
2016-06-25 18:37:49 +00:00
}
2016-12-09 00:24:07 +00:00
return super.makeBody(x);
2016-06-25 18:37:49 +00:00
}
}