From 98838cd8de6b4cdd726d27e9cfe856df1a63e00b Mon Sep 17 00:00:00 2001 From: regiostech Date: Thu, 23 Jun 2016 20:25:11 -0400 Subject: [PATCH] :) --- .gitignore | 1 + lib/angel_client.dart | 26 ++++++++++++++ lib/src/rest.dart | 80 +++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 12 +++++++ 4 files changed, 119 insertions(+) create mode 100644 lib/angel_client.dart create mode 100644 lib/src/rest.dart create mode 100644 pubspec.yaml diff --git a/.gitignore b/.gitignore index 7c280441..ea89ccf0 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/lib/angel_client.dart b/lib/angel_client.dart new file mode 100644 index 00000000..7982440c --- /dev/null +++ b/lib/angel_client.dart @@ -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 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]); +} \ No newline at end of file diff --git a/lib/src/rest.dart b/lib/src/rest.dart new file mode 100644 index 00000000..e1144be7 --- /dev/null +++ b/lib/src/rest.dart @@ -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 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); + } + + +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..56c70487 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,12 @@ +name: angel_client +version: 1.0.0-dev +description: Client library for the Angel framework. +author: Tobe O +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" \ No newline at end of file