platform/packages/client/lib/io.dart
2024-10-12 18:45:27 -07:00

74 lines
1.9 KiB
Dart

/// Command-line client library for the Protevus framework.
library angel_client.cli;
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:belatuk_json_serializer/belatuk_json_serializer.dart' as god;
import 'package:path/path.dart' as p;
import 'package:logging/logging.dart';
import 'protevus_client.dart';
import 'base_protevus_client.dart';
export 'protevus_client.dart';
/// Queries an Protevus server via REST.
class Rest extends BaseProtevusClient {
//final _log = Logger('REST');
final List<Service> _services = [];
Rest(String path) : super(http.Client() as http.BaseClient, path);
@override
Service<Id, Data> service<Id, Data>(String path,
{Type? type, ProtevusDeserializer? deserializer}) {
var url = baseUrl.replace(path: p.join(baseUrl.path, path));
var s = RestService<Id, Data>(client, this, url, type);
_services.add(s);
return s as Service<Id, Data>;
}
@override
Stream<String> authenticateViaPopup(String url,
{String eventName = 'token'}) {
throw UnimplementedError(
'Opening popup windows is not supported in the `dart:io` client.');
}
@override
Future close() async {
await super.close();
await Future.wait(_services.map((s) => s.close())).then((_) {
_services.clear();
});
}
}
/// Queries an Protevus service via REST.
class RestService<Id, Data> extends BaseProtevusService<Id, Data> {
final _log = Logger('RestService');
final Type? type;
RestService(super.client, super.app, super.url, this.type);
@override
Data? deserialize(x) {
_log.info(x);
if (type != null) {
return x.runtimeType == type
? x as Data?
: god.deserializeDatum(x, outputType: type) as Data?;
}
return x as Data?;
}
@override
String makeBody(x) {
_log.info(x);
if (type != null) {
return super.makeBody(god.serializeObject(x));
}
return super.makeBody(x);
}
}