import 'package:ac_web/Api/JsonReader.dart'; /// Dart counterpart of the backend `AgentTaskDto`. /// /// The exact shape of the backend dto is not final yet, therefore everything /// except the name is treated as optional. Unknown properties are simply /// ignored, so this dto keeps working while the api grows. class AgentTaskDto { const AgentTaskDto({ required this.name, this.id, this.description, this.status, this.progress, this.createdAt, this.finishedAt, }); final String name; final String? id; final String? description; final String? status; /// Completion between 0 and 100 if the api reports it. final int? progress; final DateTime? createdAt; final DateTime? finishedAt; bool get isFinished => finishedAt != null; factory AgentTaskDto.fromJson(Map json) { return AgentTaskDto( name: JsonReader.optionalString(json, 'Name') ?? '', id: JsonReader.optionalString(json, 'Id'), description: JsonReader.optionalString(json, 'Description'), status: JsonReader.optionalString(json, 'Status'), progress: JsonReader.optionalInt(json, 'Progress'), createdAt: JsonReader.optionalDateTime(json, 'CreatedAt'), finishedAt: JsonReader.optionalDateTime(json, 'FinishedAt'), ); } Map toJson() { return { 'Name': name, if (id != null) 'Id': id, if (description != null) 'Description': description, if (status != null) 'Status': status, if (progress != null) 'Progress': progress, if (createdAt != null) 'CreatedAt': createdAt!.toIso8601String(), if (finishedAt != null) 'FinishedAt': finishedAt!.toIso8601String(), }; } @override String toString() => 'AgentTaskDto(name: $name, status: $status)'; }