runable
This commit is contained in:
23
lib/core/network/api_interceptor.dart
Normal file
23
lib/core/network/api_interceptor.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
/// API interceptor for logging and error handling
|
||||
class ApiInterceptor extends Interceptor {
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
print('REQUEST[${options.method}] => PATH: ${options.path}');
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||
print('RESPONSE[${response.statusCode}] => PATH: ${response.requestOptions.path}');
|
||||
super.onResponse(response, handler);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
print('ERROR[${err.response?.statusCode}] => PATH: ${err.requestOptions.path}');
|
||||
print('ERROR MSG: ${err.message}');
|
||||
super.onError(err, handler);
|
||||
}
|
||||
}
|
||||
85
lib/core/network/dio_client.dart
Normal file
85
lib/core/network/dio_client.dart
Normal file
@@ -0,0 +1,85 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../constants/api_constants.dart';
|
||||
import 'api_interceptor.dart';
|
||||
|
||||
/// Dio HTTP client configuration
|
||||
class DioClient {
|
||||
late final Dio _dio;
|
||||
|
||||
DioClient() {
|
||||
_dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: ApiConstants.fullBaseUrl,
|
||||
connectTimeout: Duration(milliseconds: ApiConstants.connectTimeout),
|
||||
receiveTimeout: Duration(milliseconds: ApiConstants.receiveTimeout),
|
||||
sendTimeout: Duration(milliseconds: ApiConstants.sendTimeout),
|
||||
headers: {
|
||||
ApiConstants.contentType: ApiConstants.applicationJson,
|
||||
ApiConstants.accept: ApiConstants.applicationJson,
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
_dio.interceptors.add(ApiInterceptor());
|
||||
}
|
||||
|
||||
Dio get dio => _dio;
|
||||
|
||||
/// GET request
|
||||
Future<Response> get(
|
||||
String path, {
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
}) async {
|
||||
return await _dio.get(
|
||||
path,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
}
|
||||
|
||||
/// POST request
|
||||
Future<Response> post(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
}) async {
|
||||
return await _dio.post(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
}
|
||||
|
||||
/// PUT request
|
||||
Future<Response> put(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
}) async {
|
||||
return await _dio.put(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
}
|
||||
|
||||
/// DELETE request
|
||||
Future<Response> delete(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
}) async {
|
||||
return await _dio.delete(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
}
|
||||
}
|
||||
21
lib/core/network/network_info.dart
Normal file
21
lib/core/network/network_info.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
|
||||
/// Network connectivity checker
|
||||
class NetworkInfo {
|
||||
final Connectivity connectivity;
|
||||
|
||||
NetworkInfo(this.connectivity);
|
||||
|
||||
/// Check if device has internet connection
|
||||
Future<bool> get isConnected async {
|
||||
final result = await connectivity.checkConnectivity();
|
||||
return result.contains(ConnectivityResult.mobile) ||
|
||||
result.contains(ConnectivityResult.wifi) ||
|
||||
result.contains(ConnectivityResult.ethernet);
|
||||
}
|
||||
|
||||
/// Stream of connectivity changes
|
||||
Stream<List<ConnectivityResult>> get connectivityStream {
|
||||
return connectivity.onConnectivityChanged;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user