Class: Harmony::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/harmony/api/client.rb

Direct Known Subclasses

V1::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network: :mainnet, shard: 0, configuration: ::Harmony::Api.configuration, options: {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
# File 'lib/harmony/api/client.rb', line 8

def initialize(network: :mainnet, shard: 0, configuration: ::Harmony::Api.configuration, options: {})
  self.configuration = configuration

  set_url(network, shard)
  set_defaults
  set_connection
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



6
7
8
# File 'lib/harmony/api/client.rb', line 6

def api_version
  @api_version
end

#configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/harmony/api/client.rb', line 6

def configuration
  @configuration
end

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/harmony/api/client.rb', line 6

def connection
  @connection
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/harmony/api/client.rb', line 6

def headers
  @headers
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/harmony/api/client.rb', line 6

def payload
  @payload
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/harmony/api/client.rb', line 6

def url
  @url
end

Instance Method Details

#log(tag = self.class.name, message) ⇒ Object



82
83
84
# File 'lib/harmony/api/client.rb', line 82

def log(tag = self.class.name, message)
  puts "[#{tag}] - #{Time.now}: #{message}" if configuration.verbose
end

#post(method, params: []) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/harmony/api/client.rb', line 51

def post(method, params: [])
  versioned_method = api_version.eql?(1) ? "hmy_#{method}" : "hmyv#{version}_#{method}"

  data = payload.merge(
    method: versioned_method,
    params: params
  )

  response = connection.post do |request|
    if headers && !headers.empty?
      request.headers = connection.headers.merge(headers)
    end
    request.body = data if data && !data.empty?
  end
end

#response(resp) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/harmony/api/client.rb', line 67

def response(resp)
  if resp.success?
    resp  = resp&.body

    error = resp&.fetch('error', {})
    unless error.empty?
      raise ::Harmony::Api::Error, "#{error.fetch('message', '')} (#{error.fetch('code', -1)})"
    end

    resp&.fetch('result')
  else
    raise ::Harmony::Api::Error, "Failed to send request to #{self.url}"
  end
end

#set_connectionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/harmony/api/client.rb', line 27

def set_connection
  self.connection = ::Faraday.new(url) do |builder|
    if configuration.faraday.fetch(:timeout, nil)
      builder.options[:timeout]         =   configuration.faraday.fetch(:timeout, nil)
    end
    if configuration.faraday.fetch(:open_timeout, nil)
      builder.options[:open_timeout]    =   configuration.faraday.fetch(:open_timeout, nil)
    end

    builder.headers = headers if headers && !headers.empty?

    builder.request :json

    if configuration.verbose
      builder.response :logger, ::Logger.new(STDOUT), bodies: true
    end
    builder.response :json, content_type: /\bjson$/

    builder.use ::FaradayMiddleware::FollowRedirects, limit: 10

    builder.adapter configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
  end
end

#set_defaultsObject



20
21
22
23
24
25
# File 'lib/harmony/api/client.rb', line 20

def set_defaults
  self.payload = {
    jsonrpc: '2.0',
    id: 1
  }
end

#set_url(network, shard) ⇒ Object



16
17
18
# File 'lib/harmony/api/client.rb', line 16

def set_url(network, shard)
  self.url = configuration.networks[network][:url] % shard
end