Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PotatoDataSourceImpl implements PotatoDataSource {
- PotatoDataSourceImpl({@required this.client});
- final Dio client;
- @override
- Future<Either<Exception, PotatoPriceList>> getPriceList() => _get('getPriceList');
- @override
- Future<Either<Exception, PotatoCatalog>> getCatalog() => _get('getCatalog');
- @override
- Future<Either<Exception, PotatoGoodPictureList>> getPictureList() => _get('getPictureList');
- Future<Either<Exception, T>> _get<T>(String q) async {
- final response = await client
- .get<Map<String, dynamic>>(
- _query(q),
- options: Options(
- headers: {
- 'Authorization': 'Basic ${AppConfig.base64}',
- 'Content-Type': 'application/json',
- },
- ),
- )
- .catchError((e) => throw ServerException());
- if (response.statusCode != 200) {
- throw ServerException();
- }
- return Right(_fromJson<T>(response.data));
- }
- }
- const _queries = {
- 'getPriceList': r"query1...", // разные запросы
- 'getCatalog': r"query2...", // разные запросы
- 'getPictureList': r"query3...", // разные запросы
- };
- //
- // Utilits
- //
- String _query(String q) => AppConfig.db.address + _queries[q];
- final _factories = <Type, Function>{
- PotatoPriceList: (Map<String, dynamic> json) => PotatoPriceList.fromJson(json),
- PotatoCatalog: (Map<String, dynamic> json) => PotatoCatalog.fromJson(json),
- PotatoGoodPictureList: (Map<String, dynamic> json) => PotatoGoodPictureList.fromJson(json),
- };
- T _fromJson<T>(Map<String, dynamic> json) {
- return _factories[T](json) as T;
- }
Add Comment
Please, Sign In to add comment