writeLinks method

Future<List<String>> writeLinks(
  1. String enrollmentId,
  2. Map<String, LinkWrite> links
)

Sends one enroll:link for enrollmentId carrying links, and returns the kids the atServer reports linked.

Throws when the atServer refuses the call, and when its answer carries no linked list: only a reply naming what it linked counts as success.

Implementation

Future<List<String>> writeLinks(
    String enrollmentId, Map<String, LinkWrite> links) async {
  final builder = _EnrollLinkBuilder()
    ..operation = EnrollOperationEnum.link
    ..enrollmentId = enrollmentId
    ..links = {
      for (final entry in links.entries)
        entry.key: {'link': entry.value.link, 'expect': entry.value.expect}
    };
  final String response;
  try {
    response = await _atClient.getRemoteSecondary()!.executeVerb(builder);
  } on InvalidSyntaxException {
    if (matchesEnrollGrammar(builder.buildCommand())) {
      _withoutEnrollLink[_atClient] = true;
    }
    rethrow;
  }
  if (!response.startsWith('data:')) {
    throw AtEnrollmentException(
        'enroll:link for enrollment $enrollmentId was refused: $response');
  }
  final decoded = jsonDecode(response.substring('data:'.length).trim());
  final linked = decoded is Map ? decoded['linked'] : null;
  if (linked is! List) {
    _withoutEnrollLink[_atClient] = true;
    throw AtEnrollmentException('enroll:link for enrollment $enrollmentId '
        'was answered without saying what it linked: $response');
  }
  return [
    for (final kid in linked)
      if (kid is String) kid
  ];
}