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

io.proximax.sdk.infrastructure.AccountHttp 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.stream;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

import io.proximax.sdk.AccountRepository;
import io.proximax.sdk.BlockchainApi;
import io.proximax.sdk.gen.model.AccountInfoDTO;
import io.proximax.sdk.gen.model.AccountPropertiesInfoDTO;
import io.proximax.sdk.gen.model.MultisigAccountGraphInfoDTO;
import io.proximax.sdk.gen.model.MultisigAccountInfoDTO;
import io.proximax.sdk.model.account.AccountInfo;
import io.proximax.sdk.model.account.Address;
import io.proximax.sdk.model.account.MultisigAccountGraphInfo;
import io.proximax.sdk.model.account.MultisigAccountInfo;
import io.proximax.sdk.model.account.PublicAccount;
import io.proximax.sdk.model.account.props.AccountProperties;
import io.proximax.sdk.model.transaction.AggregateTransaction;
import io.proximax.sdk.model.transaction.Transaction;
import io.reactivex.Observable;

/**
 * Account http repository.
 *
 * @since 1.0
 */
public class AccountHttp extends Http implements AccountRepository {

   private static final String ROUTE = "/account";
   private static final String PROPERTIES_SUFFIX = "/properties";

   private static final Type TYPE_ACCOUNT_LIST = new TypeToken>(){}.getType();
   private static final Type TYPE_MULTISIGACCT_LIST = new TypeToken>(){}.getType();
   private static final Type TYPE_ACCTPROPS_LIST = new TypeToken>(){}.getType();
   
   public AccountHttp(BlockchainApi api) {
      super(api);
   }

   @Override
   public Observable getAccountInfo(Address address) {
      return this.client.get(ROUTE + SLASH + address.plain()).map(Http::mapStringOrError)
            .map(str -> gson.fromJson(str, AccountInfoDTO.class))
            .map(AccountInfo::fromDto);
   }

   private List toAccountInfo(String json) {
      return gson.fromJson(json, TYPE_ACCOUNT_LIST);
   }
   
   private List toMultisigAccountInfo(String json) {
      return gson.fromJson(json, TYPE_MULTISIGACCT_LIST);
   }
   
   private List toAccountProperties(String json) {
      return gson.fromJson(json, TYPE_ACCTPROPS_LIST);
   }
   
   @Override
   public Observable> getAccountsInfo(List
addresses) { // prepare JSON array with addresses JsonArray arr = new JsonArray(addresses.size()); addresses.stream().map(Address::plain).forEachOrdered(arr::add); JsonObject requestBody = new JsonObject(); requestBody.add("addresses", arr); return this.client.post(ROUTE, requestBody) .map(Http::mapStringOrError) .map(this::toAccountInfo) .flatMapIterable(item -> item) .map(AccountInfo::fromDto) .toList().toObservable(); } @Override public Observable getMultisigAccountInfo(Address address) { return this.client.get(ROUTE + SLASH + address.plain() + "/multisig").map(Http::mapStringOrError) .map(str -> gson.fromJson(str, MultisigAccountInfoDTO.class)) .map(dto -> MultisigAccountInfo.fromDto(dto, api.getNetworkType())); } @Override public Observable getMultisigAccountGraphInfo(Address address) { return this.client.get(ROUTE + SLASH + address.plain() + "/multisig/graph") .map(Http::mapStringOrError) .map(this::toMultisigAccountInfo) .map(dto -> MultisigAccountGraphInfo.fromDto(dto, api.getNetworkType())); } @Override public Observable getAccountProperties(Address address) { return this.client.get(ROUTE + SLASH + address.plain() + PROPERTIES_SUFFIX).map(Http::mapStringOrError) .map(str -> gson.fromJson(str, AccountPropertiesInfoDTO.class)) .map(AccountPropertiesInfoDTO::getAccountProperties).map(AccountProperties::fromDto); } @Override public Observable> getAccountProperties(List
addresses) { // prepare JSON array with addresses JsonArray arr = new JsonArray(addresses.size()); addresses.stream().map(Address::plain).forEachOrdered(arr::add); JsonObject requestBody = new JsonObject(); requestBody.add("addresses", arr); // post to the API return this.client.post(ROUTE + PROPERTIES_SUFFIX, requestBody).map(Http::mapStringOrError) .map(this::toAccountProperties) .flatMapIterable(item -> item).map(AccountPropertiesInfoDTO::getAccountProperties) .map(AccountProperties::fromDto).toList().toObservable(); } @Override public Observable> transactions(PublicAccount publicAccount) { return this.transactions(publicAccount, Optional.empty()); } @Override public Observable> transactions(PublicAccount publicAccount, QueryParams queryParams) { return this.transactions(publicAccount, Optional.of(queryParams)); } private Observable> transactions(PublicAccount publicAccount, Optional queryParams) { return this.findTransactions(publicAccount.getPublicKey(), queryParams, "/transactions"); } @Override public Observable> incomingTransactions(PublicAccount publicAccount) { return this.incomingTransactions(publicAccount.getPublicKey(), Optional.empty()); } @Override public Observable> incomingTransactions(PublicAccount publicAccount, QueryParams queryParams) { return this.incomingTransactions(publicAccount.getPublicKey(), Optional.of(queryParams)); } @Override public Observable> incomingTransactions(Address address) { return this.incomingTransactions(address.plain(), Optional.empty()); } @Override public Observable> incomingTransactions(Address address, QueryParams queryParams) { return this.incomingTransactions(address.plain(), Optional.of(queryParams)); } private Observable> incomingTransactions(String accountKey, Optional queryParams) { return this.findTransactions(accountKey, queryParams, "/transactions/incoming"); } @Override public Observable> outgoingTransactions(PublicAccount publicAccount) { return this.outgoingTransactions(publicAccount, Optional.empty()); } @Override public Observable> outgoingTransactions(PublicAccount publicAccount, QueryParams queryParams) { return this.outgoingTransactions(publicAccount, Optional.of(queryParams)); } private Observable> outgoingTransactions(PublicAccount publicAccount, Optional queryParams) { return this.findTransactions(publicAccount.getPublicKey(), queryParams, "/transactions/outgoing"); } @Override public Observable> aggregateBondedTransactions(PublicAccount publicAccount) { return this.aggregateBondedTransactions(publicAccount, Optional.empty()); } @Override public Observable> aggregateBondedTransactions(PublicAccount publicAccount, QueryParams queryParams) { return this.aggregateBondedTransactions(publicAccount, Optional.of(queryParams)); } private Observable> aggregateBondedTransactions(PublicAccount publicAccount, Optional queryParams) { return this.findTransactions(publicAccount.getPublicKey(), queryParams, "/transactions/partial").flatMapIterable(item -> item) .map(item -> (AggregateTransaction) item).toList().toObservable(); } @Override public Observable> unconfirmedTransactions(PublicAccount publicAccount) { return this.unconfirmedTransactions(publicAccount, Optional.empty()); } @Override public Observable> unconfirmedTransactions(PublicAccount publicAccount, QueryParams queryParams) { return this.unconfirmedTransactions(publicAccount, Optional.of(queryParams)); } private Observable> unconfirmedTransactions(PublicAccount publicAccount, Optional queryParams) { return this.findTransactions(publicAccount.getPublicKey(), queryParams, "/transactions/unconfirmed"); } private Observable> findTransactions(String accountKey, Optional queryParams, String path) { return this.client .get(ROUTE + SLASH + accountKey + path + (queryParams.isPresent() ? queryParams.get().toUrl() : "")) .map(Http::mapStringOrError) .map(str -> stream(new Gson().fromJson(str, JsonArray.class)).map(s -> (JsonObject) s) .collect(Collectors.toList())) .flatMapIterable(item -> item).map(new TransactionMapping()).toList().toObservable(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy