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/presentation/observers/core_bloc_observer.dart';
|
||||||
export 'src/config/app_config.dart';
|
export 'src/config/app_config.dart';
|
||||||
export 'src/routing/routing.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
|
/// This class provides a wrapper around [Dio]'s methods to handle
|
||||||
/// response parsing and error handling in a consistent way.
|
/// response parsing and error handling in a consistent way.
|
||||||
class ApiService {
|
class ApiService implements BaseApiService {
|
||||||
/// Creates an [ApiService] with the given [Dio] instance.
|
/// Creates an [ApiService] with the given [Dio] instance.
|
||||||
ApiService(this._dio);
|
ApiService(this._dio);
|
||||||
|
|
||||||
@@ -13,6 +13,7 @@ class ApiService {
|
|||||||
final Dio _dio;
|
final Dio _dio;
|
||||||
|
|
||||||
/// Performs a GET request to the specified [endpoint].
|
/// Performs a GET request to the specified [endpoint].
|
||||||
|
@override
|
||||||
Future<ApiResponse> get(
|
Future<ApiResponse> get(
|
||||||
String endpoint, {
|
String endpoint, {
|
||||||
Map<String, dynamic>? params,
|
Map<String, dynamic>? params,
|
||||||
@@ -29,6 +30,7 @@ class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Performs a POST request to the specified [endpoint].
|
/// Performs a POST request to the specified [endpoint].
|
||||||
|
@override
|
||||||
Future<ApiResponse> post(
|
Future<ApiResponse> post(
|
||||||
String endpoint, {
|
String endpoint, {
|
||||||
dynamic data,
|
dynamic data,
|
||||||
@@ -47,6 +49,7 @@ class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Performs a PUT request to the specified [endpoint].
|
/// Performs a PUT request to the specified [endpoint].
|
||||||
|
@override
|
||||||
Future<ApiResponse> put(
|
Future<ApiResponse> put(
|
||||||
String endpoint, {
|
String endpoint, {
|
||||||
dynamic data,
|
dynamic data,
|
||||||
@@ -65,6 +68,7 @@ class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Performs a PATCH request to the specified [endpoint].
|
/// Performs a PATCH request to the specified [endpoint].
|
||||||
|
@override
|
||||||
Future<ApiResponse> patch(
|
Future<ApiResponse> patch(
|
||||||
String endpoint, {
|
String endpoint, {
|
||||||
dynamic data,
|
dynamic data,
|
||||||
@@ -7,7 +7,9 @@
|
|||||||
library;
|
library;
|
||||||
|
|
||||||
// Core
|
// 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
|
// Users & Membership
|
||||||
export 'src/entities/users/user.dart';
|
export 'src/entities/users/user.dart';
|
||||||
|
|||||||
@@ -18,5 +18,5 @@ class ApiResponse {
|
|||||||
final dynamic data;
|
final dynamic data;
|
||||||
|
|
||||||
/// A map of field-specific error messages, if any.
|
/// 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