redactBody static method
Return body with the value under every key in keys masked, when
it parses as a JSON object or array, or reads as a form-encoded body
(key=value&key=value, an OAuth token call or a form login).
Anything else (plain text, a truncated JSON snippet) and a body with nothing to hide come back as given, byte for byte, so the formatting survives; in a form body only the value of a matching pair changes. Never throws on a malformed body. Use it for a body an adapter holds as a string, before truncating it: a cut JSON body no longer parses, so the store could not mask it afterwards.
Implementation
static String? redactBody(String? body, Set<String> keys) {
if (body == null || body.isEmpty) return body;
final Set<String> hidden = keys.map(_normalize).toSet();
final Object? decoded;
try {
decoded = jsonDecode(body);
} on FormatException {
// Not JSON: a form body is masked pair by pair, and anything else
// (plain text, a truncated payload) has no keys to find.
return _redactForm(body, hidden);
}
if (!_mentions(decoded, hidden)) return body;
return jsonEncode(_redact(decoded, hidden));
}