Class: Harmony::Api::Client
- Inherits:
-
Object
- Object
- Harmony::Api::Client
- Defined in:
- lib/harmony/api/client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(network: :mainnet, shard: 0, configuration: ::Harmony::Api.configuration, options: {}) ⇒ Client
constructor
A new instance of Client.
- #log(tag = self.class.name, message) ⇒ Object
- #post(method, params: []) ⇒ Object
- #response(resp) ⇒ Object
- #set_connection ⇒ Object
- #set_defaults ⇒ Object
- #set_url(network, shard) ⇒ Object
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_version ⇒ Object
Returns the value of attribute api_version.
6 7 8 |
# File 'lib/harmony/api/client.rb', line 6 def api_version @api_version end |
#configuration ⇒ Object
Returns the value of attribute configuration.
6 7 8 |
# File 'lib/harmony/api/client.rb', line 6 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
6 7 8 |
# File 'lib/harmony/api/client.rb', line 6 def connection @connection end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/harmony/api/client.rb', line 6 def headers @headers end |
#payload ⇒ Object
Returns the value of attribute payload.
6 7 8 |
# File 'lib/harmony/api/client.rb', line 6 def payload @payload end |
#url ⇒ Object
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, ) puts "[#{tag}] - #{Time.now}: #{}" 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_connection ⇒ Object
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.[:timeout] = configuration.faraday.fetch(:timeout, nil) end if configuration.faraday.fetch(:open_timeout, nil) builder.[: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_defaults ⇒ Object
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 |