mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
162 lines
4.2 KiB
Dart
162 lines
4.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.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() {
|
|
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 {
|
|
// Log the request path and query parameters
|
|
debugPrint('GET Request: $path');
|
|
if (queryParameters != null) {
|
|
debugPrint('Query Parameters: $queryParameters');
|
|
}
|
|
|
|
// Perform the HTTP GET request
|
|
final response = await client.get(
|
|
path,
|
|
queryParameters: queryParameters,
|
|
);
|
|
|
|
// Log the raw response data
|
|
debugPrint('Response Data: ${response.data}');
|
|
|
|
// Process the response using the expected model function
|
|
final result = expectedResponseModel(response.data);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
// Log the error details
|
|
debugPrint('Error in GET Request: $error');
|
|
rethrow; // Re-throw the error to propagate it further
|
|
}
|
|
}
|
|
|
|
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 bodyString = body is Map || body is List
|
|
? jsonEncode(body)
|
|
: body?.toString() ?? 'null';
|
|
|
|
print("POST Request: $path, Body: $bodyString, Query: $queryParameters");
|
|
final response = await client.post(
|
|
path,
|
|
data: body,
|
|
queryParameters: queryParameters,
|
|
options: options,
|
|
);
|
|
print("POST Response: ${response.data.toString()}");
|
|
return expectedResponseModel(response.data);
|
|
} catch (error) {
|
|
print("POST Error: $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;
|
|
}
|
|
}
|
|
}
|