Setting up basic API Structure
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import 'package:ac_web/Api/Dtos/AgentDto.dart';
|
||||
import 'package:ac_web/Api/Services/ApiServiceBase.dart';
|
||||
|
||||
/// Access to the backend `AgentsController` (`/agents`).
|
||||
class AgentsService extends ApiServiceBase {
|
||||
const AgentsService(super.client);
|
||||
|
||||
@override
|
||||
String get controllerRoute => 'agents';
|
||||
|
||||
/// `GET /agents` - all known agents including their current tasks.
|
||||
Future<List<AgentDto>> getAgents() {
|
||||
return client.getList(route(), AgentDto.fromJson);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'package:ac_web/Api/ApiClient.dart';
|
||||
|
||||
/// Base class for every service that maps onto one backend controller.
|
||||
///
|
||||
/// Adding support for a new controller means:
|
||||
/// 1. create the dtos under `Api/Dtos`
|
||||
/// 2. create a `<Name>Service extends ApiServiceBase` with `controllerRoute`
|
||||
/// 3. expose it as a lazy getter on [AcApi]
|
||||
abstract class ApiServiceBase {
|
||||
const ApiServiceBase(this.client);
|
||||
|
||||
final ApiClient client;
|
||||
|
||||
/// Route of the controller without leading slash, e.g. `agents`.
|
||||
String get controllerRoute;
|
||||
|
||||
/// Combines the controller route with an optional relative sub path.
|
||||
String route([String? subPath]) {
|
||||
if (subPath == null || subPath.isEmpty) {
|
||||
return controllerRoute;
|
||||
}
|
||||
return '$controllerRoute/$subPath';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import 'package:ac_web/Api/Dtos/ReportDto.dart';
|
||||
import 'package:ac_web/Api/Services/ApiServiceBase.dart';
|
||||
|
||||
/// Access to the backend `ReportsController` (`/reports`).
|
||||
class ReportsService extends ApiServiceBase {
|
||||
const ReportsService(super.client);
|
||||
|
||||
@override
|
||||
String get controllerRoute => 'reports';
|
||||
|
||||
/// `GET /reports` - all reports, newest first.
|
||||
Future<List<ReportDto>> getReports() async {
|
||||
final reports = await client.getList(route(), ReportDto.fromJson);
|
||||
final sorted = List<ReportDto>.of(reports);
|
||||
sorted.sort((left, right) => right.timestamp.compareTo(left.timestamp));
|
||||
return sorted;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user