mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 09:45:22 +00:00
112 lines
3.1 KiB
Dart
112 lines
3.1 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:syncrow_app/services/locator.dart';
|
|
|
|
import 'http_interceptor.dart';
|
|
|
|
class HTTPService {
|
|
final Dio client = locator<Dio>();
|
|
final navigatorKey = GlobalKey<NavigatorState>();
|
|
|
|
String certificateString = "";
|
|
|
|
static Dio setupDioClient() {
|
|
final client = Dio(
|
|
BaseOptions(
|
|
// TODO add base url
|
|
// baseUrl: URLConstants.baseURL,
|
|
receiveDataWhenStatusError: true,
|
|
followRedirects: false,
|
|
connectTimeout: const Duration(milliseconds: 60000),
|
|
receiveTimeout: const Duration(milliseconds: 60000),
|
|
),
|
|
);
|
|
|
|
client.interceptors.add(locator<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) {
|
|
debugPrint("******* Error");
|
|
debugPrint(error.toString());
|
|
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);
|
|
debugPrint("status code is ${response.statusCode}");
|
|
return expectedResponseModel(response.data);
|
|
} catch (error) {
|
|
debugPrint("******* Error");
|
|
debugPrint(error.toString());
|
|
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,
|
|
);
|
|
debugPrint("status code is ${response.statusCode}");
|
|
return expectedResponseModel(response.data);
|
|
} catch (error) {
|
|
debugPrint("******* Error");
|
|
debugPrint(error.toString());
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<T> download<T>(
|
|
{required String path,
|
|
required String savePath,
|
|
Map<String, dynamic>? queryParameters,
|
|
required T Function(dynamic) expectedResponseModel}) async {
|
|
try {
|
|
debugPrint("download begins");
|
|
final response = await client.download(
|
|
path,
|
|
savePath,
|
|
onReceiveProgress: (current, total) {
|
|
debugPrint("current = $current, while total = $total");
|
|
},
|
|
);
|
|
debugPrint("download ends");
|
|
return expectedResponseModel(response.data);
|
|
// return expectedResponseModel(response.data);
|
|
} catch (error) {
|
|
debugPrint("******* Error");
|
|
debugPrint("download error");
|
|
debugPrint(error.toString());
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|