mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
135 lines
3.4 KiB
Dart
135 lines
3.4 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:syncrow_web/services/api/api_links_endpoints.dart';
|
|
import 'package:syncrow_web/services/api/http_interceptor.dart';
|
|
import 'package:syncrow_web/services/locator.dart';
|
|
|
|
class HTTPService {
|
|
final Dio client = serviceLocator.get<Dio>();
|
|
|
|
// final navigatorKey = GlobalKey<NavigatorState>();
|
|
|
|
String certificateString = "";
|
|
|
|
static Dio setupDioClient() {
|
|
Dio 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>());
|
|
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;
|
|
}
|
|
}
|
|
}
|