Files
syncrow-web/lib/services/api/http_service.dart
2025-06-12 15:33:32 +03:00

147 lines
3.7 KiB
Dart

import 'package:dio/dio.dart';
import 'package:syncrow_web/services/api/http_interceptor.dart';
import 'package:syncrow_web/services/locator.dart';
import 'package:syncrow_web/utils/constants/api_const.dart';
class HTTPService {
final Dio client = serviceLocator.get<Dio>();
// final navigatorKey = GlobalKey<NavigatorState>();
String certificateString = '';
static Dio setupDioClient() {
final client = Dio(
BaseOptions(
baseUrl: ApiEndpoints.baseUrl,
receiveDataWhenStatusError: true,
followRedirects: false,
connectTimeout: const Duration(seconds: 30),
receiveTimeout: const Duration(seconds: 30),
),
);
client.interceptors.add(serviceLocator.get<HTTPInterceptor>());
// Add this interceptor for logging requests and responses
// client.interceptors.add(
// LogInterceptor(
// request: true,
// requestHeader: true,
// requestBody: true,
// responseHeader: false,
// responseBody: true,
// error: true,
// logPrint: (object) => print(object),
// ),
// );
return client;
}
Future<T> get<T>({
required String path,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel,
bool showServerMessage = true,
}) async {
try {
final response = await client.get(
path,
queryParameters: queryParameters,
);
return expectedResponseModel(response.data);
} catch (error) {
rethrow;
}
}
Future<T> post<T>(
{required String path,
Map<String, dynamic>? queryParameters,
Options? options,
dynamic body,
bool showServerMessage = true,
required T Function(dynamic) expectedResponseModel}) async {
try {
final response = await client.post(
path,
data: body,
queryParameters: queryParameters,
options: options,
);
return expectedResponseModel(response.data);
} catch (error) {
rethrow;
}
}
Future<T> patch<T>(
{required String path,
Map<String, dynamic>? queryParameters,
dynamic body,
required T Function(dynamic) expectedResponseModel}) async {
try {
final response = await client.patch(
path,
data: body,
queryParameters: queryParameters,
);
return expectedResponseModel(response.data);
} catch (error) {
rethrow;
}
}
Future<T> put<T>(
{required String path,
Map<String, dynamic>? queryParameters,
dynamic body,
required T Function(dynamic) expectedResponseModel}) async {
try {
final response = await client.put(
path,
data: body,
queryParameters: queryParameters,
);
return expectedResponseModel(response.data);
} catch (error) {
rethrow;
}
}
Future<T> download<T>(
{required String path,
required String savePath,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel}) async {
try {
final response = await client.download(
path,
savePath,
onReceiveProgress: (current, total) {},
);
return expectedResponseModel(response.data);
// return expectedResponseModel(response.data);
} catch (error) {
rethrow;
}
}
Future<T> delete<T>({
required String path,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel,
bool showServerMessage = true,
}) async {
try {
final response = await client.delete(
path,
queryParameters: queryParameters,
);
return expectedResponseModel(response.data);
} catch (error) {
rethrow;
}
}
}