read method

ScenarioRecording? read(
  1. String method,
  2. Uri url
)

What method url was recorded as, or null when nothing was.

Implementation

ScenarioRecording? read(String method, Uri url) {
  var file = File(p.join(directory, '${_name(method, url)}.json'));
  if (!file.existsSync()) return null;
  var json = jsonDecode(file.readAsStringSync());
  if (json is! Map<String, Object?>) return null;
  // The url is read back off the file rather than trusted from the caller:
  // a digest collision, or a hand-edited file, must answer for what it
  // actually holds or refuse — never for what was asked.
  if (json['method'] != method || json['url'] != '$url') return null;
  return ScenarioRecording(
    method: method,
    url: url,
    status: switch (json['status']) {
      int code => code,
      _ => 200,
    },
    contentType: json['contentType'] as String?,
    headers: switch (json['headers']) {
      Map<String, Object?> raw => {
        for (var MapEntry(:key, :value) in raw.entries)
          if (value is List) key: [for (var item in value) '$item'],
      },
      _ => const {},
    },
    body: switch (json['body']) {
      String name => _readBody(p.join(directory, name)),
      _ => Uint8List(0),
    },
    recorded: switch (json['recorded']) {
      String raw => DateTime.tryParse(raw),
      _ => null,
    },
  );
}