58 lines
1.7 KiB
Dart
58 lines
1.7 KiB
Dart
import 'package:ac_web/Api/ApiClient.dart';
|
|
import 'package:ac_web/Api/ApiConfig.dart';
|
|
import 'package:ac_web/Api/Services/AgentsService.dart';
|
|
import 'package:ac_web/Api/Services/ReportsService.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
/// Single entry point for the whole Rest API of the Artificial Company backend.
|
|
///
|
|
/// Usage:
|
|
/// ```dart
|
|
/// // once during startup, e.g. in main.dart
|
|
/// AcApi.configure(const ApiConfig(host: '192.168.0.10', port: 5000));
|
|
///
|
|
/// // anywhere in the app
|
|
/// final agents = await AcApi.instance.agents.getAgents();
|
|
/// ```
|
|
class AcApi {
|
|
AcApi({ApiConfig config = ApiConfig.development, http.Client? httpClient})
|
|
: mClient = ApiClient(config: config, httpClient: httpClient);
|
|
|
|
final ApiClient mClient;
|
|
|
|
static AcApi? _instance;
|
|
|
|
/// Globally shared instance. Created lazily with [ApiConfig.development] if
|
|
/// [configure] was not called before.
|
|
static AcApi get instance => _instance ??= AcApi();
|
|
|
|
/// Replaces the shared instance, e.g. during app startup or in tests.
|
|
static AcApi configure(ApiConfig config, {http.Client? httpClient}) {
|
|
_instance?.dispose();
|
|
return _instance = AcApi(config: config, httpClient: httpClient);
|
|
}
|
|
|
|
/// Drops the shared instance. Mainly useful for tests.
|
|
static void reset() {
|
|
_instance?.dispose();
|
|
_instance = null;
|
|
}
|
|
|
|
ApiClient get client => mClient;
|
|
|
|
ApiConfig get config => mClient.config;
|
|
|
|
AgentsService? _agents;
|
|
ReportsService? _reports;
|
|
|
|
/// `/agents` controller.
|
|
AgentsService get agents => _agents ??= AgentsService(mClient);
|
|
|
|
/// `/reports` controller.
|
|
ReportsService get reports => _reports ??= ReportsService(mClient);
|
|
|
|
void dispose() {
|
|
mClient.dispose();
|
|
}
|
|
}
|