Skip to content

Commit

Permalink
Move getFromApi to api_core
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenkleinle committed Jan 23, 2025
1 parent 3c82385 commit f81151e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
19 changes: 19 additions & 0 deletions lib/app/services/gruene_api_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import 'dart:async';
import 'dart:io';

import 'package:chopper/chopper.dart' as chopper;
import 'package:chopper/chopper.dart';
import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
import 'package:gruene_app/app/auth/repository/auth_repository.dart';
import 'package:gruene_app/app/constants/config.dart';
import 'package:gruene_app/i18n/translations.g.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';
import 'package:package_info_plus/package_info_plus.dart';

Expand Down Expand Up @@ -182,3 +185,19 @@ class AccessTokenAuthenticator implements chopper.Authenticator {
@override
chopper.AuthenticationCallback? get onAuthenticationSuccessful => null;
}

Future<T> getFromApi<S, T>({
required Future<Response<S>> Function(GrueneApi api) request,
required T Function(S data) map,
}) async {
final GrueneApi api = GetIt.I<GrueneApi>();

final response = await request(api);
final body = response.body;

if (!response.isSuccessful || body == null) {
throw Exception('${t.error.apiError(statusCode: response.statusCode)}\n${response.base.request?.url}');
}

return map(body);
}
25 changes: 3 additions & 22 deletions lib/features/news/domain/news_api_service.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
import 'package:chopper/chopper.dart';
import 'package:get_it/get_it.dart';
import 'package:gruene_app/app/services/gruene_api_core.dart';
import 'package:gruene_app/features/news/models/news_model.dart';
import 'package:gruene_app/i18n/translations.g.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';

Future<T> get<S, T>({
required Future<Response<S>> Function(GrueneApi api) request,
required T Function(S data) map,
}) async {
final GrueneApi api = GetIt.I<GrueneApi>();

final response = await request(api);
final body = response.body;

if (!response.isSuccessful || body == null) {
throw Exception('${t.error.apiError(statusCode: response.statusCode)}\n${response.base.request?.url}');
}

return map(body);
}

Future<NewsModel> fetchNewsById(String newsId) async => get(
Future<NewsModel> fetchNewsById(String newsId) async => getFromApi(
request: (api) => api.v1NewsNewsIdGet(newsId: newsId),
map: NewsModel.fromApi,
);

Future<List<NewsModel>> fetchNews() async => get(
Future<List<NewsModel>> fetchNews() async => getFromApi(
request: (api) => api.v1NewsGet(),
map: (data) => data.data.map(NewsModel.fromApi).toList(),
);

0 comments on commit f81151e

Please sign in to comment.