diff --git a/lib/src/api/requests/request_handler.dart b/lib/src/api/requests/request_handler.dart index f1d6aa7..6571a97 100644 --- a/lib/src/api/requests/request_handler.dart +++ b/lib/src/api/requests/request_handler.dart @@ -25,8 +25,8 @@ class RequestHandler { dynamic body, String contentType = 'application/json'}) async { try { - final Response response = - await route.submit(routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType); + final Response response = await route.submit( + routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType); return RestResponse(data: mapper.call(response.data), statusCode: response.statusCode); } on DioException catch (e) { return _handleDioException(e); @@ -61,8 +61,8 @@ class RequestHandler { Map errorMap = const {}, String contentType = 'application/json'}) async { try { - final Response response = - await route.submit(routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType); + final Response response = await route.submit( + routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType); return RestResponse(data: true, statusCode: response.statusCode); } on DioException catch (e) { return _handleDioException(e); @@ -97,8 +97,8 @@ class RequestHandler { dynamic body, String contentType = 'application/json'}) async { try { - final Response response = - await route.submit(routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType); + final Response response = await route.submit( + routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType); if (response.data is! List) { throw StateError('Received response is not a list!'); } diff --git a/lib/src/api/restrr.dart b/lib/src/api/restrr.dart index d05ea81..9422530 100644 --- a/lib/src/api/restrr.dart +++ b/lib/src/api/restrr.dart @@ -73,7 +73,8 @@ abstract class Restrr { /* Currencies */ - Future createCurrency({required String name, required String symbol, required int decimalPlaces, String? isoCode}); + Future createCurrency( + {required String name, required String symbol, required int decimalPlaces, String? isoCode}); List getCurrencies(); diff --git a/lib/src/api/restrr_builder.dart b/lib/src/api/restrr_builder.dart index 8745e38..75dcade 100644 --- a/lib/src/api/restrr_builder.dart +++ b/lib/src/api/restrr_builder.dart @@ -42,13 +42,16 @@ class RestrrBuilder { }); } - Future _handleAuthProcess({required Future> Function(RestrrImpl) authFunction}) async { + Future _handleAuthProcess( + {required Future> Function(RestrrImpl) authFunction}) async { // check if the URI is valid and the API is healthy final ServerInfo statusResponse = await Restrr.checkUri(uri, isWeb: options.isWeb); Restrr.log.config('Host: $uri, API v${statusResponse.apiVersion}'); // build api instance final RestrrImpl apiImpl = RestrrImpl( - options: options, routeOptions: RouteOptions(hostUri: uri, apiVersion: statusResponse.apiVersion), eventMap: _eventMap); + options: options, + routeOptions: RouteOptions(hostUri: uri, apiVersion: statusResponse.apiVersion), + eventMap: _eventMap); // call auth function final RestResponse response = await authFunction(apiImpl); if (response.hasError) { @@ -60,11 +63,12 @@ class RestrrBuilder { apiImpl.session = response.data! as Session; /// Retrieve all accounts & currencies to make them available in the cache - final List accounts = await RequestUtils.fetchAllPaginated(apiImpl, await apiImpl.retrieveAllAccounts(limit: 50)); + final List accounts = + await RequestUtils.fetchAllPaginated(apiImpl, await apiImpl.retrieveAllAccounts(limit: 50)); Restrr.log.info('Cached ${accounts.length} account(s)'); - final List currencies = - await RequestUtils.fetchAllPaginated(apiImpl, await apiImpl.retrieveAllCurrencies(limit: 50)); + final List currencies = await RequestUtils.fetchAllPaginated( + apiImpl, await apiImpl.retrieveAllCurrencies(limit: 50)); Restrr.log.info('Cached ${currencies.length} currencies'); apiImpl.eventHandler.fire(ReadyEvent(api: apiImpl)); diff --git a/lib/src/internal/cache/cache_view.dart b/lib/src/internal/cache/cache_view.dart index 0b9e556..763833e 100644 --- a/lib/src/internal/cache/cache_view.dart +++ b/lib/src/internal/cache/cache_view.dart @@ -5,7 +5,8 @@ class EntityCacheView, ID extends EntityId> ext EntityCacheView(RestrrImpl api) : super(api, valueFunction: (entity) => entity.id.value); } -class PageCacheView, ID extends EntityId> extends MapCacheView<(int, int), Paginated> { +class PageCacheView, ID extends EntityId> + extends MapCacheView<(int, int), Paginated> { PageCacheView(RestrrImpl api) : super(api, valueFunction: (page) => (page.pageNumber, page.limit)); } diff --git a/lib/src/internal/entities/account_impl.dart b/lib/src/internal/entities/account_impl.dart index 95dc9bf..2b6d96a 100644 --- a/lib/src/internal/entities/account_impl.dart +++ b/lib/src/internal/entities/account_impl.dart @@ -57,7 +57,8 @@ class AccountImpl extends RestrrEntityImpl implements Accoun cacheView: api.transactionCache); @override - Future update({String? name, String? description, String? iban, int? originalBalance, Id? currencyId}) async { + Future update( + {String? name, String? description, String? iban, int? originalBalance, Id? currencyId}) async { if (name == null && description == null && iban == null && originalBalance == null && currencyId == null) { throw ArgumentError('At least one field must be set'); } diff --git a/lib/src/internal/entities/currency/currency_impl.dart b/lib/src/internal/entities/currency/currency_impl.dart index 3c7a60b..a1f60b0 100644 --- a/lib/src/internal/entities/currency/currency_impl.dart +++ b/lib/src/internal/entities/currency/currency_impl.dart @@ -11,11 +11,11 @@ class CurrencyIdImpl extends IdImpl implements CurrencyId { @override Future retrieve({forceRetrieve = false}) => RequestUtils.getOrRetrieveSingle( - key: this, - cacheView: api.currencyCache, - compiledRoute: CurrencyRoutes.getById.compile(params: [value]), - mapper: (json) => api.entityBuilder.buildCurrency(json), - forceRetrieve: forceRetrieve); + key: this, + cacheView: api.currencyCache, + compiledRoute: CurrencyRoutes.getById.compile(params: [value]), + mapper: (json) => api.entityBuilder.buildCurrency(json), + forceRetrieve: forceRetrieve); } class CurrencyImpl extends RestrrEntityImpl implements Currency { diff --git a/lib/src/internal/entities/session/partial_session_impl.dart b/lib/src/internal/entities/session/partial_session_impl.dart index 1f86c4d..be9d633 100644 --- a/lib/src/internal/entities/session/partial_session_impl.dart +++ b/lib/src/internal/entities/session/partial_session_impl.dart @@ -1,5 +1,4 @@ import 'package:restrr/src/internal/entities/restrr_entity_impl.dart'; -import 'package:restrr/src/internal/requests/responses/rest_response.dart'; import '../../../../restrr.dart'; import '../../utils/request_utils.dart'; @@ -12,11 +11,11 @@ class PartialSessionIdImpl extends IdImpl implements PartialSess @override Future retrieve({forceRetrieve = false}) => RequestUtils.getOrRetrieveSingle( - key: this, - cacheView: api.sessionCache, - compiledRoute: SessionRoutes.getById.compile(params: [value]), - mapper: (json) => api.entityBuilder.buildPartialSession(json), - forceRetrieve: forceRetrieve); + key: this, + cacheView: api.sessionCache, + compiledRoute: SessionRoutes.getById.compile(params: [value]), + mapper: (json) => api.entityBuilder.buildPartialSession(json), + forceRetrieve: forceRetrieve); } class PartialSessionImpl extends RestrrEntityImpl implements PartialSession { diff --git a/lib/src/internal/entities/transaction_impl.dart b/lib/src/internal/entities/transaction_impl.dart index f01b676..7116d79 100644 --- a/lib/src/internal/entities/transaction_impl.dart +++ b/lib/src/internal/entities/transaction_impl.dart @@ -12,11 +12,11 @@ class TransactionIdImpl extends IdImpl implements TransactionId { @override Future retrieve({forceRetrieve = false}) => RequestUtils.getOrRetrieveSingle( - key: this, - cacheView: api.transactionCache, - compiledRoute: TransactionRoutes.getById.compile(params: [value]), - mapper: (json) => api.entityBuilder.buildTransaction(json), - forceRetrieve: forceRetrieve); + key: this, + cacheView: api.transactionCache, + compiledRoute: TransactionRoutes.getById.compile(params: [value]), + mapper: (json) => api.entityBuilder.buildTransaction(json), + forceRetrieve: forceRetrieve); } class TransactionImpl extends RestrrEntityImpl implements Transaction { @@ -66,10 +66,10 @@ class TransactionImpl extends RestrrEntityImpl imple @override Future delete() => RequestUtils.deleteSingle( - compiledRoute: TransactionRoutes.deleteById.compile(params: [id.value]), - api: api, - key: id, - cacheView: api.transactionCache); + compiledRoute: TransactionRoutes.deleteById.compile(params: [id.value]), + api: api, + key: id, + cacheView: api.transactionCache); @override Future update( diff --git a/lib/src/internal/requests/client_errors.dart b/lib/src/internal/requests/client_errors.dart index 23f1ca6..fd4ce27 100644 --- a/lib/src/internal/requests/client_errors.dart +++ b/lib/src/internal/requests/client_errors.dart @@ -14,5 +14,6 @@ enum ClientError { const ClientError(this.message); RestrrException toException() => ClientException(this); - RestResponse toResponse({required int? statusCode}) => RestResponse(error: toException(), statusCode: statusCode); + RestResponse toResponse({required int? statusCode}) => + RestResponse(error: toException(), statusCode: statusCode); } diff --git a/lib/src/internal/requests/restrr_errors.dart b/lib/src/internal/requests/restrr_errors.dart index 3012fa7..c01d27c 100644 --- a/lib/src/internal/requests/restrr_errors.dart +++ b/lib/src/internal/requests/restrr_errors.dart @@ -49,5 +49,6 @@ enum RestrrError { } RestrrException toException() => ServerException(this); - RestResponse toResponse({required int? statusCode}) => RestResponse(error: toException(), statusCode: statusCode); + RestResponse toResponse({required int? statusCode}) => + RestResponse(error: toException(), statusCode: statusCode); } diff --git a/lib/src/internal/restrr_impl.dart b/lib/src/internal/restrr_impl.dart index cfc8104..645f36d 100644 --- a/lib/src/internal/restrr_impl.dart +++ b/lib/src/internal/restrr_impl.dart @@ -83,7 +83,8 @@ class RestrrImpl implements Restrr { @override Future deleteCurrentSession() async { - final RestResponse response = await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteCurrent.compile()); + final RestResponse response = + await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteCurrent.compile()); if (response.hasData && response.data!) { eventHandler.fire(SessionDeleteEvent(api: this)); return true; @@ -93,7 +94,8 @@ class RestrrImpl implements Restrr { @override Future deleteAllSessions() async { - final RestResponse response = await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteAll.compile()); + final RestResponse response = + await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteAll.compile()); return response.hasData && response.data!; } @@ -147,7 +149,12 @@ class RestrrImpl implements Restrr { final RestResponse response = await requestHandler.apiRequest( route: CurrencyRoutes.create.compile(), mapper: (json) => entityBuilder.buildCurrency(json), - body: {'name': name, 'symbol': symbol, 'decimal_places': decimalPlaces, if (isoCode != null) 'iso_code': isoCode}); + body: { + 'name': name, + 'symbol': symbol, + 'decimal_places': decimalPlaces, + if (isoCode != null) 'iso_code': isoCode + }); if (response.hasError) { throw response.error!; } @@ -189,17 +196,19 @@ class RestrrImpl implements Restrr { if (sourceId == null && destinationId == null) { throw ArgumentError('Either source or destination must be set!'); } - final RestResponse response = await requestHandler - .apiRequest(route: TransactionRoutes.create.compile(), mapper: (json) => entityBuilder.buildTransaction(json), body: { - 'amount': amount, - 'currency_id': currencyId, - 'executed_at': executedAt.toUtc().toIso8601String(), - 'name': name, - if (description != null) 'description': description, - if (sourceId != null) 'source_id': sourceId, - if (destinationId != null) 'destination_id': destinationId, - if (budgetId != null) 'budget_id': budgetId - }); + final RestResponse response = await requestHandler.apiRequest( + route: TransactionRoutes.create.compile(), + mapper: (json) => entityBuilder.buildTransaction(json), + body: { + 'amount': amount, + 'currency_id': currencyId, + 'executed_at': executedAt.toUtc().toIso8601String(), + 'name': name, + if (description != null) 'description': description, + if (sourceId != null) 'source_id': sourceId, + if (destinationId != null) 'destination_id': destinationId, + if (budgetId != null) 'budget_id': budgetId + }); if (response.hasError) { throw response.error!; } @@ -214,7 +223,8 @@ class RestrrImpl implements Restrr { } @override - Future> retrieveAllTransactions({int page = 1, int limit = 25, bool forceRetrieve = false}) async { + Future> retrieveAllTransactions( + {int page = 1, int limit = 25, bool forceRetrieve = false}) async { return RequestUtils.getOrRetrievePage( pageCache: transactionPageCache, compiledRoute: TransactionRoutes.getAll.compile(), diff --git a/lib/src/internal/utils/request_utils.dart b/lib/src/internal/utils/request_utils.dart index 1304024..738dea1 100644 --- a/lib/src/internal/utils/request_utils.dart +++ b/lib/src/internal/utils/request_utils.dart @@ -25,7 +25,11 @@ class RequestUtils { } static Future deleteSingle, ID extends EntityId>( - {required CompiledRoute compiledRoute, required Restrr api, required EntityId key, required EntityCacheView cacheView, bool noAuth = false}) async { + {required CompiledRoute compiledRoute, + required Restrr api, + required EntityId key, + required EntityCacheView cacheView, + bool noAuth = false}) async { final RestResponse response = await RequestHandler.noResponseRequest( route: compiledRoute, routeOptions: api.routeOptions, bearerToken: noAuth ? null : api.session.token); if (response.hasData && response.data!) {