class Blockchain::InvokeBlockchainApi < ApplicationService
  # 调用blockchain

  attr_reader :params

  def initialize(params)
    @params  = params
  end

  def call
    begin
      uri = Blockchain.blockchain_config[:api_url]
      uri = URI.parse(URI.encode(uri.strip))
      res = Net::HTTP.start(uri.host, uri.port) do |http|
        req = Net::HTTP::Post.new(uri)
        req['Content-Type'] = 'application/json'
        req.body = @params
        http.request(req)
      end
      if res.code.to_i != 200
        raise "区块链接口请求失败."
      else
        res_body = JSON.parse(res.body)
        if res_body.has_key?("status")
        else
          raise "区块链接口请求失败."
        end
      end
      return res_body
    rescue Exception => e
      raise "区块链接口请求失败."
    end
  end
end