Sha256: 3241507c30dc6f935f8e6cfbe0ae470113130a351962673f6d5f330070ef9c28

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'net/http'
require 'json'
module Platon
  class HttpClient < Client
    attr_accessor :host, :port, :uri, :ssl, :proxy

    def initialize(host,chain_name, proxy = nil, log = false)
      super(chain_name,log)
      uri = URI.parse(host)
      raise ArgumentError unless ['http', 'https'].include? uri.scheme
      @host = uri.host
      @port = uri.port
      @proxy = proxy
      
      @ssl = uri.scheme == 'https'
      @uri = URI("#{uri.scheme}://#{@host}:#{@port}#{uri.path}")
    end

    def update_setting(params)  # :host,:proxy, :chain_id, :hrp
      if params[:host]
        uri = URI.parse(params[:host])
        raise ArgumentError unless ['http', 'https'].include? uri.scheme
        @host = uri.host
        @port = uri.port
      end
      @proxy = proxy if params[:proxy]
      super(params)
    end

    def send_single(payload)
      if @proxy.present?
        _, p_username, p_password, p_host, p_port = @proxy.gsub(/(:|\/|@)/,' ').squeeze(' ').split
        http = ::Net::HTTP.new(@host, @port, p_host, p_port, p_username, p_password)
      else
        http = ::Net::HTTP.new(@host, @port)
      end

      if @ssl
        http.use_ssl = true
      end
      header = {'Content-Type' => 'application/json'}
      request = ::Net::HTTP::Post.new(uri, header)
      request.body = payload
      response = http.request(request)
      response.body
    end

    def send_batch(batch)
      result = send_single(batch.to_json)
      result = JSON.parse(result)
      result.sort_by! { |c| c['id'] }
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
platon-0.2.9 lib/platon/http_client.rb
platon-0.2.7 lib/platon/http_client.rb