All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nem.symbol.sdk.infrastructure.vertx.ReceiptRepositoryVertxImpl Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*
 * Copyright 2020 NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.nem.symbol.sdk.infrastructure.vertx;

import io.nem.symbol.sdk.api.ReceiptRepository;
import io.nem.symbol.sdk.model.blockchain.MerklePathItem;
import io.nem.symbol.sdk.model.blockchain.MerkleProofInfo;
import io.nem.symbol.sdk.model.blockchain.Position;
import io.nem.symbol.sdk.model.network.NetworkType;
import io.nem.symbol.sdk.model.receipt.Statement;
import io.nem.symbol.sdk.openapi.vertx.api.ReceiptRoutesApi;
import io.nem.symbol.sdk.openapi.vertx.api.ReceiptRoutesApiImpl;
import io.nem.symbol.sdk.openapi.vertx.invoker.ApiClient;
import io.nem.symbol.sdk.openapi.vertx.model.MerkleProofInfoDTO;
import io.nem.symbol.sdk.openapi.vertx.model.StatementsDTO;
import io.reactivex.Observable;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import java.math.BigInteger;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;


/**
 * OkHttp implementation of {@link ReceiptRepository}.
 */
public class ReceiptRepositoryVertxImpl extends AbstractRepositoryVertxImpl implements
    ReceiptRepository {

    private final ReceiptRoutesApi client;

    private final Observable networkTypeObservable;

    public ReceiptRepositoryVertxImpl(ApiClient apiClient,
        Observable networkTypeObservable) {
        super(apiClient);
        this.client = new ReceiptRoutesApiImpl(apiClient);
        this.networkTypeObservable = networkTypeObservable;
    }

    @Override
    public Observable getBlockReceipts(BigInteger height) {
        Consumer>> callback = handler ->
            getClient().getBlockReceipts(height, handler);
        return exceptionHandling(
            networkTypeObservable.flatMap(networkType -> call(callback).map(statementsDTO ->
                new ReceiptMappingVertx(getJsonHelper())
                    .createStatementFromDto(statementsDTO, networkType))));
    }


    @Override
    public Observable getMerkleReceipts(BigInteger height, String hash) {

        Consumer>> callback = handler ->
            getClient().getMerkleReceipts(height, hash, handler);
        return exceptionHandling(call(callback).map(this::toMerkleProofInfo));
    }


    private MerkleProofInfo toMerkleProofInfo(MerkleProofInfoDTO dto) {
        List pathItems =
            dto.getMerklePath().stream()
                .map(
                    pathItem ->
                        new MerklePathItem(pathItem.getPosition() == null ? null
                            : Position.rawValueOf(pathItem.getPosition().getValue()),
                            pathItem.getHash()))
                .collect(Collectors.toList());
        return new MerkleProofInfo(pathItems);
    }


    public ReceiptRoutesApi getClient() {
        return client;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy