Created
February 21, 2019 18:04
-
-
Save marcguilera/eae5ec88b2cf1d37698dd32a0bc0993c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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