Search in sources :

Example 1 with IdempotentRecordBO

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);
}
Also used : IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) Error(com.nexblocks.authguard.api.dto.entities.Error) RequestValidationError(com.nexblocks.authguard.api.dto.entities.RequestValidationError)

Example 2 with IdempotentRecordBO

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;
    });
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) Entity(com.nexblocks.authguard.service.model.Entity) Inject(com.google.inject.Inject) IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) Optional(java.util.Optional) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CompletableFuture(java.util.concurrent.CompletableFuture) IdempotentRecordsRepository(com.nexblocks.authguard.dal.persistence.IdempotentRecordsRepository) Supplier(java.util.function.Supplier) ID(com.nexblocks.authguard.service.util.ID) IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException)

Aggregations

IdempotentRecordBO (com.nexblocks.authguard.service.model.IdempotentRecordBO)2 Inject (com.google.inject.Inject)1 Error (com.nexblocks.authguard.api.dto.entities.Error)1 RequestValidationError (com.nexblocks.authguard.api.dto.entities.RequestValidationError)1 IdempotentRecordsRepository (com.nexblocks.authguard.dal.persistence.IdempotentRecordsRepository)1 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)1 IdempotencyException (com.nexblocks.authguard.service.exceptions.IdempotencyException)1 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)1 Entity (com.nexblocks.authguard.service.model.Entity)1 ID (com.nexblocks.authguard.service.util.ID)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Supplier (java.util.function.Supplier)1