class Xooa::Api::QueryApi

Attributes

debugging[RW]
logger[RW]
requestUtil[RW]

Public Class Methods

new(appUrl, apiToken, debugging) click to toggle source
# File lib/xooa/api/QueryApi.rb, line 36
def initialize(appUrl, apiToken, debugging)

  @appUrl = appUrl
  @apiToken = apiToken
  @requestUtil = Xooa::Util::RequestUtil.new
  @logger = Logger.new(STDOUT)
  @debugging = debugging
end

Public Instance Methods

query(functionName, args, timeout = "4000") click to toggle source

The query API endpoint is used for querying (reading) a blockchain ledger using smart contract function. The endpoint must call a function already defined in your smart contract app which will process the query request. The function name is part of the endpoint URL, or can be entered as the fcn parameter when testing using the Sandbox. The function arguments (number of arguments and type) is determined by the smart contract. The smart contract is responsible for validation and exception management. In case of error the smart contract is responsible for returning the proper http error code. When exception happens, and it is not caught by smart contract or if caught and no http status code is returned, the API gateway will return http-status-code 500 to the client app. For example, if testing the sample get-set smart contract app, enter ‘get’ (without quotes) as the value for fcn.

@param functionName Name of the smart contract function to be invoked @param args the arguments to be passed to the smart contract @param timeout Request timeout in millisecond @return QueryResponse

# File lib/xooa/api/QueryApi.rb, line 60
def query(functionName, args, timeout = "4000")

  path = "/query/{fcn}".sub('{' + 'fcn' + '}', functionName.to_s)

  url = requestUtil.getUrl(@appUrl, path)

  logger.info "Calling API #{url}"
  if debugging
    logger.debug "Calling API #{url}"
  end

  queryParams = {}
  queryParams[:'async'] = 'false'
  queryParams[:'timeout'] = timeout

  headerParams = {}
  headerParams[:'Authorization'] = 'Bearer ' + @apiToken
  headerParams[:'Content-Type'] = 'application/json'

  postBody = "["

  if args.respond_to?("each")
    args.each do |argument|
      postBody += "\"" + argument + "\", "
    end
  elsif args.nil?
    postBody = nil
  elsif postBody += "\"" + args + "\", "
  end

  if !postBody.nil?
    postBody = postBody[0..(postBody.size - 3)] + "]"
  end

  begin
    request = requestUtil.buildRequest(url, 'POST', :headerParams => headerParams, :queryParams => queryParams, :body => postBody)

    response, statusCode = requestUtil.getResponse(request)

    if debugging
      logger.debug "Status Code - #{statusCode}"
      logger.debug "Response - #{response}"
    end

  rescue Xooa::Exception::XooaApiException => xae
    logger.error xae
    raise xae

  rescue StandardError => se
    logger.error se
    raise Xooa::Exception::XooaApiException.new('0', se.to_s)

  end

  if statusCode == 200
    return Xooa::Response::QueryResponse.new(response['payload'])

  elsif statusCode == 202
    logger.error response
    raise Xooa::Exception::XooaRequestTimeoutException.new(response['resultId'], response['resultURL'])

  else
    logger.error response
    raise Xooa::Exception::XooaApiException.new(statusCode, response)
  end
end
queryAsync(functionName, args) click to toggle source

The query API endpoint is used for querying (reading) a blockchain ledger using smart contract function. The endpoint must call a function already defined in your smart contract app which will process the query request. The function name is part of the endpoint URL, or can be entered as the fcn parameter when testing using the Sandbox. The function arguments (number of arguments and type) is determined by the smart contract. The smart contract is responsible for validation and exception management. In case of error the smart contract is responsible for returning the proper http error code. When exception happens, and it is not caught by smart contract or if caught and no http status code is returned, the API gateway will return http-status-code 500 to the client app. For example, if testing the sample get-set smart contract app, enter ‘get’ (without quotes) as the value for fcn.

@param functionName Name of the smart contract function to be invoked @param args the arguments to be passed to the smart contract @return PendingTransactionResponse

# File lib/xooa/api/QueryApi.rb, line 141
def queryAsync(functionName, args)

  path = "/query/{fcn}".sub('{' + 'fcn' + '}', functionName.to_s)

  url = requestUtil.getUrl(@appUrl, path)

  logger.info "Calling API #{url}"
  if debugging
    logger.debug "Calling API #{url}"
  end

  queryParams = {}
  queryParams[:'async'] = 'true'

  headerParams = {}
  headerParams[:'Authorization'] = 'Bearer ' + @apiToken
  headerParams[:'Content-Type'] = 'application/json'

  postBody = "["

  if args.respond_to?("each")
    args.each do |argument|

      postBody += "\"" + argument + "\", "
    end
  elsif args.nil?
    postBody = nil
  elsif postBody += "\"" + args + "\", "
  end

  if !postBody.nil?
    postBody = postBody[0..(postBody.size - 3)] + "]"
  end

  begin
    request = requestUtil.buildRequest(url, 'POST', :headerParams => headerParams, :queryParams => queryParams, :body => postBody)

    response, statusCode = requestUtil.getResponse(request)

    if debugging
      logger.debug "Status Code - #{statusCode}"
      logger.debug "Response - #{response}"
    end

  rescue Xooa::Exception::XooaApiException => xae
    logger.error response
    raise xae

  rescue StandardError => se
    logger.error se
    raise Xooa::Exception::XooaApiException.new('0', se.to_s)
  end

  if statusCode == 202
    return Xooa::Response::PendingTransactionResponse.new(response['resultId'], response['resultURL'])

  else
    logger.error response
    raise Xooa::Exception::XooaApiException.new(statusCode, response)
  end
end