Class: Exa::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/exa/connection.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.exa.ai"

Class Method Summary collapse

Class Method Details

.build(api_key:, **options, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/exa/connection.rb', line 9

def self.build(api_key:, **options, &block)
  Faraday.new(url: options[:base_url] || DEFAULT_BASE_URL) do |conn|
    # Authentication
    conn.headers["x-api-key"] = api_key

    # Request/Response JSON encoding
    conn.request :json

    # Debug logging (when enabled via option)
    if options[:debug]
      conn.response :logger, Logger.new($stdout), headers: true, bodies: true
    end

    # Custom error handling (registered before JSON so it runs after in response chain)
    conn.response :raise_error
    conn.response :json, content_type: /\bjson$/

    # Timeouts
    conn.options.timeout = options[:timeout] || 30
    conn.options.open_timeout = options[:open_timeout] || 10

    # Adapter (allow override for testing)
    if block_given?
      conn.adapter options[:adapter] || Faraday.default_adapter, &block
    else
      conn.adapter options[:adapter] || Faraday.default_adapter
    end
  end
end