mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Break the 372-line frontend API client into focused files under lib/utils/api/: error mapping (api_error.dart), Dio setup behind a buildDio factory (dio_config.dart), and device/scanner/notification endpoints as extensions in part files. Collapse the five near-identical scanner-status methods via a generic _getModel helper and de-duplicate the pagination logic via a shared _paginate helper. The BackendAPI singleton and dioErrorToUserMessage remain importable from oott_api.dart unchanged, so no call sites are affected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
109 lines
2.8 KiB
Dart
109 lines
2.8 KiB
Dart
part of '../oott_api.dart';
|
|
|
|
/// Device endpoints: lookup, listing, registration, updates and event history.
|
|
extension DeviceApi on BackendAPI {
|
|
Future<Device> getDevice(String macAddress) =>
|
|
_getModel('/devices/$macAddress', Device.fromJson);
|
|
|
|
Future<({List<Device> items, bool hasNextPage})> listDevices({
|
|
bool? isRegistered,
|
|
String? owner,
|
|
DeviceType? deviceType,
|
|
String? sortBy,
|
|
bool? sortAscending,
|
|
int page = 0,
|
|
int perPage = 10,
|
|
CancelToken? cancelToken,
|
|
}) async {
|
|
final params = <String, dynamic>{};
|
|
if (isRegistered != null) params['is_registered'] = isRegistered;
|
|
if (owner != null && owner.isNotEmpty) params['owner'] = owner;
|
|
if (deviceType != null) {
|
|
params['device_type'] = deviceType == DeviceType.unknown
|
|
? ''
|
|
: deviceType.apiName;
|
|
}
|
|
if (sortBy != null) params['sort_by'] = sortBy;
|
|
if (sortAscending != null) {
|
|
params['sort_order'] = sortAscending ? 'asc' : 'desc';
|
|
}
|
|
params['page_offset'] = page * perPage;
|
|
params['page_limit'] = perPage + 1;
|
|
|
|
final response = await _dio.get(
|
|
'/devices',
|
|
queryParameters: params,
|
|
cancelToken: cancelToken,
|
|
);
|
|
|
|
return _paginate(
|
|
response.data as List,
|
|
(item) => Device.fromJson(item as Map<String, dynamic>),
|
|
perPage,
|
|
);
|
|
}
|
|
|
|
Future<void> registerDevice(
|
|
String macAddress,
|
|
String owner,
|
|
String deviceType, {
|
|
String? name,
|
|
}) async {
|
|
await _dio.put(
|
|
'/devices',
|
|
data: {
|
|
'mac_address': macAddress,
|
|
'owner': owner,
|
|
'device_type': deviceType,
|
|
'name': ?name,
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> updateDevice(
|
|
String macAddress,
|
|
String owner,
|
|
String deviceType,
|
|
String vendor, {
|
|
String? name,
|
|
}) async {
|
|
await _dio.put(
|
|
'/devices/$macAddress',
|
|
data: {
|
|
'owner': owner,
|
|
'device_type': deviceType,
|
|
'vendor': vendor,
|
|
'name': name,
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> forgetDevice(String macAddress) async {
|
|
await _dio.delete('/devices/$macAddress');
|
|
}
|
|
|
|
Future<List<DeviceEvent>> getDeviceEvents(
|
|
String macAddress, {
|
|
DateTime? createdFrom,
|
|
}) async {
|
|
final queryParams = <String, dynamic>{};
|
|
if (createdFrom != null) {
|
|
queryParams['created_from'] = createdFrom.toUtc().toIso8601String();
|
|
}
|
|
final response = await _dio.get(
|
|
'/devices/$macAddress/events',
|
|
queryParameters: queryParams.isEmpty ? null : queryParams,
|
|
);
|
|
return (response.data as List)
|
|
.map((item) => DeviceEvent.fromJson(item as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
|
|
Future<DeviceSummary> getDeviceSummary({CancelToken? cancelToken}) =>
|
|
_getModel(
|
|
'/devices/summary',
|
|
DeviceSummary.fromJson,
|
|
cancelToken: cancelToken,
|
|
);
|
|
}
|