send<P, R> method

Future<TrustTaskDocument<R>> send<P, R>(
  1. TrustTaskDocument<P> request, {
  2. required Object? encode(
    1. P payload
    ),
  3. required R decodeResponse(
    1. Map<String, dynamic> json
    ),
})

Send request and return the #response document.

issuer, recipient and issuedAt are filled from myVid, serverVid and the clock where the request leaves them unset. Throws a HttpsClientException for anything but a response that belongs to this request.

Implementation

Future<TrustTaskDocument<R>> send<P, R>(
  TrustTaskDocument<P> request, {
  required Object? Function(P payload) encode,
  required R Function(Map<String, dynamic> json) decodeResponse,
}) async {
  final stamped = _stamp(request);
  final reply = await _post(stamped.toJson(encode));

  if (reply.statusCode == 202 || reply.statusCode == 204) {
    throw DuplicateAbsorbedException(reply.statusCode);
  }
  _throwIfError(reply, stamped);
  if (reply.bodyBytes.isEmpty) {
    throw DuplicateAbsorbedException(reply.statusCode);
  }

  final Map<String, dynamic> json;
  final TrustTaskDocument<R> response;
  try {
    json = jsonDecode(utf8.decode(reply.bodyBytes)) as Map<String, dynamic>;
    response = TrustTaskDocument<R>.fromJson(
      json,
      (p) => decodeResponse(p! as Map<String, dynamic>),
    );
  } on Object catch (e) {
    throw ResponseDecodeException(e);
  }

  _checkBinding(stamped, response);
  await _verify(json);
  return response;
}