Guest User

Untitled

a guest
Aug 12th, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. class PotatoDataSourceImpl implements PotatoDataSource {
  2. PotatoDataSourceImpl({@required this.client});
  3. final Dio client;
  4.  
  5. @override
  6. Future<Either<Exception, PotatoPriceList>> getPriceList() => _get('getPriceList');
  7.  
  8. @override
  9. Future<Either<Exception, PotatoCatalog>> getCatalog() => _get('getCatalog');
  10.  
  11. @override
  12. Future<Either<Exception, PotatoGoodPictureList>> getPictureList() => _get('getPictureList');
  13.  
  14. Future<Either<Exception, T>> _get<T>(String q) async {
  15. final response = await client
  16. .get<Map<String, dynamic>>(
  17. _query(q),
  18. options: Options(
  19. headers: {
  20. 'Authorization': 'Basic ${AppConfig.base64}',
  21. 'Content-Type': 'application/json',
  22. },
  23. ),
  24. )
  25. .catchError((e) => throw ServerException());
  26.  
  27. if (response.statusCode != 200) {
  28. throw ServerException();
  29. }
  30.  
  31. return Right(_fromJson<T>(response.data));
  32. }
  33. }
  34.  
  35. const _queries = {
  36. 'getPriceList': r"query1...", // разные запросы
  37. 'getCatalog': r"query2...", // разные запросы
  38. 'getPictureList': r"query3...", // разные запросы
  39. };
  40.  
  41. //
  42. // Utilits
  43. //
  44.  
  45. String _query(String q) => AppConfig.db.address + _queries[q];
  46.  
  47. final _factories = <Type, Function>{
  48. PotatoPriceList: (Map<String, dynamic> json) => PotatoPriceList.fromJson(json),
  49. PotatoCatalog: (Map<String, dynamic> json) => PotatoCatalog.fromJson(json),
  50. PotatoGoodPictureList: (Map<String, dynamic> json) => PotatoGoodPictureList.fromJson(json),
  51. };
  52.  
  53. T _fromJson<T>(Map<String, dynamic> json) {
  54. return _factories[T](json) as T;
  55. }
Add Comment
Please, Sign In to add comment