From 59a4a22774aeafd217dcc37ec7ec154c4ce04dc2 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 10 Dec 2016 09:45:22 -0500 Subject: [PATCH] +17 --- README.md | 4 ++- lib/angel_client.dart | 2 +- lib/base_angel_client.dart | 57 ++++++++++++++++++++++++++++++++------ lib/io.dart | 7 +++-- pubspec.yaml | 4 ++- test/io_test.dart | 3 +- 6 files changed, 60 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 14a9ef28..9736cd33 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # angel_client -[![pub 1.0.0-dev+16](https://img.shields.io/badge/pub-1.0.0--dev+16-red.svg)](https://pub.dartlang.org/packages/angel_client) +[![pub 1.0.0-dev+17](https://img.shields.io/badge/pub-1.0.0--dev+17-red.svg)](https://pub.dartlang.org/packages/angel_client) ![build status](https://travis-ci.org/angel-dart/client.svg) Client library for the Angel framework. @@ -44,6 +44,8 @@ class Todo extends Model { } bar() async { + // By the next release, this will just be: + // app.service("todos") Service Todos = app.service("todos", type: Todo); List todos = await Todos.index(); diff --git a/lib/angel_client.dart b/lib/angel_client.dart index b603c430..ba6f3e6e 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -26,7 +26,7 @@ abstract class Angel { await configurer(this); } - Service service(Pattern path, {Type type}); + Service service(Pattern path, {Type type}); } /// Represents the result of authentication with an Angel server. diff --git a/lib/base_angel_client.dart b/lib/base_angel_client.dart index ff6f17e7..f6d0a5f8 100644 --- a/lib/base_angel_client.dart +++ b/lib/base_angel_client.dart @@ -11,6 +11,7 @@ import 'package:merge_map/merge_map.dart'; import 'angel_client.dart'; import 'auth_types.dart' as auth_types; +final RegExp straySlashes = new RegExp(r"(^/)|(/+$)"); const Map _readHeaders = const {'Accept': 'application/json'}; final Map _writeHeaders = mergeMap([ _readHeaders, @@ -23,7 +24,7 @@ _buildQuery(Map params) { List query = []; params.forEach((k, v) { - query.add('$k=$v'); + query.add('$k=${Uri.encodeQueryComponent(v.toString())}'); }); return '?' + query.join('&'); @@ -124,8 +125,8 @@ abstract class BaseAngelClient extends Angel { } @override - Service service(String path, {Type type}) { - String uri = path.replaceAll(new RegExp(r"(^/)|(/+$)"), ""); + Service service(String path, {Type type}) { + String uri = path.replaceAll(straySlashes, ""); return new BaseAngelService(client, this, '$basePath/$uri'); } } @@ -166,6 +167,16 @@ class BaseAngelService extends Service { return http.Response.fromStream(await client.send(request)); } + String _join(url) { + final head = basePath.replaceAll(new RegExp(r'/+$'), ''); + final tail = basePath.replaceAll(straySlashes, ''); + return '$head/$tail'; + } + + Future close() async { + client.close(); + } + Future send(http.BaseRequest request) { if (app.authToken != null && app.authToken.isNotEmpty) { request.headers['Authorization'] = 'Bearer ${app.authToken}'; @@ -174,6 +185,34 @@ class BaseAngelService extends Service { return client.send(request); } + Future delete(String url, + {Map headers}) async { + return client.delete(_join(url), headers: headers); + } + + Future get(String url, {Map headers}) async { + return client.get(_join(url), headers: headers); + } + + Future head(String url, {Map headers}) async { + return client.head(_join(url), headers: headers); + } + + Future patch(String url, + {body, Map headers}) async { + return client.patch(_join(url), body: body, headers: headers); + } + + Future post(String url, + {body, Map headers}) async { + return client.post(_join(url), body: body, headers: headers); + } + + Future put(String url, + {body, Map headers}) async { + return client.put(_join(url), body: body, headers: headers); + } + @override Future index([Map params]) async { final response = await sendUnstreamed( @@ -214,8 +253,8 @@ class BaseAngelService extends Service { @override Future create(data, [Map params]) async { - final response = await sendUnstreamed( - 'POST', '$basePath/${_buildQuery(params)}', _writeHeaders, makeBody(data)); + final response = await sendUnstreamed('POST', + '$basePath/${_buildQuery(params)}', _writeHeaders, makeBody(data)); try { if (response.statusCode != 200) { @@ -230,8 +269,8 @@ class BaseAngelService extends Service { @override Future modify(id, data, [Map params]) async { - final response = await sendUnstreamed( - 'PATCH', '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); + final response = await sendUnstreamed('PATCH', + '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); try { if (response.statusCode != 200) { @@ -246,8 +285,8 @@ class BaseAngelService extends Service { @override Future update(id, data, [Map params]) async { - final response = await sendUnstreamed( - 'POST', '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); + final response = await sendUnstreamed('POST', + '$basePath/$id${_buildQuery(params)}', _writeHeaders, makeBody(data)); try { if (response.statusCode != 200) { diff --git a/lib/io.dart b/lib/io.dart index 0e544e7e..9bd8d80d 100644 --- a/lib/io.dart +++ b/lib/io.dart @@ -13,9 +13,10 @@ class Rest extends BaseAngelClient { Rest(String path) : super(new http.Client(), path); @override - Service service(String path, {Type type}) { - String uri = path.replaceAll(new RegExp(r"(^/)|(/+$)"), ""); - return new RestService(client, this, "$basePath/$uri", type); + Service service(String path, {Type type}) { + String uri = path.replaceAll(straySlashes, ""); + return new RestService( + client, this, "$basePath/$uri", T != dynamic ? T : type); } } diff --git a/pubspec.yaml b/pubspec.yaml index d0e032d7..962ceef9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,10 @@ name: angel_client -version: 1.0.0-dev+16 +version: 1.0.0-dev+17 description: Client library for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_client +environment: + sdk: ">=1.21.0" dependencies: angel_framework: ">=1.0.0-dev <2.0.0" http: ">= 0.11.3 < 0.12.0" diff --git a/test/io_test.dart b/test/io_test.dart index d804a485..a54fd802 100644 --- a/test/io_test.dart +++ b/test/io_test.dart @@ -1,7 +1,6 @@ import 'dart:io'; import 'package:angel_client/io.dart' as client; import 'package:angel_framework/angel_framework.dart' as server; -import 'package:http/http.dart' as http; import 'package:json_god/json_god.dart' as god; import 'package:test/test.dart'; import 'shared.dart'; @@ -24,7 +23,7 @@ main() { clientApp = new client.Rest(url); clientPostcards = clientApp.service("postcards"); - clientTypedPostcards = clientApp.service("postcards", type: Postcard); + clientTypedPostcards = clientApp.service("postcards", type: Postcard); }); tearDown(() async {