Class: Tavily::Connection Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Thin HTTP layer over Ruby’s standard library Net::HTTP. Handles JSON encoding/decoding, authentication, timeouts, automatic retries with exponential backoff, and translation of HTTP errors into APIErrors.

Constant Summary collapse

RETRIABLE_STATUSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTTP statuses that are safe to retry (transient).

[408, 409, 425, 429, 500, 502, 503, 504].freeze
RETRIABLE_TIMEOUTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Network-level exceptions that warrant a retry.

[
  Net::OpenTimeout,
  Net::ReadTimeout,
  defined?(Net::WriteTimeout) ? Net::WriteTimeout : nil
].compact.freeze
RETRIABLE_NETWORK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  SocketError,
  SystemCallError,
  IOError,
  OpenSSL::SSL::SSLError,
  EOFError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Connection.

Parameters:



34
35
36
# File 'lib/tavily/connection.rb', line 34

def initialize(config)
  @config = config
end

Instance Method Details

#get(path, params = {}) ⇒ Hash, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Issue a GET request with query parameters.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (Hash, Array, String)

    parsed response body



50
51
52
# File 'lib/tavily/connection.rb', line 50

def get(path, params = {})
  request(:get, path, query: params)
end

#post(path, body = {}) ⇒ Hash, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Issue a POST request with a JSON body.

Parameters:

  • path (String)

    request path (e.g. “/search”)

  • body (Hash) (defaults to: {})

    request body; nil values are removed before sending

Returns:

  • (Hash, Array, String)

    parsed response body



42
43
44
# File 'lib/tavily/connection.rb', line 42

def post(path, body = {})
  request(:post, path, body: body)
end

#stream(path, body = {}) {|event| ... } ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Issue a POST request and yield Server-Sent Events as they arrive. Streaming responses are not retried.

Parameters:

  • path (String)
  • body (Hash) (defaults to: {})

Yield Parameters:

Returns:

  • (nil)


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
87
88
89
90
91
92
93
94
# File 'lib/tavily/connection.rb', line 61

def stream(path, body = {})
  ensure_api_key!
  uri = build_uri(path, nil)
  http = build_http(uri)
  req = build_request(:post, uri, body)
  req["Accept"] = "text/event-stream"

  buffer = +""
  begin
    http.start do |conn|
      conn.request(req) do |response|
        status = response.code.to_i
        raise stream_error(response) unless status.between?(200, 299)

        response.read_body do |chunk|
          buffer << chunk.gsub("\r\n", "\n")
          while (idx = buffer.index("\n\n"))
            raw_event = buffer.slice!(0..(idx + 1))
            event = parse_sse_event(raw_event)
            yield event if event
          end
        end
      end
    end
  rescue *RETRIABLE_TIMEOUTS => e
    raise TimeoutError, "Tavily stream timed out: #{e.message}"
  rescue *RETRIABLE_NETWORK => e
    raise ConnectionError, "Tavily stream connection failed: #{e.message}"
  end

  event = parse_sse_event(buffer)
  yield event if event
  nil
end