This commit is contained in:
thosakwe 2016-12-10 09:45:22 -05:00
parent b0c5f16730
commit 59a4a22774
6 changed files with 60 additions and 17 deletions

View file

@ -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<Todo>("todos")
Service Todos = app.service("todos", type: Todo);
List<Todo> todos = await Todos.index();

View file

@ -26,7 +26,7 @@ abstract class Angel {
await configurer(this);
}
Service service(Pattern path, {Type type});
Service service<T>(Pattern path, {Type type});
}
/// Represents the result of authentication with an Angel server.

View file

@ -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<String, String> _readHeaders = const {'Accept': 'application/json'};
final Map<String, String> _writeHeaders = mergeMap([
_readHeaders,
@ -23,7 +24,7 @@ _buildQuery(Map params) {
List<String> 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<T>(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<http.StreamedResponse> 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<http.Response> delete(String url,
{Map<String, String> headers}) async {
return client.delete(_join(url), headers: headers);
}
Future<http.Response> get(String url, {Map<String, String> headers}) async {
return client.get(_join(url), headers: headers);
}
Future<http.Response> head(String url, {Map<String, String> headers}) async {
return client.head(_join(url), headers: headers);
}
Future<http.Response> patch(String url,
{body, Map<String, String> headers}) async {
return client.patch(_join(url), body: body, headers: headers);
}
Future<http.Response> post(String url,
{body, Map<String, String> headers}) async {
return client.post(_join(url), body: body, headers: headers);
}
Future<http.Response> put(String url,
{body, Map<String, String> headers}) async {
return client.put(_join(url), body: body, headers: headers);
}
@override
Future<List> 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) {

View file

@ -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<T>(String path, {Type type}) {
String uri = path.replaceAll(straySlashes, "");
return new RestService(
client, this, "$basePath/$uri", T != dynamic ? T : type);
}
}

View file

@ -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 <thosakwe@gmail.com>
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"

View file

@ -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<Postcard>("postcards", type: Postcard);
});
tearDown(() async {