2026-06-02 10:24:37 -04:00
|
|
|
library;
|
2026-02-27 12:28:16 -05:00
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2026-03-02 13:59:50 -05:00
|
|
|
import 'package:encrypter/encrypter/xor.dart';
|
2026-06-03 19:41:52 -04:00
|
|
|
import 'package:flutter/foundation.dart';
|
2026-06-02 10:24:37 -04:00
|
|
|
|
|
|
|
|
import 'api/api_error.dart';
|
|
|
|
|
import 'api/dio_config.dart';
|
|
|
|
|
import 'backend_reachability.dart';
|
|
|
|
|
import 'pref_utils.dart';
|
2026-06-05 21:57:58 -04:00
|
|
|
import '../model/active_scanner_status.dart';
|
2026-06-08 17:26:53 -04:00
|
|
|
import '../model/app_config.dart';
|
2026-06-05 21:57:58 -04:00
|
|
|
import '../model/passive_scanner_status.dart';
|
2026-05-27 14:58:11 -04:00
|
|
|
import '../model/device.dart';
|
2026-05-28 10:05:49 -04:00
|
|
|
import '../model/device_event.dart';
|
2026-05-28 19:10:08 -04:00
|
|
|
import '../model/device_summary.dart';
|
2026-05-27 19:46:19 -04:00
|
|
|
import '../model/device_type.dart';
|
2026-03-02 13:59:50 -05:00
|
|
|
import '../model/notification.dart';
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-06-02 10:24:37 -04:00
|
|
|
export 'api/api_error.dart';
|
2026-05-31 09:06:56 -04:00
|
|
|
|
2026-06-08 17:26:53 -04:00
|
|
|
part 'api/oott_api_config.dart';
|
2026-06-02 10:24:37 -04:00
|
|
|
part 'api/oott_api_devices.dart';
|
|
|
|
|
part 'api/oott_api_scanners.dart';
|
|
|
|
|
part 'api/oott_api_notifications.dart';
|
2026-06-08 12:28:03 -04:00
|
|
|
part 'api/oott_api_push.dart';
|
2026-05-31 09:06:56 -04:00
|
|
|
|
2026-02-27 12:28:16 -05:00
|
|
|
class BackendAPI {
|
2026-03-02 13:59:50 -05:00
|
|
|
static final BackendAPI _instance = BackendAPI._internal();
|
|
|
|
|
static BackendAPI get instance => _instance;
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
BackendAPI._internal() {
|
2026-05-31 09:17:15 -04:00
|
|
|
reconfigureFromPrefs();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 10:24:37 -04:00
|
|
|
late String _baseUrl;
|
|
|
|
|
late String _apiKey;
|
|
|
|
|
late Dio _dio;
|
|
|
|
|
|
2026-05-31 09:17:15 -04:00
|
|
|
void reconfigureFromPrefs() {
|
2026-03-03 12:20:57 -05:00
|
|
|
_baseUrl =
|
|
|
|
|
PrefUtil.getValue("base_url", "http://localhost:3000/api") as String;
|
|
|
|
|
_apiKey = XOR().xorDecode(PrefUtil.getValue("api_key", "") as String);
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-06-02 10:24:37 -04:00
|
|
|
_dio = buildDio(baseUrl: _baseUrl, apiKey: _apiKey);
|
2026-05-31 18:21:55 -04:00
|
|
|
BackendReachability.instance.setProber(() => _dio.get('/test'));
|
2026-03-02 13:59:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-03 19:41:52 -04:00
|
|
|
/// Test-only access to the underlying [Dio] so tests can swap in a client
|
|
|
|
|
/// with a mock adapter installed. Mirrors [reconfigureFromPrefs]'s prober
|
|
|
|
|
/// wiring so reachability stays consistent after a swap.
|
|
|
|
|
@visibleForTesting
|
|
|
|
|
Dio get dioForTesting => _dio;
|
|
|
|
|
|
|
|
|
|
@visibleForTesting
|
|
|
|
|
set dioForTesting(Dio dio) {
|
|
|
|
|
_dio = dio;
|
|
|
|
|
BackendReachability.instance.setProber(() => _dio.get('/test'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 12:20:57 -05:00
|
|
|
// Returns null if the test was successful, and a String with a message about the issue if not
|
|
|
|
|
static Future<String?> test(String baseUrl, String apiKey) async {
|
2026-06-02 10:24:37 -04:00
|
|
|
final dio = buildDio(
|
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
|
apiKey: apiKey,
|
|
|
|
|
withRetry: false,
|
|
|
|
|
withReachability: false,
|
2026-03-03 12:20:57 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-06-02 10:24:37 -04:00
|
|
|
final response = await dio.get('/test');
|
2026-05-31 18:32:40 -04:00
|
|
|
return response.data == 'OOTT_API_OK'
|
2026-03-03 12:20:57 -05:00
|
|
|
? null
|
|
|
|
|
: "URL successfully called but didn't return the expected value. Check your URL and make sure it points to your OOTT backend base URL.";
|
2026-05-31 09:06:56 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
return dioErrorToUserMessage(e);
|
2026-03-03 12:20:57 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 10:24:37 -04:00
|
|
|
/// Fetches [path] and decodes the JSON object response with [fromJson].
|
|
|
|
|
Future<T> _getModel<T>(
|
|
|
|
|
String path,
|
|
|
|
|
T Function(Map<String, dynamic>) fromJson, {
|
2026-05-31 09:30:03 -04:00
|
|
|
CancelToken? cancelToken,
|
2026-05-27 19:46:19 -04:00
|
|
|
}) async {
|
2026-06-02 10:24:37 -04:00
|
|
|
final response = await _dio.get(path, cancelToken: cancelToken);
|
|
|
|
|
return fromJson(response.data as Map<String, dynamic>);
|
2026-05-27 14:58:11 -04:00
|
|
|
}
|
|
|
|
|
|
2026-06-05 21:09:25 -04:00
|
|
|
/// Decodes a paged list response of the shape `{items: [...], total_count: N}`
|
|
|
|
|
/// into the page's items and the total number of rows matching the request's
|
|
|
|
|
/// filters. The total lets callers report how many pages exist and offer a
|
|
|
|
|
/// "go to last page" control.
|
|
|
|
|
({List<T> items, int totalCount}) _paginate<T>(
|
|
|
|
|
Map<String, dynamic> data,
|
2026-06-02 10:24:37 -04:00
|
|
|
T Function(dynamic) fromItem,
|
|
|
|
|
) {
|
2026-06-05 21:09:25 -04:00
|
|
|
final items = (data['items'] as List<dynamic>).map(fromItem).toList();
|
|
|
|
|
return (items: items, totalCount: data['total_count'] as int);
|
2026-02-27 12:28:16 -05:00
|
|
|
}
|
|
|
|
|
}
|