Class: Elrond::Api::Wallet::Client

Inherits:
Object
  • Object
show all
Includes:
Endpoints::Address, Endpoints::Transactions
Defined in:
lib/elrond/api/wallet/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Endpoints::Transactions

#request_tokens

Methods included from Endpoints::Address

#address

Constructor Details

#initialize(proxy: nil, user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", configuration: ::Elrond::Api.configuration, options: {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(proxy: nil, user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", configuration: ::Elrond::Api.configuration, options: {})
  self.proxy            =   proxy
  self.user_agent       =   user_agent
  self.configuration    =   configuration

  set_headers
  set_connection
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#user_agentObject

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

Instance Method Details

#delete(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



56
57
58
# File 'lib/elrond/api/wallet/client.rb', line 56

def delete(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :delete, parameters: parameters, data: data, headers: headers, options: options
end

#get(path, parameters: {}, headers: {}, options: {}) ⇒ Object



40
41
42
# File 'lib/elrond/api/wallet/client.rb', line 40

def get(path, parameters: {}, headers: {}, options: {})
  request path, method: :get, parameters: parameters, headers: headers, options: options
end

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



93
94
95
# File 'lib/elrond/api/wallet/client.rb', line 93

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

#patch(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



52
53
54
# File 'lib/elrond/api/wallet/client.rb', line 52

def patch(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :patch, parameters: parameters, data: data, headers: headers, options: options
end

#post(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



44
45
46
# File 'lib/elrond/api/wallet/client.rb', line 44

def post(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :post, parameters: parameters, data: data, headers: headers, options: options
end

#put(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



48
49
50
# File 'lib/elrond/api/wallet/client.rb', line 48

def put(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :put, parameters: parameters, data: data, headers: headers, options: options
end

#request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/elrond/api/wallet/client.rb', line 60

def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3)
  response                  =   nil

  response                  =   case method
    when :get
      self.connection.get do |request|
        request.url path
        request.headers     =   self.connection.headers.merge(headers)
        request.params      =   parameters if parameters && !parameters.empty?
      end
    when :head
      self.connection.head do |request|
        request.url path
        request.headers     =   self.connection.headers.merge(headers)
        request.params      =   parameters if parameters && !parameters.empty?
      end
    when :post, :put, :patch, :delete
      self.connection.send(method) do |request|
        request.url path
        request.headers     =   self.connection.headers.merge(headers)
        request.body        =   data if data && !data.empty?
        request.params      =   parameters if parameters && !parameters.empty?
      end
  end
      
  return response
end

#set_connectionObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/elrond/api/wallet/client.rb', line 17

def set_connection
  self.connection       =   ::Faraday.new(self.configuration.endpoints.dig(:wallet, :host)) do |builder|
    builder.options[:timeout]         =   self.configuration.faraday.fetch(:timeout, nil)      if self.configuration.faraday.fetch(:timeout, nil)
    builder.options[:open_timeout]    =   self.configuration.faraday.fetch(:open_timeout, nil) if self.configuration.faraday.fetch(:open_timeout, nil)
      
    builder.headers     =   self.headers if self.headers && !self.headers.empty?
  
    builder.request  :json
      
    builder.response :logger, ::Logger.new(STDOUT), bodies: true if self.configuration.verbose
    builder.response :json, content_type: /\bjson$/
      
    builder.use ::FaradayMiddleware::FollowRedirects, limit: 10
      
    if self.proxy
      builder.proxy     =   self.proxy
      log("Will use proxy: #{builder.proxy.inspect}")
    end
  
    builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
  end
end

#set_headersObject



88
89
90
91
# File 'lib/elrond/api/wallet/client.rb', line 88

def set_headers
  self.headers                =   self.configuration.endpoints.dig(:wallet, :headers)
  self.headers["User-Agent"]  =   self.user_agent unless self.user_agent.to_s.empty?
end