platform/lib/io.dart

70 lines
1.7 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;
2016-06-25 18:37:49 +00:00
import 'package:json_god/json_god.dart' as god;
2016-09-03 12:02:32 +00:00
import 'angel_client.dart';
2016-12-09 00:24:07 +00:00
import 'base_angel_client.dart';
2016-09-03 12:02:32 +00:00
export 'angel_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 {
Rest(String path) : super(new http.Client(), path);
2016-06-25 18:37:49 +00:00
@override
2016-12-09 00:24:07 +00:00
Service service(String path, {Type type}) {
String uri = path.replaceAll(new RegExp(r"(^/)|(/+$)"), "");
return new RestService(client, this, "$basePath/$uri", type);
2016-06-25 18:37:49 +00:00
}
}
/// Queries an Angel service via REST.
2016-12-09 00:24:07 +00:00
class RestService extends BaseAngelService {
final Type type;
2016-06-25 18:37:49 +00:00
2016-12-09 00:24:07 +00:00
RestService(http.BaseClient client, Angel app, String url, this.type)
: super(client, app, url);
2016-06-25 18:37:49 +00:00
2016-12-09 00:24:07 +00:00
deserialize(x) {
if (type != null) {
return god.deserializeDatum(x, outputType: type);
}
return x;
2016-06-25 18:37:49 +00:00
}
@override
2016-12-09 00:24:07 +00:00
makeBody(x) {
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
}
@override
2016-12-09 00:24:07 +00:00
Future<List> index([Map params]) async {
final items = await super.index(params);
return items.map(deserialize).toList();
2016-06-25 18:37:49 +00:00
}
@override
2016-12-09 00:24:07 +00:00
Future read(id, [Map params]) => super.read(id, params).then(deserialize);
2016-06-25 18:37:49 +00:00
@override
2016-12-09 00:24:07 +00:00
Future create(data, [Map params]) =>
super.create(data, params).then(deserialize);
2016-06-25 18:37:49 +00:00
@override
2016-12-09 00:24:07 +00:00
Future modify(id, data, [Map params]) =>
super.modify(id, data, params).then(deserialize);
2016-06-25 18:37:49 +00:00
@override
2016-12-09 00:24:07 +00:00
Future update(id, data, [Map params]) =>
super.update(id, data, params).then(deserialize);
@override
Future remove(id, [Map params]) => super.remove(id, params).then(deserialize);
2016-06-25 18:37:49 +00:00
}