refactor: introduce base API service and core service for standardized API interaction and error handling.
This commit is contained in:
@@ -8,4 +8,4 @@ export 'src/presentation/mixins/bloc_error_handler.dart';
|
||||
export 'src/presentation/observers/core_bloc_observer.dart';
|
||||
export 'src/config/app_config.dart';
|
||||
export 'src/routing/routing.dart';
|
||||
export 'src/services/api_service.dart';
|
||||
export 'src/services/api_service/api_service.dart';
|
||||
|
||||
@@ -5,7 +5,7 @@ import 'package:krow_domain/krow_domain.dart';
|
||||
///
|
||||
/// This class provides a wrapper around [Dio]'s methods to handle
|
||||
/// response parsing and error handling in a consistent way.
|
||||
class ApiService {
|
||||
class ApiService implements BaseApiService {
|
||||
/// Creates an [ApiService] with the given [Dio] instance.
|
||||
ApiService(this._dio);
|
||||
|
||||
@@ -13,6 +13,7 @@ class ApiService {
|
||||
final Dio _dio;
|
||||
|
||||
/// Performs a GET request to the specified [endpoint].
|
||||
@override
|
||||
Future<ApiResponse> get(
|
||||
String endpoint, {
|
||||
Map<String, dynamic>? params,
|
||||
@@ -29,6 +30,7 @@ class ApiService {
|
||||
}
|
||||
|
||||
/// Performs a POST request to the specified [endpoint].
|
||||
@override
|
||||
Future<ApiResponse> post(
|
||||
String endpoint, {
|
||||
dynamic data,
|
||||
@@ -47,6 +49,7 @@ class ApiService {
|
||||
}
|
||||
|
||||
/// Performs a PUT request to the specified [endpoint].
|
||||
@override
|
||||
Future<ApiResponse> put(
|
||||
String endpoint, {
|
||||
dynamic data,
|
||||
@@ -65,6 +68,7 @@ class ApiService {
|
||||
}
|
||||
|
||||
/// Performs a PATCH request to the specified [endpoint].
|
||||
@override
|
||||
Future<ApiResponse> patch(
|
||||
String endpoint, {
|
||||
dynamic data,
|
||||
@@ -7,7 +7,9 @@
|
||||
library;
|
||||
|
||||
// Core
|
||||
export 'src/entities/core/services/api_service/api_response.dart';
|
||||
export 'src/core/services/api_services/api_response.dart';
|
||||
export 'src/core/services/api_services/base_api_service.dart';
|
||||
export 'src/core/services/api_services/base_core_service.dart';
|
||||
|
||||
// Users & Membership
|
||||
export 'src/entities/users/user.dart';
|
||||
|
||||
@@ -18,5 +18,5 @@ class ApiResponse {
|
||||
final dynamic data;
|
||||
|
||||
/// A map of field-specific error messages, if any.
|
||||
final Map<String, dynamic?> errors;
|
||||
final Map<String, dynamic> errors;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'api_response.dart';
|
||||
|
||||
/// Abstract base class for API services.
|
||||
///
|
||||
/// This defines the contract for making HTTP requests.
|
||||
abstract class BaseApiService {
|
||||
/// Performs a GET request to the specified [endpoint].
|
||||
Future<ApiResponse> get(String endpoint, {Map<String, dynamic>? params});
|
||||
|
||||
/// Performs a POST request to the specified [endpoint].
|
||||
Future<ApiResponse> post(
|
||||
String endpoint, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? params,
|
||||
});
|
||||
|
||||
/// Performs a PUT request to the specified [endpoint].
|
||||
Future<ApiResponse> put(
|
||||
String endpoint, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? params,
|
||||
});
|
||||
|
||||
/// Performs a PATCH request to the specified [endpoint].
|
||||
Future<ApiResponse> patch(
|
||||
String endpoint, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? params,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'api_response.dart';
|
||||
import 'base_api_service.dart';
|
||||
|
||||
/// Abstract base class for core business services.
|
||||
///
|
||||
/// This provides a common [action] wrapper for standardized execution
|
||||
/// and error catching across all core service implementations.
|
||||
abstract class BaseCoreService {
|
||||
/// Creates a [BaseCoreService] with the given [api] client.
|
||||
const BaseCoreService(this.api);
|
||||
|
||||
/// The API client used to perform requests.
|
||||
final BaseApiService api;
|
||||
|
||||
/// Standardized wrapper to execute API actions.
|
||||
///
|
||||
/// This handles generic error normalization for unexpected non-HTTP errors.
|
||||
Future<ApiResponse> action(Future<ApiResponse> Function() execution) async {
|
||||
try {
|
||||
return await execution();
|
||||
} catch (e) {
|
||||
return ApiResponse(
|
||||
code: 'CORE_INTERNAL_ERROR',
|
||||
message: e.toString(),
|
||||
errors: <String, dynamic>{'exception': e.runtimeType.toString()},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user