mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00
initialized Application theme
This commit is contained in:
121
lib/services/api/http_service.dart
Normal file
121
lib/services/api/http_service.dart
Normal file
@ -0,0 +1,121 @@
|
||||
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.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
|
||||
// client. = (X509Certificate cert, String host, int port) {
|
||||
// // TODO add SSL certificate
|
||||
// // if(cert.pem == certificateString){ // Verify the certificate
|
||||
// // return true;
|
||||
// // }
|
||||
// // return false;
|
||||
// return true;
|
||||
// };
|
||||
// return client;
|
||||
// };
|
||||
client.interceptors.add(locator<HTTPInterceptor>());
|
||||
return client;
|
||||
}
|
||||
|
||||
Future<T> getRequest<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> postRequest<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> patchRequest<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> downloadRequest<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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user