keys method

List<String> keys()

Every exchange the store holds, as METHOD url, in file order.

Read by a refusal rather than by the funnel: a miss is worth answering with what the store does have for that host, and the walk is only paid for on the step that already failed.

Implementation

List<String> keys() {
  var dir = Directory(directory);
  if (!dir.existsSync()) return const [];
  var keys = <String>[];
  for (var entry in dir.listSync().whereType<File>()) {
    // `.body.json` is a recorded *body*, not a recording. Parsed as one, an
    // API that answers `{"method": …, "url": …}` — a webhook or audit-log
    // endpoint — would list an exchange the store does not hold, and the
    // refusal would be lying to whoever is reading it.
    var name = p.basename(entry.path);
    if (!name.endsWith('.json') || name.contains('.body.')) continue;
    try {
      var json = jsonDecode(entry.readAsStringSync());
      if (json case {'method': String method, 'url': String url}) {
        keys.add('$method $url');
      }
    } on FormatException {
      // A file somebody hand-edited into invalid JSON is not a reason for a
      // refusal to fail instead of explaining itself.
      continue;
    }
  }
  return keys..sort();
}