Class: Helo::Core::Client

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

Direct Known Subclasses

Helo::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/helo/core/client.rb', line 9

def initialize(configuration)
  raise ArgumentError, "base_url is not configured" if configuration.base_url.to_s.empty?

  @configuration = configuration
  @connection = Faraday.new(url: configuration.base_url) do |f|
    f.request :json
    f.response :json
    f.adapter Faraday.default_adapter
    f.options.params_encoder = Faraday::FlatParamsEncoder

    # Allow custom Faraday configuration
    configuration.faraday_configuration_block&.call(f)
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/helo/core/client.rb', line 7

def configuration
  @configuration
end

Instance Method Details

#request(method, path, params: {}, body: nil, headers: {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/helo/core/client.rb', line 24

def request(method, path, params: {}, body: nil, headers: {})
  response = connection.public_send(method, path) do |req|
    req.params = params
    req.body = body

    # Set Authorization header with dynamic or static token
    token = current_api_key
    req.headers["Authorization"] = "Bearer #{token}" if token

    # Apply per-request headers verbatim (already in wire form)
    headers.each do |key, value|
      req.headers[key.to_s] = value.to_s
    end
  end

  handle_response(response)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
  handle_connection_error(e)
end