refactor: introduce base API service and core service for standardized API interaction and error handling.
This commit is contained in:
@@ -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