mirror of
https://github.com/SyncrowIOT/web.git
synced 2026-10-25 06:57:55 +00:00
Implemented devices bloc, service, param and model.
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/models/device_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/params/load_devices_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/services/device_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
|
||||
class RemoteDeviceService implements DeviceService {
|
||||
final HTTPService _httpService;
|
||||
|
||||
RemoteDeviceService({
|
||||
required HTTPService httpService,
|
||||
}) : _httpService = httpService;
|
||||
|
||||
static const _defaultErrorMessage = 'Failed to load devices';
|
||||
|
||||
@override
|
||||
Future<List<DeviceModel>> getDevices(LoadDevicesParam param) async {
|
||||
try {
|
||||
final response = await _httpService.get(
|
||||
path: 'devices',
|
||||
queryParameters: {
|
||||
'spaceUuid': param.spaceUuid,
|
||||
if (param.type != null) 'type': param.type,
|
||||
if (param.status != null) 'status': param.status,
|
||||
},
|
||||
expectedResponseModel: (data) {
|
||||
return (data as List)
|
||||
.map((e) => DeviceModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} on DioException catch (e) {
|
||||
final message = e.response?.data as Map<String, dynamic>?;
|
||||
final error = message?['error'] as Map<String, dynamic>?;
|
||||
final errorMessage = error?['error'] as String? ?? '';
|
||||
final formattedErrorMessage = [_defaultErrorMessage, errorMessage].join(
|
||||
': ',
|
||||
);
|
||||
throw APIException(formattedErrorMessage);
|
||||
} catch (e) {
|
||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||
throw APIException(formattedErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user