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
# 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
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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/helo/core/client.rb', line 15

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