Skip to content

Instantly share code, notes, and snippets.

@marcguilera
Created February 21, 2019 18:04
Show Gist options
  • Save marcguilera/eae5ec88b2cf1d37698dd32a0bc0993c to your computer and use it in GitHub Desktop.
Save marcguilera/eae5ec88b2cf1d37698dd32a0bc0993c to your computer and use it in GitHub Desktop.
abstract class Service<M> {
Future<M> getOne(int id);
Future<List<M>> getAll();
}
abstract class RestServiceBase<M> implements Service<M> {
final Injector _injector;
final String _endpoint;
String get _root => "${_injector.get<String>(name: "api_root")}/$_endpoint";
RestServiceBase(this._injector, this._endpoint);
@override
Future<M> getOne(int id) async {
final url = "$_root/$id";
final response = await _call(url) as Map;
return toModel(response);
}
@override
Future<List<M>> getAll() async {
final url = _root;
final response = await _call(url) as List;
return response.map((r) => toModel(r)).toList();
}
_call(String uri) async {
final response = await http.get(uri);
return json.decode(response.body);
}
M toModel(Map map);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment