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
22
23
# File 'lib/tezos_client/rpc_interface/request_manager.rb', line 6

def get(path, query: {})
  url = "http://#{@host}:#{@port}/#{path}"
  response = nil
  exec_time = Benchmark.realtime do
    response = HTTParty.get(url, headers: { "Content-Type" => "application/json" }, query: query)
  end
  formatted_response = format_response(response.parsed_response)

  log("-------")
  log(">>> GET #{response.request.uri} \n")
  log("<<< code: #{response.code} \n    exec time: #{exec_time}\n #{tezos_contents_log(formatted_response)}")
  log("-------")
  unless response.success?
    failed!(url: url, code: response.code, responses: formatted_response)
  end

  formatted_response
end

#monitor(path, &event_handler) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tezos_client/rpc_interface/request_manager.rb', line 49

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}"
    raise
  end
end

#post(path, content) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tezos_client/rpc_interface/request_manager.rb', line 25

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

  response = nil
  exec_time = Benchmark.realtime do
    response = HTTParty.post(url,
                             body: content.to_json,
                             headers: { "Content-Type" => "application/json" })
  end

  formatted_response = format_response(response.parsed_response)

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

  unless response.success?
    failed!(url: url, code: response.code, responses: formatted_response)
  end

  formatted_response
end