Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlessenich committed Mar 30, 2024
1 parent 5b10dda commit 874d3df
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 52 deletions.
12 changes: 6 additions & 6 deletions lib/src/api/requests/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class RequestHandler {
dynamic body,
String contentType = 'application/json'}) async {
try {
final Response<dynamic> response =
await route.submit(routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType);
final Response<dynamic> 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);
Expand Down Expand Up @@ -61,8 +61,8 @@ class RequestHandler {
Map<int, RestrrError> errorMap = const {},
String contentType = 'application/json'}) async {
try {
final Response<dynamic> response =
await route.submit(routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType);
final Response<dynamic> 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);
Expand Down Expand Up @@ -97,8 +97,8 @@ class RequestHandler {
dynamic body,
String contentType = 'application/json'}) async {
try {
final Response<dynamic> response =
await route.submit(routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType);
final Response<dynamic> response = await route.submit(
routeOptions: routeOptions, body: body, bearerToken: bearerToken, contentType: contentType);
if (response.data is! List<dynamic>) {
throw StateError('Received response is not a list!');
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/api/restrr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ abstract class Restrr {

/* Currencies */

Future<Currency> createCurrency({required String name, required String symbol, required int decimalPlaces, String? isoCode});
Future<Currency> createCurrency(
{required String name, required String symbol, required int decimalPlaces, String? isoCode});

List<Currency> getCurrencies();

Expand Down
14 changes: 9 additions & 5 deletions lib/src/api/restrr_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ class RestrrBuilder {
});
}

Future<Restrr> _handleAuthProcess({required Future<RestResponse<PartialSession>> Function(RestrrImpl) authFunction}) async {
Future<Restrr> _handleAuthProcess(
{required Future<RestResponse<PartialSession>> 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<PartialSession> response = await authFunction(apiImpl);
if (response.hasError) {
Expand All @@ -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<Account> accounts = await RequestUtils.fetchAllPaginated<Account, AccountId>(apiImpl, await apiImpl.retrieveAllAccounts(limit: 50));
final List<Account> accounts =
await RequestUtils.fetchAllPaginated<Account, AccountId>(apiImpl, await apiImpl.retrieveAllAccounts(limit: 50));
Restrr.log.info('Cached ${accounts.length} account(s)');

final List<Currency> currencies =
await RequestUtils.fetchAllPaginated<Currency, CurrencyId>(apiImpl, await apiImpl.retrieveAllCurrencies(limit: 50));
final List<Currency> currencies = await RequestUtils.fetchAllPaginated<Currency, CurrencyId>(
apiImpl, await apiImpl.retrieveAllCurrencies(limit: 50));
Restrr.log.info('Cached ${currencies.length} currencies');

apiImpl.eventHandler.fire(ReadyEvent(api: apiImpl));
Expand Down
3 changes: 2 additions & 1 deletion lib/src/internal/cache/cache_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class EntityCacheView<E extends RestrrEntity<E, ID>, ID extends EntityId<E>> ext
EntityCacheView(RestrrImpl api) : super(api, valueFunction: (entity) => entity.id.value);
}

class PageCacheView<E extends RestrrEntity<E, ID>, ID extends EntityId<E>> extends MapCacheView<(int, int), Paginated<E>> {
class PageCacheView<E extends RestrrEntity<E, ID>, ID extends EntityId<E>>
extends MapCacheView<(int, int), Paginated<E>> {
PageCacheView(RestrrImpl api) : super(api, valueFunction: (page) => (page.pageNumber, page.limit));
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/internal/entities/account_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class AccountImpl extends RestrrEntityImpl<Account, AccountId> implements Accoun
cacheView: api.transactionCache);

@override
Future<Account> update({String? name, String? description, String? iban, int? originalBalance, Id? currencyId}) async {
Future<Account> 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');
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/internal/entities/currency/currency_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class CurrencyIdImpl extends IdImpl<Currency> implements CurrencyId {

@override
Future<Currency> 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<Currency, CurrencyId> implements Currency {
Expand Down
11 changes: 5 additions & 6 deletions lib/src/internal/entities/session/partial_session_impl.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,11 +11,11 @@ class PartialSessionIdImpl extends IdImpl<PartialSession> implements PartialSess

@override
Future<PartialSession> 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<PartialSession, PartialSessionId> implements PartialSession {
Expand Down
18 changes: 9 additions & 9 deletions lib/src/internal/entities/transaction_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class TransactionIdImpl extends IdImpl<Transaction> implements TransactionId {

@override
Future<Transaction> 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<Transaction, TransactionId> implements Transaction {
Expand Down Expand Up @@ -66,10 +66,10 @@ class TransactionImpl extends RestrrEntityImpl<Transaction, TransactionId> imple

@override
Future<bool> 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<Transaction> update(
Expand Down
3 changes: 2 additions & 1 deletion lib/src/internal/requests/client_errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ enum ClientError {
const ClientError(this.message);

RestrrException toException() => ClientException(this);
RestResponse<T> toResponse<T>({required int? statusCode}) => RestResponse<T>(error: toException(), statusCode: statusCode);
RestResponse<T> toResponse<T>({required int? statusCode}) =>
RestResponse<T>(error: toException(), statusCode: statusCode);
}
3 changes: 2 additions & 1 deletion lib/src/internal/requests/restrr_errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ enum RestrrError {
}

RestrrException toException() => ServerException(this);
RestResponse<T> toResponse<T>({required int? statusCode}) => RestResponse<T>(error: toException(), statusCode: statusCode);
RestResponse<T> toResponse<T>({required int? statusCode}) =>
RestResponse<T>(error: toException(), statusCode: statusCode);
}
40 changes: 25 additions & 15 deletions lib/src/internal/restrr_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class RestrrImpl implements Restrr {

@override
Future<bool> deleteCurrentSession() async {
final RestResponse<bool> response = await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteCurrent.compile());
final RestResponse<bool> response =
await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteCurrent.compile());
if (response.hasData && response.data!) {
eventHandler.fire(SessionDeleteEvent(api: this));
return true;
Expand All @@ -93,7 +94,8 @@ class RestrrImpl implements Restrr {

@override
Future<bool> deleteAllSessions() async {
final RestResponse<bool> response = await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteAll.compile());
final RestResponse<bool> response =
await requestHandler.noResponseApiRequest(route: SessionRoutes.deleteAll.compile());
return response.hasData && response.data!;
}

Expand Down Expand Up @@ -147,7 +149,12 @@ class RestrrImpl implements Restrr {
final RestResponse<Currency> 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!;
}
Expand Down Expand Up @@ -189,17 +196,19 @@ class RestrrImpl implements Restrr {
if (sourceId == null && destinationId == null) {
throw ArgumentError('Either source or destination must be set!');
}
final RestResponse<Transaction> 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<Transaction> 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!;
}
Expand All @@ -214,7 +223,8 @@ class RestrrImpl implements Restrr {
}

@override
Future<Paginated<Transaction>> retrieveAllTransactions({int page = 1, int limit = 25, bool forceRetrieve = false}) async {
Future<Paginated<Transaction>> retrieveAllTransactions(
{int page = 1, int limit = 25, bool forceRetrieve = false}) async {
return RequestUtils.getOrRetrievePage(
pageCache: transactionPageCache,
compiledRoute: TransactionRoutes.getAll.compile(),
Expand Down
6 changes: 5 additions & 1 deletion lib/src/internal/utils/request_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class RequestUtils {
}

static Future<bool> deleteSingle<E extends RestrrEntity<E, ID>, ID extends EntityId<E>>(
{required CompiledRoute compiledRoute, required Restrr api, required EntityId key, required EntityCacheView<E, ID> cacheView, bool noAuth = false}) async {
{required CompiledRoute compiledRoute,
required Restrr api,
required EntityId key,
required EntityCacheView<E, ID> cacheView,
bool noAuth = false}) async {
final RestResponse<bool> response = await RequestHandler.noResponseRequest(
route: compiledRoute, routeOptions: api.routeOptions, bearerToken: noAuth ? null : api.session.token);
if (response.hasData && response.data!) {
Expand Down

0 comments on commit 874d3df

Please sign in to comment.