mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00

Added a new method in DevicesCubit to fetch devices by room ID and updated related classes and API calls to support this functionality.
136 lines
3.7 KiB
Dart
136 lines
3.7 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
|
import 'package:syncrow_app/services/api/http_interceptor.dart';
|
|
import 'package:syncrow_app/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) {
|
|
debugPrint("******* Error");
|
|
debugPrint(error.toString());
|
|
debugPrint("******* Error End");
|
|
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}");
|
|
// debugPrint("response data is ${response.data}");
|
|
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,
|
|
);
|
|
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;
|
|
}
|
|
}
|
|
|
|
//delete
|
|
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) {
|
|
debugPrint("******* Error");
|
|
debugPrint(error.toString());
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|