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

io.proximax.sdk.infrastructure.MosaicHttp Maven / Gradle / Ivy

Go to download

The ProximaX Sirius Chain Java SDK is a Java library for interacting with the Sirius Blockchain.

The newest version!
/*
 * Copyright 2018 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.proximax.sdk.infrastructure;

import static io.proximax.sdk.utils.GsonUtils.getJsonArray;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import io.proximax.sdk.BlockchainApi;
import io.proximax.sdk.MosaicRepository;
import io.proximax.sdk.gen.model.MosaicInfoDTO;
import io.proximax.sdk.gen.model.MosaicNamesDTO;
import io.proximax.sdk.model.mosaic.MosaicId;
import io.proximax.sdk.model.mosaic.MosaicInfo;
import io.proximax.sdk.model.mosaic.MosaicNames;
import io.proximax.sdk.model.transaction.UInt64Id;
import io.proximax.sdk.utils.dto.UInt64Utils;
import io.reactivex.Observable;


/**
 * Mosaic HTTP repository.
 */
public class MosaicHttp extends Http implements MosaicRepository {

   private static final String ROUTE = "/mosaic";
   private static final String NAMES_ROUTE = "/mosaic/names";

   private static final Type MOSAIC_INFO_LIST_TYPE = new TypeToken>(){}.getType();
   private static final Type MOSAIC_NAMES_LIST_TYPE = new TypeToken>(){}.getType();

   public MosaicHttp(BlockchainApi api) {
      super(api);
   }

   @Override
   public Observable getMosaic(MosaicId mosaicId) {
      return this.client.get(ROUTE + SLASH + mosaicId.getIdAsHex())
            .map(Http::mapStringOrError)
            .map(str -> gson.fromJson(str, MosaicInfoDTO.class))
            .map(dto -> MosaicInfo.fromDto(dto, api.getNetworkType()));
   }

   @Override
   public Observable> getMosaics(List mosaicIds) {
      JsonObject requestBody = new JsonObject();
      requestBody.add("mosaicIds", getJsonArray(mosaicIds, UInt64Id::getIdAsHex));
      return this.client.post(ROUTE, requestBody)
            .map(Http::mapStringOrError)
            .map(this::toMosaicInfoList)
            .flatMapIterable(item -> item)
            .map(dto -> MosaicInfo.fromDto(dto, api.getNetworkType()))
            .toList().toObservable();
   }

   @Override
   public Observable> getMosaicNames(List mosaicIds) {
      JsonObject requestBody = new JsonObject();
      requestBody.add("mosaicIds", getJsonArray(mosaicIds, UInt64Id::getIdAsHex));
      return this.client.post(NAMES_ROUTE, requestBody)
            .map(Http::mapStringOrError)
            .map(this::toMosaicNamesList)
            .flatMapIterable(item -> item)
            .map(mosaicNameDTO -> new MosaicNames(new MosaicId(UInt64Utils.toBigInt(mosaicNameDTO.getMosaicId())),
                  mosaicNameDTO.getNames()))
            .toList().toObservable();
   }
   
   private List toMosaicInfoList(String json) {
      return gson.fromJson(json, MOSAIC_INFO_LIST_TYPE);
   }
   
   private List toMosaicNamesList(String json) {
      return gson.fromJson(json, MOSAIC_NAMES_LIST_TYPE);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy