returnPublication method

  1. @override
Future<Try<bool, LcpException>> returnPublication()
override

Returns the publication to its provider.

Implementation

@override
Future<Try<bool, LcpException>> returnPublication() async {
  try {
    StatusDocument? status = _documents.status;
    Uri? url;
    try {
      url = status?.url(StatusRel.ret,
          preferredType: null, parameters: await _device.asQueryParameters);
    } on Error {
      url = null;
    }
    if (status == null || url == null) {
      throw LcpException.licenseInteractionNotAvailable;
    }

    await _network.fetch(url.toString(), method: Method.put).then((value) =>
        value
            .onSuccess((data) => _validateStatusDocument(data))
            .onFailure((error) {
          switch (error.status) {
            case HttpStatus.badRequest:
              throw LcpException.return_.returnFailed;
            case HttpStatus.forbidden:
              throw LcpException.return_.alreadyReturnedOrExpired;
            default:
              throw LcpException.return_.unexpectedServerError;
          }
        }));

    return Try.success(true);
  } on Exception catch (e) {
    return Try.failure(LcpException.wrap(e));
  }
}