Class: TreezorConnect::Client

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

Defined Under Namespace

Modules: FaradayResponseCompatibilyMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
# File 'lib/treezor_connect/client.rb', line 7

def initialize(access_token)
  @access_token = access_token || default_access_token
  @conn = Faraday.new(
    url: TreezorConnect.api_base_url,
    headers: { 'Authorization' => "Bearer #{@access_token}" }
  ) do |faraday|
    faraday.response :logger, Logger.new($stdout) if ENV['ENABLE_HTTP_LOGGING']
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/treezor_connect/client.rb', line 5

def access_token
  @access_token
end

Instance Method Details

#execute_parallel_requests(method, path, headers: {}, params_list: []) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/treezor_connect/client.rb', line 26

def execute_parallel_requests(method, path, headers: {}, params_list: [])
  # For now this is only needed for BulkVoP that uses json, if needed for other uses we might need to refactor a bit
  unless params_list.flat_map(&:keys).uniq == [:body]
    raise NotImplementedError, 'For now parallel requests only work on JSON POST'
  end

  # Shared frozen headers — built once, never duped : this is to avoid huge memory costs
  shared_headers = { **conn.headers, 'Content-Type' => 'application/json', **headers }.freeze

  requests = params_list.map do |params|
    Typhoeus::Request.new(
      conn.url_prefix + path, method:, headers: shared_headers, body: params.fetch(:body).to_json
    ).tap { |req| parallel_manager.queue(req) }
  end

  parallel_manager.run

  process_parallel_requests(requests)
end

#execute_request(method, path, headers: {}, params: {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/treezor_connect/client.rb', line 17

def execute_request(method, path, headers: {}, params: {})
  response = conn.public_send(method, path) do |req|
    req.headers.merge!(headers)
    configure_request(req, params)
  end

  process_response(response)
end