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

com.ryuta46.nemkotlin.client.RxNemApiClient.kt Maven / Gradle / Ivy

Go to download

nem-kotlin is client library for easy use of NEM(New Economy Movement) API.

There is a newer version: 0.6.0
Show newest version
/*
 * MIT License
 *
 * Copyright (c) 2017 Taizo Kusuda 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.ryuta46.nemkotlin.client

import com.ryuta46.nemkotlin.model.*
import com.ryuta46.nemkotlin.net.HttpClient
import com.ryuta46.nemkotlin.net.HttpURLConnectionClient
import com.ryuta46.nemkotlin.util.Logger
import com.ryuta46.nemkotlin.util.NoOutputLogger
import io.reactivex.Observable

/**
 * NEM API client to NIS.
 * This class returns an observable object of RxJava.
 *
 * This class calls each NemApiClient method internally.
 *
 * @param hostUrl NIS host URL.
 * @param httpClient HTTP client to communicate with NIS.
 * @param logger Logging function.
 */
class RxNemApiClient(hostUrl: String,
                     httpClient: HttpClient = HttpURLConnectionClient(),
                     logger: Logger = NoOutputLogger()) {
    val syncClient: NemApiClient = NemApiClient(hostUrl, httpClient, logger)

    /**
     * Creates an observable object
     */
    inline fun  observe(crossinline body: () -> T): Observable {
        return Observable.create { subscriber ->
            try {
                val response = body()
                if (response != null) subscriber.onNext(response)
            } catch (e: Throwable) {
                subscriber.onError(e)
            }
            subscriber.onComplete()
        }

    }

    /**
     * @see NemApiClient.get
     */
    inline fun get(path: String, query: Map = emptyMap()): Observable =
            observe { syncClient.get(path, query) }

    /**
     * @see NemApiClient.post
     */
    inline fun post(path: String, body: R, query: Map = emptyMap()): Observable =
            observe { syncClient.post(path, body, query) }



    /**
     * @see NemApiClient.heartbeat
     */
    fun heartbeat(): Observable =
            observe { syncClient.heartbeat() }

    /**
     * @see NemApiClient.status
     */
    fun status(): Observable =
            observe { syncClient.status() }

    /**
     * @see NemApiClient.accountGet
     */
    fun accountGet(address: String): Observable =
            observe { syncClient.accountGet(address) }

    /**
     * @see NemApiClient.accountGetFromPublicKey
     */
    fun accountGetFromPublicKey(publicKey: String): Observable =
            observe { syncClient.accountGetFromPublicKey(publicKey) }

    /**
     * @see NemApiClient.accountGetForwarded
     */
    fun accountGetForwarded(address: String): Observable =
            observe { syncClient.accountGetForwarded(address) }

    /**
     * @see NemApiClient.accountGetForwardedFromPublicKey
     */
    fun accountGetForwardedFromPublicKey(publicKey: String): Observable =
            observe { syncClient.accountGetFromPublicKey(publicKey) }

    /**
     * @see NemApiClient.accountStatus
     */
    fun accountStatus(address: String): Observable =
            observe { syncClient.accountStatus(address) }

    /**
     * @see NemApiClient.accountTransfersIncoming
     */
    fun accountTransfersIncoming(address: String, hash: String = "", id: Int = -1): Observable> =
            observe { syncClient.accountTransfersIncoming(address, hash, id) }

    /**
     * @see NemApiClient.accountTransfersOutgoing
     */
    fun accountTransfersOutgoing(address: String, hash: String = "", id: Int = -1): Observable> =
            observe { syncClient.accountTransfersOutgoing(address, hash, id) }

    /**
     * @see NemApiClient.accountTransfersAll
     */
    fun accountTransfersAll(address: String, hash: String = "", id: Int = -1): Observable> =
            observe { syncClient.accountTransfersAll(address, hash, id) }

    /**
     * @see NemApiClient.accountUnconfirmedTransactions
     */
    fun accountUnconfirmedTransactions(address: String): Observable> =
            observe { syncClient.accountUnconfirmedTransactions(address) }

    /**
     * @see NemApiClient.accountHarvests
     */
    fun accountHarvests(address: String): Observable> =
            observe { syncClient.accountHarvests(address) }

    /**
     * @see NemApiClient.accountImportances
     */
    fun accountImportances(): Observable> =
            observe { syncClient.accountImportances() }

    /**
     * @see NemApiClient.accountNamespacePage
     */
    fun accountNamespacePage(address: String, parent: String = "", id: Int = -1, pageSize: Int = -1): Observable> =
            observe { syncClient.accountNamespacePage(address, parent, id, pageSize) }

    /**
     * @see NemApiClient.accountMosaicOwned
     */
    fun accountMosaicOwned(address: String): Observable> =
            observe { syncClient.accountMosaicOwned(address) }

    /**
     * @see NemApiClient.accountHistoricalGet
     */
    fun accountHistoricalGet(address: String, startHeight: Int, endHeight: Int, increment: Int): Observable> =
            observe { syncClient.accountHistoricalGet(address, startHeight, endHeight, increment) }

    /**
     * @see NemApiClient.namespaceMosaicDefinitionPage
     */
    fun namespaceMosaicDefinitionPage(namespace: String, id: Int = -1, pageSize: Int = -1): Observable> =
            observe { syncClient.namespaceMosaicDefinitionPage(namespace, id, pageSize) }

    /**
     * @see NemApiClient.namespaceMosaicDefinitionFromName
     */
    fun namespaceMosaicDefinitionFromName(namespace: String, name: String): Observable =
            observe { syncClient.namespaceMosaicDefinitionFromName(namespace, name) }

    /**
     * @see NemApiClient.transactionAnnounce
     */
    fun transactionAnnounce(requestAnnounce: RequestAnnounce): Observable =
            observe { syncClient.transactionAnnounce(requestAnnounce) }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy