horizon/server.js

import { JsonApiServer } from '../json_api'
import * as resources from './resources'

/**
 * Facilitates interaction with a Horizon server instance.
 *
 * @class
 */
export class HorizonServer extends JsonApiServer {
  /**
   * Create a new Horizon instance.
   *
   * @constructor
   *
   * @param {ShelfNetwork} sdk Parent SDK instance.
   * @param {string} serverUrl Horizon server instance URL.
   * @param {Object} opts
   * @param {boolean} [opts.allowHttp] Allow connecting to http servers, default: `false`. This must be set to false in production deployments!
   * @param {Object} [opts.proxy] Proxy configuration. Look [axios docs](https://github.com/axios/axios#request-config) for more info
   * @param {Object} [opts.httpBasicAuth] HTTP basic auth credentials. Look [axios docs](https://github.com/axios/axios#request-config) for more info.
   * @param {Object} [opts.customHeaders] Custom headers for request.
   * @param {boolean} [opts.withCredentials] Indicates whether or not cross-site Access-Control requests should be made using credentials.
   */
  constructor (sdk, serverUrl, opts = {}) {
    opts.alias = 'horizon'
    super(sdk, serverUrl, opts)
  }

  /**
   * Get network details.
   *
   * @return {JsonApiResponse} Network details.
   */
  getNetworkDetails () {
    return this._makeCallBuilder().appendUrlSegment(``).get()
  }

  /**
   * Convert currency.
   *
   * @param {object} query Request options.
   * @param {string} query.from From currency.
   * @param {string} query.to To currency.
   * @param {Number} query.amount Ampount.
   * @return {JsonApiResponse} Network details.
   */
  convertCurrency (query) {
    return this._makeCallBuilder().appendUrlSegment(`convert`).get(query)
  }

  /**
   * Convert multiple amounts.
   *
   * @param {object[]} paris Array of currency paris for convertation.
   * @param {string} paris.from From currency.
   * @param {string} paris.to To currency.
   * @param {Number} paris.amount Amount.
   * @return {JsonApiResponse} Network details.
   */
  convertCurrencyBulk (paris) {
    return this._makeCallBuilder()
      .appendUrlSegment(`convert`)
      .post({
        data: paris.map(pair => ({
          attributes: {
            from: pair.from,
            to: pair.to,
            amount: pair.amount
          }
        }))
      })
  }

  /**
   * Migrate a legacy cardeal account.
   *
   * @param {string} email Email.
   * @param {string} password Password.
   * @param {string} [platformId] User's platform ID.
   *
   * @return {JsonApiResponse}
   */
  migrateLegacyAccount (email, password, platformId = null) {
    platformId = platformId || this._sdk.platformId

    return this._makeCallBuilder()
      .withCredentials()
      .appendUrlSegment('migration')
      .post({
        data: {
          attributes: {
            email,
            password,
            platformId
          }
        }
      })
  }

  /**
   * Account details.
   *
   * @return {Account}
   */
  get account () {
    return new resources.Account(this, this._sdk)
  }

  /**
   * Enums.
   *
   * @return {Enums}
   */
  get enums () {
    return new resources.Enums(this, this._sdk)
  }

  /**
   * Transactions.
   *
   * @return {Account}
   */
  get transactions () {
    return new resources.Transactions(this, this._sdk)
  }

  /**
   * Messages.
   *
   * @return {Messages}
   */
  get messages () {
    return new resources.Messages(this, this._sdk)
  }

  /**
   * Participants.
   *
   * @return {Participants}
   */
  get participants () {
    return new resources.Participants(this, this._sdk)
  }

  /**
   * Lots.
   *
   * @return {Lots}
   */
  get lots () {
    return new resources.Lots(this, this._sdk)
  }

  /**
   * ReviewableRequests.
   *
   * @return {ReviewableRequests}
   */
  get reviewableRequests () {
    return new resources.ReviewableRequests(this, this._sdk)
  }

  /**
   * Deposits management.
   *
   * @return {Deposits}
   */
  get deposits () {
    return new resources.Deposits(this, this._sdk)
  }
}