platform/lib/browser.dart

93 lines
2.3 KiB
Dart
Raw Normal View History

2016-06-25 18:37:49 +00:00
/// Browser library for the Angel framework.
library angel_client.browser;
import 'dart:async';
import 'dart:convert' show JSON;
import 'dart:html';
2016-09-03 12:02:32 +00:00
import 'angel_client.dart';
export 'angel_client.dart';
2016-06-25 18:37:49 +00:00
_buildQuery(Map params) {
if (params == null || params == {})
return "";
String result = "";
return result;
}
/// Queries an Angel server via REST.
class Rest extends Angel {
Rest(String basePath) :super(basePath);
@override
RestService service(String path, {Type type}) {
String uri = path.replaceAll(new RegExp(r"(^\/)|(\/+$)"), "");
2016-09-03 12:02:32 +00:00
return new RestService("$basePath/$uri")
2016-06-25 18:37:49 +00:00
..app = this;
}
}
/// Queries an Angel service via REST.
class RestService extends Service {
String basePath;
2016-09-03 12:02:32 +00:00
RestService(Pattern path) {
2016-06-25 18:37:49 +00:00
this.basePath = (path is RegExp) ? path.pattern : path;
}
_makeBody(data) {
return JSON.encode(data);
}
2016-09-03 12:02:32 +00:00
HttpRequest buildRequest(String url,
2016-06-25 18:37:49 +00:00
{String method: "POST", bool write: true}) {
HttpRequest request = new HttpRequest();
request.open(method, url, async: false);
request.responseType = "json";
request.setRequestHeader("Accept", "application/json");
if (write)
request.setRequestHeader("Content-Type", "application/json");
return request;
}
@override
Future<List> index([Map params]) async {
return JSON.decode(
await HttpRequest.getString("$basePath/${_buildQuery(params)}"));
}
@override
Future read(id, [Map params]) async {
return JSON.decode(
await HttpRequest.getString("$basePath/$id${_buildQuery(params)}"));
}
@override
Future create(data, [Map params]) async {
2016-09-03 12:02:32 +00:00
var request = buildRequest("$basePath/${_buildQuery(params)}");
2016-06-25 18:37:49 +00:00
request.send(_makeBody(data));
return request.response;
}
@override
Future modify(id, data, [Map params]) async {
2016-09-03 12:02:32 +00:00
var request = buildRequest("$basePath/$id${_buildQuery(params)}", method: "PATCH");
2016-06-25 18:37:49 +00:00
request.send(_makeBody(data));
return request.response;
}
@override
Future update(id, data, [Map params]) async {
2016-09-03 12:02:32 +00:00
var request = buildRequest("$basePath/$id${_buildQuery(params)}");
2016-06-25 18:37:49 +00:00
request.send(_makeBody(data));
return request.response;
}
@override
Future remove(id, [Map params]) async {
2016-09-03 12:02:32 +00:00
var request = buildRequest("$basePath/$id${_buildQuery(params)}", method: "DELETE");
2016-06-25 18:37:49 +00:00
request.send();
return request.response;
}
}