dvOfflineSendOverHttp function

DVOfflineSend dvOfflineSendOverHttp({
  1. required Uri endpoint(),
  2. Map<String, String> headers()?,
})

Posts replay requests to endpoint as JSON, with the session headers the generated client sends on every call and a CSRF token, because a browser is one of the clients that replays and the route checks one.

Implementation

DVOfflineSend dvOfflineSendOverHttp({
  required Uri Function() endpoint,
  Map<String, String> Function()? headers,
}) {
  return (Map<String, Object?> body) async {
    final DVHttpResponse response = await dvSendHttpRequest(DVHttpRequest(
      url: endpoint(),
      method: 'POST',
      headers: <String, String>{
        ...?headers?.call(),
        'content-type': 'application/json; charset=utf-8',
        DVCSRF.headerName: const DVCSRF().token(),
      },
      body: utf8.encode(jsonEncode(body)),
    ));
    Map<String, Object?>? decoded;
    try {
      final Object? parsed = jsonDecode(response.body);
      if (parsed is Map<String, Object?>) decoded = parsed;
    } on FormatException {
      decoded = null;
    }
    return DVOfflineReply(response.statusCode, decoded);
  };
}