|
| 1 | +import "dart:convert"; |
| 2 | + |
| 3 | +import "package:dart_api_service/dart_api_service.dart"; |
| 4 | +import "package:dart_api_service/dart_api_service.dart" as http; |
| 5 | +import "package:flutter_catalog_interface/flutter_catalog_interface.dart"; |
| 6 | +import "package:flutter_catalog_rest_api/src/rest_converters.dart"; |
| 7 | + |
| 8 | +export "package:dart_api_service/dart_api_service.dart" show Client; |
| 9 | + |
| 10 | +/// An implementation of [CatalogRepository] that uses a RESTful API. |
| 11 | +class RestCatalogRepository extends HttpApiService<List<CatalogItem>> |
| 12 | + implements CatalogRepository { |
| 13 | + /// Creates a [RestCatalogRepository]. |
| 14 | + /// |
| 15 | + /// [baseUrl] is the base URL for the catalog API. |
| 16 | + RestCatalogRepository({ |
| 17 | + required super.baseUrl, |
| 18 | + super.apiResponseConverter, |
| 19 | + super.authenticationService, |
| 20 | + super.client, |
| 21 | + super.defaultHeaders, |
| 22 | + this.apiPrefix = "", |
| 23 | + }); |
| 24 | + |
| 25 | + /// The prefix for the API endpoints. |
| 26 | + final String apiPrefix; |
| 27 | + |
| 28 | + Endpoint get _baseEndpoint => endpoint(apiPrefix); |
| 29 | + |
| 30 | + @override |
| 31 | + Future<List<CatalogItem>> fetchCatalogItems({ |
| 32 | + required String userId, |
| 33 | + LatLng? userLocation, |
| 34 | + Map<String, dynamic>? filters, |
| 35 | + int? limit, |
| 36 | + int? offset, |
| 37 | + }) async { |
| 38 | + var catalogEndpoint = _baseEndpoint.child("/catalog/catalog-items"); |
| 39 | + |
| 40 | + var queryParameters = <String, dynamic>{"userId": userId}; |
| 41 | + if (userLocation != null) { |
| 42 | + queryParameters["latitude"] = userLocation.latitude.toString(); |
| 43 | + queryParameters["longitude"] = userLocation.longitude.toString(); |
| 44 | + } |
| 45 | + if (filters != null) { |
| 46 | + queryParameters["filters"] = jsonEncode(filters); |
| 47 | + } |
| 48 | + if (limit != null) { |
| 49 | + queryParameters["limit"] = limit.toString(); |
| 50 | + } |
| 51 | + if (offset != null) { |
| 52 | + queryParameters["offset"] = offset.toString(); |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + var response = await catalogEndpoint.get( |
| 57 | + queryParameters: queryParameters, |
| 58 | + ); |
| 59 | + |
| 60 | + if (response.result != null) { |
| 61 | + return response.result!; |
| 62 | + } else { |
| 63 | + throw ApiException( |
| 64 | + inner: response.inner, |
| 65 | + error: "No catalog items found, but request succeeded.", |
| 66 | + ); |
| 67 | + } |
| 68 | + } on ApiException { |
| 69 | + rethrow; |
| 70 | + } on Exception catch (e, s) { |
| 71 | + throw ApiException( |
| 72 | + inner: http.Response("Unexpected error: $e", 500), |
| 73 | + error: e, |
| 74 | + stackTrace: s, |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + Future<CatalogItem?> fetchCatalogItemById(String id, String userId) async { |
| 81 | + var itemEndpoint = _baseEndpoint |
| 82 | + .child("/catalog/catalog-items/:id") |
| 83 | + .withVariables({"id": id}).withConverter(catalogItemConverter); |
| 84 | + |
| 85 | + try { |
| 86 | + var response = await itemEndpoint.get(); |
| 87 | + |
| 88 | + if (response.statusCode == 200 && response.result != null) { |
| 89 | + return response.result!; |
| 90 | + } else if (response.statusCode == 404) { |
| 91 | + return null; |
| 92 | + } else { |
| 93 | + throw ApiException(inner: response.inner); |
| 94 | + } |
| 95 | + } on ApiException { |
| 96 | + rethrow; |
| 97 | + } on Exception catch (e, s) { |
| 98 | + throw ApiException( |
| 99 | + inner: http.Response("Unexpected error: $e", 500), |
| 100 | + error: e, |
| 101 | + stackTrace: s, |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @override |
| 107 | + Future<void> toggleFavorite(String itemId, String userId) async { |
| 108 | + var favoriteEndpoint = _baseEndpoint |
| 109 | + .child("/catalog/catalog-items/:itemId/favorite") |
| 110 | + .withVariables({"itemId": itemId}).withConverter( |
| 111 | + const NoOpConverter(), |
| 112 | + ); |
| 113 | + |
| 114 | + try { |
| 115 | + await favoriteEndpoint.post( |
| 116 | + requestModel: {"userId": userId}, |
| 117 | + ); |
| 118 | + } on ApiException { |
| 119 | + rethrow; |
| 120 | + } on Exception catch (e, s) { |
| 121 | + throw ApiException( |
| 122 | + inner: http.Response("Unexpected error: $e", 500), |
| 123 | + error: e, |
| 124 | + stackTrace: s, |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments