feat(app): add template catalogue and create-only submit repository
Add the nonvisual templates data/domain slice: TemplateRepository over the shared ApiGateway for list_templates, validate and create-only submit, and TemplateEntry preserving every server field in a deeply read-only raw map. Re-include test/**/data/ in app/.gitignore so the mirroring tests are not silently dropped from a fresh checkout. Tests cover exact request shapes, source preservation, blank-name refusal, malformed replies, server refusals and immutability.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/template_entry.dart';
|
||||
|
||||
/// A templates protocol refusal kept separate from transport errors; server
|
||||
/// error envelopes are propagated unchanged by [ApiGateway].
|
||||
class TemplateRepositoryException implements Exception {
|
||||
const TemplateRepositoryException(this.message);
|
||||
final String message;
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
/// Template catalogue, definition validation and create-only submission over
|
||||
/// the application's shared gateway.
|
||||
///
|
||||
/// These are reads plus one explicit submit. No method publishes, runs,
|
||||
/// overwrites or schedules a pipeline, so merely loading or validating a
|
||||
/// template can never start work.
|
||||
class TemplateRepository {
|
||||
TemplateRepository(this._gateway);
|
||||
final ApiGateway _gateway;
|
||||
|
||||
/// The server's template catalogue, exactly as `list_templates` returns it.
|
||||
/// A reply without a `templates` field is the older server answering with no
|
||||
/// catalogue, which is an empty list rather than an error.
|
||||
Future<List<TemplateEntry>> listTemplates() async {
|
||||
final reply = await _gateway.call('list_templates');
|
||||
final templates = reply['templates'];
|
||||
if (templates == null) return const [];
|
||||
if (templates is! List) {
|
||||
throw const TemplateRepositoryException(
|
||||
'The server returned no template catalogue.',
|
||||
);
|
||||
}
|
||||
return List.unmodifiable(
|
||||
templates.map((entry) {
|
||||
if (entry is! Map) {
|
||||
throw const TemplateRepositoryException(
|
||||
'The server returned a template that is not an object.',
|
||||
);
|
||||
}
|
||||
return TemplateEntry.fromJson(Map<String, dynamic>.from(entry));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/// Parses one definition source on the server (`validate`) and returns the
|
||||
/// graph it produced. The source is sent byte-for-byte: no trimming, casing or
|
||||
/// whitespace changes. A malformed definition rejects with the server's own
|
||||
/// error; a reply without a graph is refused rather than reported as valid.
|
||||
Future<JsonMap> validate(String source) async {
|
||||
final reply = await _gateway.call('validate', {'source': source});
|
||||
final graph = reply['graph'];
|
||||
if (graph is! Map) {
|
||||
throw const TemplateRepositoryException('The server returned no graph.');
|
||||
}
|
||||
return Map<String, dynamic>.from(graph);
|
||||
}
|
||||
|
||||
/// Creates a new draft pipeline from [source] (`submit`), create-only.
|
||||
///
|
||||
/// A blank name is refused locally before anything is sent, matching the
|
||||
/// browser and Android clients; the name that is sent is trimmed. The source
|
||||
/// is not changed. `create_only` is always true, so an existing pipeline can
|
||||
/// never be overwritten, and no run is started.
|
||||
Future<JsonMap> submit(String name, String source) async {
|
||||
final trimmed = name.trim();
|
||||
if (trimmed.isEmpty) {
|
||||
throw const TemplateRepositoryException('Enter a pipeline name.');
|
||||
}
|
||||
final reply = await _gateway.call('submit', {
|
||||
'name': trimmed,
|
||||
'source': source,
|
||||
'create_only': true,
|
||||
});
|
||||
final detail = reply['detail'];
|
||||
if (detail is! Map) {
|
||||
throw const TemplateRepositoryException(
|
||||
'The server returned no pipeline detail.',
|
||||
);
|
||||
}
|
||||
return Map<String, dynamic>.from(detail);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user