Module: TezosClient::RpcInterface::RequestManager

Included in:
TezosClient::RpcInterface
Defined in:
lib/tezos_client/rpc_interface/request_manager.rb

Instance Method Summary collapse

Instance Method Details

#get(path, query: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tezos_client/rpc_interface/request_manager.rb', line 6

def get(path, query: {})
  url = "http://#{@host}:#{@port}/#{path}"

  response = HTTParty.get(url, headers: { "Content-Type" => "application/json" }, query: query)
  formatted_response = format_response(response.parsed_response)

  log("-------")
  log(">>> GET #{response.request.uri.to_s} \n")
  log("<<< code: #{response.code} \n #{formatted_response.pretty_inspect}")
  log("-------")
  unless response.success?
    raise "#{url} failed with code #{response.code}: #{formatted_response.pretty_inspect}"
  end

  formatted_response
end

#monitor(path, &event_handler) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tezos_client/rpc_interface/request_manager.rb', line 43

def monitor(path, &event_handler)
  uuid = SecureRandom.uuid

  url = "http://#{@host}:#{@port}/#{path}"

  event_reader = monitor_event_reader(uuid, event_handler)

  Thread.new do
    log("Monitor #{uuid}: Start monitoring GET #{url}")
    RestClient::Request.execute(method: :get,
                                read_timeout: 60 * 60 * 24 * 365,
                                url: url,
                                block_response: event_reader,
                                content_type: :json,
                                accept: :json)
  rescue => e
    log "#{uuid}: failed with error #{e}"
  end
end

#post(path, content) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tezos_client/rpc_interface/request_manager.rb', line 23

def post(path, content)
  url = "http://#{@host}:#{@port}/#{path}"
  response = HTTParty.post(url,
                           body: content.to_json,
                           headers: { "Content-Type" => "application/json" })

  formatted_response = format_response(response.parsed_response)

  log("-------")
  log(">>> POST #{url} \n #{content.pretty_inspect}")
  log("<<< code: #{response.code} \n #{formatted_response.pretty_inspect}")
  log("-------")

  unless response.success?
    raise "#{url} failed with code #{response.code}:\n #{formatted_response.pretty_inspect}"
  end

  formatted_response
end