platform/lib/angel_client.dart

110 lines
2.8 KiB
Dart
Raw Normal View History

2016-06-24 00:25:11 +00:00
/// Client library for the Angel framework.
library angel_client;
import 'dart:async';
2016-12-09 00:24:07 +00:00
import 'dart:convert';
2016-12-10 17:15:54 +00:00
import 'package:http/src/response.dart' as http;
2016-12-09 00:24:07 +00:00
export 'package:angel_framework/src/http/angel_http_exception.dart';
2016-06-24 19:02:35 +00:00
2016-06-24 21:06:57 +00:00
/// A function that configures an [Angel] client in some way.
typedef Future AngelConfigurer(Angel app);
2016-06-24 19:02:35 +00:00
2016-12-13 16:35:35 +00:00
/// A function that deserializes data received from the server.
///
/// This is only really necessary in the browser, where `json_god`
/// doesn't work.
typedef AngelDeserializer(x);
2016-06-24 19:02:35 +00:00
/// Represents an Angel server that we are querying.
abstract class Angel {
2016-12-09 00:24:07 +00:00
String get authToken;
2016-06-24 19:02:35 +00:00
String basePath;
Angel(String this.basePath);
2016-06-24 21:06:57 +00:00
2016-11-29 00:42:02 +00:00
Future<AngelAuthResult> authenticate(
2016-12-03 18:21:44 +00:00
{String type,
2016-11-29 00:42:02 +00:00
credentials,
String authEndpoint: '/auth',
String reviveEndpoint: '/auth/token'});
2016-11-28 03:28:41 +00:00
2016-12-10 14:50:05 +00:00
Future close();
2016-06-24 21:06:57 +00:00
/// Applies an [AngelConfigurer] to this instance.
Future configure(AngelConfigurer configurer) async {
await configurer(this);
}
2016-12-13 16:35:35 +00:00
Service service<T>(String path, {Type type, AngelDeserializer deserializer});
2016-12-10 17:15:54 +00:00
Future<http.Response> delete(String url,
{Map<String, String> headers});
Future<http.Response> get(String url, {Map<String, String> headers});
Future<http.Response> head(String url, {Map<String, String> headers});
Future<http.Response> patch(String url,
{body, Map<String, String> headers});
Future<http.Response> post(String url,
{body, Map<String, String> headers});
Future<http.Response> put(String url,
{body, Map<String, String> headers});
2016-06-24 19:02:35 +00:00
}
2016-06-24 00:25:11 +00:00
2016-11-28 03:28:41 +00:00
/// Represents the result of authentication with an Angel server.
2016-12-09 00:24:07 +00:00
class AngelAuthResult {
String _token;
final Map<String, dynamic> data = {};
String get token => _token;
AngelAuthResult({String token, Map<String, dynamic> data: const {}}) {
_token = token;
this.data.addAll(data ?? {});
}
factory AngelAuthResult.fromMap(Map data) {
final result = new AngelAuthResult();
if (data is Map && data.containsKey('token') && data['token'] is String)
result._token = data['token'];
if (data is Map) result.data.addAll(data['data'] ?? {});
2016-12-03 18:21:44 +00:00
2016-12-09 00:24:07 +00:00
return result;
}
factory AngelAuthResult.fromJson(String json) =>
new AngelAuthResult.fromMap(JSON.decode(json));
Map<String, dynamic> toJson() {
return {'token': token, 'data': data};
}
2016-11-28 03:28:41 +00:00
}
2016-06-24 00:25:11 +00:00
/// Queries a service on an Angel server, with the same API.
abstract class Service {
2016-06-24 21:06:57 +00:00
/// The Angel instance powering this service.
2016-11-28 03:28:41 +00:00
Angel get app;
2016-06-24 21:06:57 +00:00
2016-06-24 00:25:11 +00:00
/// Retrieves all resources.
2017-02-12 19:58:18 +00:00
Future index([Map params]);
2016-06-24 00:25:11 +00:00
/// 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]);
2016-09-03 12:02:32 +00:00
}