platform/lib/rest.dart

102 lines
2.7 KiB
Dart
Raw Normal View History

2016-06-24 19:02:35 +00:00
part of angel_client;
2016-06-24 00:25:11 +00:00
_buildQuery(Map params) {
if (params == null || params == {})
return "";
String result = "";
return result;
}
const Map _readHeaders = const {
"Accept": "application/json"
};
const Map _writeHeaders = const {
"Accept": "application/json",
"Content-Type": "application/json"
};
2016-06-24 19:02:35 +00:00
class Rest extends Angel {
BaseClient client;
2016-06-24 21:06:57 +00:00
Rest(String path, BaseClient this.client) :super(path);
2016-06-24 19:02:35 +00:00
@override
2016-06-24 21:06:57 +00:00
RestService service(String path, {Type type}) {
2016-06-24 19:02:35 +00:00
String uri = path.replaceAll(new RegExp(r"(^\/)|(\/+$)"), "");
2016-06-24 21:06:57 +00:00
return new RestService._base("$basePath/$uri", client, type)
..app = this;
2016-06-24 19:02:35 +00:00
}
}
2016-06-24 00:25:11 +00:00
/// Queries an Angel service via REST.
class RestService extends Service {
String basePath;
BaseClient client;
2016-06-24 21:06:57 +00:00
Type outputType;
2016-06-24 00:25:11 +00:00
2016-06-24 21:06:57 +00:00
RestService._base(Pattern path, BaseClient this.client,
Type this.outputType) {
2016-06-24 00:25:11 +00:00
this.basePath = (path is RegExp) ? path.pattern : path;
}
2016-06-24 21:06:57 +00:00
_makeBody(data) {
if (outputType == null)
return JSON.encode(data);
else return god.serialize(data);
}
2016-06-24 00:25:11 +00:00
@override
Future<List> index([Map params]) async {
var response = await client.get(
"$basePath/${_buildQuery(params)}", headers: _readHeaders);
2016-06-24 21:06:57 +00:00
if (outputType == null)
return god.deserialize(response.body);
else {
return JSON.decode(response.body).map((x) =>
god.deserializeDatum(x, outputType: outputType)).toList();
}
2016-06-24 00:25:11 +00:00
}
@override
Future read(id, [Map params]) async {
var response = await client.get(
"$basePath/$id${_buildQuery(params)}", headers: _readHeaders);
2016-06-24 21:06:57 +00:00
return god.deserialize(response.body, outputType: outputType);
2016-06-24 00:25:11 +00:00
}
@override
Future create(data, [Map params]) async {
var response = await client.post(
2016-06-24 21:06:57 +00:00
"$basePath/${_buildQuery(params)}", body: _makeBody(data),
2016-06-24 00:25:11 +00:00
headers: _writeHeaders);
2016-06-24 21:06:57 +00:00
return god.deserialize(response.body, outputType: outputType);
2016-06-24 00:25:11 +00:00
}
@override
Future modify(id, data, [Map params]) async {
var response = await client.patch(
2016-06-24 21:06:57 +00:00
"$basePath/$id${_buildQuery(params)}", body: _makeBody(data),
2016-06-24 00:25:11 +00:00
headers: _writeHeaders);
2016-06-24 21:06:57 +00:00
return god.deserialize(response.body, outputType: outputType);
2016-06-24 00:25:11 +00:00
}
@override
Future update(id, data, [Map params]) async {
var response = await client.patch(
2016-06-24 21:06:57 +00:00
"$basePath/$id${_buildQuery(params)}", body: _makeBody(data),
2016-06-24 00:25:11 +00:00
headers: _writeHeaders);
2016-06-24 21:06:57 +00:00
return god.deserialize(response.body, outputType: outputType);
2016-06-24 00:25:11 +00:00
}
@override
Future remove(id, [Map params]) async {
var response = await client.delete(
"$basePath/$id${_buildQuery(params)}", headers: _readHeaders);
2016-06-24 21:06:57 +00:00
return god.deserialize(response.body, outputType: outputType);
2016-06-24 00:25:11 +00:00
}
}