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

com.bloxbean.cardano.client.backend.gql.GqlMetadataService Maven / Gradle / Ivy

package com.bloxbean.cardano.client.backend.gql;

import com.bloxbean.cardano.client.backend.api.MetadataService;
import com.bloxbean.cardano.client.api.common.OrderEnum;
import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.model.metadata.MetadataCBORContent;
import com.bloxbean.cardano.client.backend.model.metadata.MetadataJSONContent;
import com.bloxbean.cardano.client.backend.model.metadata.MetadataLabel;
import com.bloxbean.cardano.gql.MetadataQuery;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import okhttp3.OkHttpClient;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GqlMetadataService extends BaseGqlService implements MetadataService {
    ObjectMapper objectMapper;
    public GqlMetadataService(String gqlUrl) {
        super(gqlUrl);
        this.objectMapper = new ObjectMapper();
    }

    public GqlMetadataService(String gqlUrl, Map headers) {
        super(gqlUrl, headers);
        this.objectMapper = new ObjectMapper();
    }

    public GqlMetadataService(String gqlUrl, OkHttpClient okHttpClient) {
        super(gqlUrl, okHttpClient);
        this.objectMapper = new ObjectMapper();
    }

    @Override
    public Result> getJSONMetadataByTxnHash(String txnHash) throws ApiException {
        MetadataQuery query = new MetadataQuery(txnHash);
        MetadataQuery.Data data = execute(query);
        if(data == null)
            return Result.error("Unable to get transaction with txnhash: " + txnHash);

        List transactions = data.transactions();
        if(transactions == null || transactions.size() == 0)
            return Result.error("Unable to get transaction with txnhash: " + txnHash);

        MetadataQuery.Transaction transaction = transactions.get(0);
        List metadatumList = transaction.metadata();
        List metadataJSONContentList = new ArrayList<>();
        for(MetadataQuery.Metadatum metadatum: metadatumList) {
            MetadataJSONContent metadataJSONContent = new MetadataJSONContent();
            metadataJSONContent.setTxHash(txnHash);
            metadataJSONContent.setLabel(metadatum.key());

            JsonNode jsonNode = convertToJsonNode(metadatum);
            metadataJSONContent.setJsonMetadata(jsonNode);
            metadataJSONContentList.add(metadataJSONContent);
        }


        return processSuccessResult(metadataJSONContentList);
    }

    private JsonNode convertToJsonNode(MetadataQuery.Metadatum metadatum) {
        try {
            return metadatum.value();
//            return objectMapper.readTree(metadatum.value().toString());
        } catch (Exception e) {
            return new TextNode(metadatum.value().toString());
        }
    }

    @Override
    public Result> getCBORMetadataByTxnHash(String txnHash) throws ApiException {
        throw new UnsupportedOperationException("No supported");
    }

    @Override
    public Result> getMetadataLabels(int count, int page, OrderEnum order) throws ApiException {
        throw new UnsupportedOperationException("No supported");
    }

    @Override
    public Result> getJSONMetadataByLabel(BigInteger label, int count, int page, OrderEnum order) throws ApiException {
        throw new UnsupportedOperationException("No supported");
    }

    @Override
    public Result> getCBORMetadataByLabel(BigInteger label, int count, int page, OrderEnum order) throws ApiException {
        throw new UnsupportedOperationException("No supported");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy