This commit is contained in:
regiostech 2016-06-23 20:25:11 -04:00
parent 7a2a4e7552
commit 98838cd8de
4 changed files with 119 additions and 0 deletions

1
.gitignore vendored
View file

@ -25,3 +25,4 @@ doc/api/
# Don't commit pubspec lock file
# (Library packages only! Remove pattern if developing an application package)
pubspec.lock
.idea

26
lib/angel_client.dart Normal file
View file

@ -0,0 +1,26 @@
/// Client library for the Angel framework.
library angel_client;
import 'dart:async';
export 'src/rest.dart';
/// Queries a service on an Angel server, with the same API.
abstract class Service {
/// Retrieves all resources.
Future<List> index([Map params]);
/// Retrieves the desired resource.
Future read(id, [Map params]);
/// Creates a resource.
Future create(data, [Map params]);
/// Modifies a resource.
Future modify(id, data, [Map params]);
/// Overwrites a resource.
Future update(id, data, [Map params]);
/// Removes the given resource.
Future remove(id, [Map params]);
}

80
lib/src/rest.dart Normal file
View file

@ -0,0 +1,80 @@
library angel_client.rest;
import 'dart:async';
import 'dart:convert' show JSON;
import 'package:http/http.dart';
import '../angel_client.dart';
_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"
};
/// Queries an Angel service via REST.
class RestService extends Service {
String basePath;
BaseClient client;
RestService(Pattern path, BaseClient this.client) {
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);
}
}

12
pubspec.yaml Normal file
View file

@ -0,0 +1,12 @@
name: angel_client
version: 1.0.0-dev
description: Client library for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_client
dependencies:
json_god: ">=2.0.0-beta <3.0.0"
merge_map: ">=1.0.0 <2.0.0"
dev_dependencies:
angel_framework: ">=1.0.0-dev <2.0.0"
http: ">= 0.11.3 < 0.12.0"
test: ">= 0.12.13 < 0.13.0"