platform/lib/rest.dart

88 lines
2.2 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;
Rest(String path, BaseClient this.client):super(path);
@override
RestService service(String path) {
String uri = path.replaceAll(new RegExp(r"(^\/)|(\/+$)"), "");
return new RestService._base("$basePath/$uri", client);
}
}
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 19:02:35 +00:00
RestService._base(Pattern path, BaseClient this.client) {
2016-06-24 00:25:11 +00:00
this.basePath = (path is RegExp) ? path.pattern : path;
}
@override
Future<List> index([Map params]) async {
var response = await client.get(
"$basePath/${_buildQuery(params)}", headers: _readHeaders);
return JSON.decode(response.body);
}
@override
Future read(id, [Map params]) async {
var response = await client.get(
"$basePath/$id${_buildQuery(params)}", headers: _readHeaders);
return JSON.decode(response.body);
}
@override
Future create(data, [Map params]) async {
var response = await client.post(
"$basePath/${_buildQuery(params)}", body: JSON.encode(data),
headers: _writeHeaders);
return JSON.decode(response.body);
}
@override
Future modify(id, data, [Map params]) async {
var response = await client.patch(
"$basePath/$id${_buildQuery(params)}", body: JSON.encode(data),
headers: _writeHeaders);
return JSON.decode(response.body);
}
@override
Future update(id, data, [Map params]) async {
var response = await client.patch(
"$basePath/$id${_buildQuery(params)}", body: JSON.encode(data),
headers: _writeHeaders);
return JSON.decode(response.body);
}
@override
Future remove(id, [Map params]) async {
var response = await client.delete(
"$basePath/$id${_buildQuery(params)}", headers: _readHeaders);
return JSON.decode(response.body);
}
}