diff --git a/lib/ethereum/http_client.rb b/lib/ethereum/http_client.rb index b8a214d..bf8e869 100644 --- a/lib/ethereum/http_client.rb +++ b/lib/ethereum/http_client.rb @@ -1,13 +1,15 @@ require 'net/http' module Ethereum class HttpClient < Client - attr_accessor :host, :port, :uri, :ssl + attr_accessor :host, :port, :uri, :ssl, :cookie - def initialize(host, port, ssl = false, log = false) + def initialize(host, port, ssl = false, log = false, cookie = false) super(log) @host = host @port = port @ssl = ssl + @use_cookie = cookie + @cookie = nil if ssl @uri = URI("https://#{@host}:#{@port}") else @@ -21,14 +23,28 @@ def send_single(payload) http.use_ssl = true end header = {'Content-Type' => 'application/json'} + if @use_cookie && @cookie.present? + header['Cookie'] = @cookie + end request = ::Net::HTTP::Post.new(uri, header) request.body = payload response = http.request(request) + if @use_cookie + set_cookie_headers = response.get_fields('set-cookie') + if set_cookie_headers.present? + @cookie = set_cookie_headers.map{|str| str.split('; ').first}.join('; ') + end + end return response.body end def send_batch(batch) - raise NotImplementedError + result = send_single(batch.to_json) + result = JSON.parse(result) + + # Make sure the order is the same as it was when batching calls + # See 6 Batch here http://www.jsonrpc.org/specification + return result.sort_by! { |c| c['id'] } end end