More
This commit is contained in:
parent
98838cd8de
commit
d588411d67
5 changed files with 70 additions and 8 deletions
|
@ -2,7 +2,18 @@
|
|||
library angel_client;
|
||||
|
||||
import 'dart:async';
|
||||
export 'src/rest.dart';
|
||||
import 'dart:convert' show JSON;
|
||||
import 'package:http/http.dart';
|
||||
part 'rest.dart';
|
||||
|
||||
|
||||
/// Represents an Angel server that we are querying.
|
||||
abstract class Angel {
|
||||
String basePath;
|
||||
|
||||
Angel(String this.basePath);
|
||||
Service service(Pattern path);
|
||||
}
|
||||
|
||||
/// Queries a service on an Angel server, with the same API.
|
||||
abstract class Service {
|
||||
|
|
|
@ -1,9 +1,4 @@
|
|||
library angel_client.rest;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert' show JSON;
|
||||
import 'package:http/http.dart';
|
||||
import '../angel_client.dart';
|
||||
part of angel_client;
|
||||
|
||||
_buildQuery(Map params) {
|
||||
if (params == null || params == {})
|
||||
|
@ -22,12 +17,24 @@ const Map _writeHeaders = const {
|
|||
"Content-Type": "application/json"
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// Queries an Angel service via REST.
|
||||
class RestService extends Service {
|
||||
String basePath;
|
||||
BaseClient client;
|
||||
|
||||
RestService(Pattern path, BaseClient this.client) {
|
||||
RestService._base(Pattern path, BaseClient this.client) {
|
||||
this.basePath = (path is RegExp) ? path.pattern : path;
|
||||
}
|
||||
|
1
test/packages
Symbolic link
1
test/packages
Symbolic link
|
@ -0,0 +1 @@
|
|||
../packages
|
37
test/rest.dart
Normal file
37
test/rest.dart
Normal file
|
@ -0,0 +1,37 @@
|
|||
import 'dart:io';
|
||||
import 'package:angel_client/angel_client.dart' as client;
|
||||
import 'package:angel_framework/angel_framework.dart' as server;
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:test/test.dart';
|
||||
import 'shared.dart';
|
||||
|
||||
main() {
|
||||
group("rest", () {
|
||||
server.Angel serverApp = new server.Angel();
|
||||
server.HookedService serverPostcards;
|
||||
client.Angel clientApp;
|
||||
client.Service clientPostcards;
|
||||
HttpServer httpServer;
|
||||
|
||||
setUp(() async {
|
||||
httpServer =
|
||||
await serverApp.startServer(InternetAddress.LOOPBACK_IP_V4, 3000);
|
||||
serverApp.use("/postcards", new server.MemoryService<Postcard>());
|
||||
serverPostcards = serverApp.service("postcards");
|
||||
|
||||
clientApp = new client.Rest("http://localhost:3000", new http.Client());
|
||||
clientPostcards = clientApp.service("postcards");
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await httpServer.close(force: true);
|
||||
});
|
||||
|
||||
test("index", () async {
|
||||
Postcard niagaraFalls = await serverPostcards.create(
|
||||
new Postcard(location: "Niagara Falls", message: "Missing you!"));
|
||||
List<Map> indexed = await clientPostcards.index();
|
||||
print(indexed);
|
||||
});
|
||||
});
|
||||
}
|
6
test/shared.dart
Normal file
6
test/shared.dart
Normal file
|
@ -0,0 +1,6 @@
|
|||
class Postcard {
|
||||
String location;
|
||||
String message;
|
||||
|
||||
Postcard({String this.location, String this.message});
|
||||
}
|
Loading…
Reference in a new issue