use of com.nexblocks.authguard.service.model.IdempotentRecordBO in project AuthGuard by AuthGuard.
the class ExceptionHandlers method idempotencyException.
public static void idempotencyException(final IdempotencyException e, final Context context) {
final IdempotentRecordBO record = e.getIdempotentRecord();
final Error error = new Error(ErrorCode.IDEMPOTENCY_ERROR.getCode(), String.format("Idempotent key %s was already used to create entity %s of type %s", record.getIdempotentKey(), record.getEntityId(), record.getEntityType()));
context.status(409).json(error);
}
use of com.nexblocks.authguard.service.model.IdempotentRecordBO in project AuthGuard by AuthGuard.
the class IdempotencyServiceImpl method performOperation.
@Override
public <T extends Entity> CompletableFuture<T> performOperation(final Supplier<T> operation, final String idempotentKey, final String entityType) {
return findByKeyAndEntityType(idempotentKey, entityType).thenApplyAsync(record -> {
if (record.isPresent()) {
throw new IdempotencyException(record.get());
}
return operation.get();
}).thenApply(result -> {
final IdempotentRecordBO record = IdempotentRecordBO.builder().id(ID.generate()).entityId(result.getId()).entityType(result.getEntityType()).idempotentKey(idempotentKey).build();
// we don't have to wait for this to finish
CompletableFuture.runAsync(() -> create(record));
return result;
});
}
Aggregations