id.unum.service.ReceiptService Maven / Gradle / Ivy
The newest version!
package id.unum.service;
import id.unum.dto.DidDocument;
import id.unum.dto.RegisteredIssuer;
import id.unum.dto.Unum;
import id.unum.error.UnumError;
import id.unum.facade.rest.Client;
import id.unum.facade.rest.UnumAPIService;
import id.unum.protos.receipt.v1.PresentationVerifiedReceiptOptions;
import id.unum.protos.receipt.v1.Receipt;
import id.unum.protos.receipt.v1.Verified;
import lombok.extern.log4j.Log4j2;
import retrofit2.Call;
import retrofit2.Response;
import java.io.IOException;
import java.util.List;
import static id.unum.converter.ProtoToDto.convertCredential;
@Log4j2
public class ReceiptService implements ReceiptServiceInterface{
private UnumAPIService _unumService;
public ReceiptService (String url) {
Client client = new Client(url);
_unumService = client.getRetrofit().create(UnumAPIService.class);
}
@Override
public Unum sendPresentationVerifiedReceipt(String authToken, String verifierDid, String subjectDid, String reply, Boolean isVerified, String requestId, String requestUuid, String reason, List issuers, List credentialTypes) {
Verified verified = Verified.newBuilder()
.setIsVerified(isVerified)
.addAllCredentialTypes(credentialTypes)
.addAllIssuers(issuers)
.setReason(reason)
.setReply(reply)
.setRequestId(requestId)
.setRequestUuid(requestUuid)
.build();
// send the PresentationVerified Receipt
PresentationVerifiedReceiptOptions receiptOptions = PresentationVerifiedReceiptOptions.newBuilder()
.setType("PresentationVerified")
.setVerifier(verifierDid)
.setSubject(subjectDid)
.setData(verified)
.build();
Unum receipt = this.createPresentationVerifiedReceipt(authToken, receiptOptions);
return receipt;
}
@Override
public Unum createPresentationVerifiedReceipt(String authToken, PresentationVerifiedReceiptOptions options) {
Call call = _unumService.createPresentationVerifiedReceipt(authToken, options);
try {
Response response = call.execute();
if (!response.isSuccessful()) {
String message = response.errorBody() != null
? response.errorBody().string() : "Unknown error";
log.error("Saas error calling register issuer " + message);
throw new UnumError(response.errorBody().hashCode(), message);
}
String newAuthToken = response.headers().get("X-Auth-Token");
Unum result = new Unum();
result.setBody(response.body());
result.setAuthToken(newAuthToken);
return result;
} catch (IOException e) {
log.error("IOException calling saas create receipt: " + e.getMessage());
e.printStackTrace();
}
throw new UnumError(500, "Error creating receipt.");
}
}