merge(app): land checked templates and create-only request layer
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// One template from the server catalogue.
|
||||
///
|
||||
/// The server owns this record: [key], [name], [description], [source] and
|
||||
/// [scheduled] are read from it verbatim and every other field is kept in
|
||||
/// [raw], so a later view or round-trip never loses fields this client does not
|
||||
/// understand yet. Loading a template is a read; it never publishes or runs it.
|
||||
class TemplateEntry {
|
||||
const TemplateEntry({
|
||||
required this.key,
|
||||
required this.name,
|
||||
required this.description,
|
||||
required this.source,
|
||||
required this.scheduled,
|
||||
required this.raw,
|
||||
});
|
||||
|
||||
factory TemplateEntry.fromJson(JsonMap json) {
|
||||
final raw = _frozenMap(json);
|
||||
return TemplateEntry(
|
||||
key: _text(raw['key']),
|
||||
name: _text(raw['name']),
|
||||
description: _text(raw['description']),
|
||||
source: _text(raw['source']),
|
||||
scheduled: raw['scheduled'] == true,
|
||||
raw: raw,
|
||||
);
|
||||
}
|
||||
|
||||
/// The stable catalogue key the gallery selects on.
|
||||
final String key;
|
||||
final String name;
|
||||
final String description;
|
||||
|
||||
/// The template's complete, untrimmed definition source.
|
||||
final String source;
|
||||
|
||||
/// Whether the source declares a schedule (the summary's "Includes a schedule").
|
||||
final bool scheduled;
|
||||
|
||||
/// Every field the server sent, including fields this client does not model.
|
||||
/// Deeply unmodifiable: nested maps and lists are copied into read-only
|
||||
/// collections too, so a view cannot mutate the server reply through it.
|
||||
final JsonMap raw;
|
||||
|
||||
static String _text(dynamic value) => value == null ? '' : '$value';
|
||||
|
||||
static JsonMap _frozenMap(Map<String, dynamic> json) =>
|
||||
Map<String, dynamic>.unmodifiable(
|
||||
json.map((key, value) => MapEntry(key, _frozen(value))),
|
||||
);
|
||||
|
||||
static dynamic _frozen(dynamic value) {
|
||||
if (value is Map) {
|
||||
return _frozenMap(Map<String, dynamic>.from(value));
|
||||
}
|
||||
if (value is List) {
|
||||
return List<dynamic>.unmodifiable(value.map(_frozen));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user