Module: OllamaChat::HTTPHandling

Included in:
Chat
Defined in:
lib/ollama_chat/http_handling.rb

Overview

Provides helper methods for HTTP interactions used by the OllamaChat library. The module is mixed into various classes (e.g. OllamaChat::Chat) to give them access to a convenient, configuration‑aware HTTP client.

The primary responsibilities are:

  • Build a hash of HTTP options (e.g. SSL verification and proxy settings) based on the current configuration.

  • Perform a GET request with those options, optionally using a cache and debugging information.

The module is deliberately lightweight – it delegates the actual network call to OllamaChat::Utils::Fetcher.

Instance Method Summary collapse

Instance Method Details

#get_url(url, headers: nil, reraise: false, cache: nil) {|IO| ... } ⇒ Object

Performs an HTTP GET request.

The method merges the caller‑supplied headers with the headers configured in config.request_headers?. It then delegates to OllamaChat::Utils::Fetcher.get, passing along any cache or debug flags and the HTTP options generated by http_options.

Parameters:

  • url (String)

    the target URL.

  • headers (Hash) (defaults to: nil)

    optional additional headers.

  • reraise (Boolean) (defaults to: false)

    whether to re‑raise errors from the fetcher.

  • cache (Object) (defaults to: nil)

    optional cache object to use.

Yields:

  • (IO)

    yields a temporary file handle for the caller to consume.

Returns:

  • (Object)

    the result of the block or the fetcher.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ollama_chat/http_handling.rb', line 51

def get_url(url, headers: nil, reraise: false, cache: nil, &block)
  headers = config.request_headers?.to_h | headers
  OllamaChat::Utils::Fetcher.get(
    url,
    headers:      ,
    cache:        ,
    debug:        ,
    reraise:      ,
    http_options: http_options(OllamaChat::Utils::Fetcher.normalize_url(url)),
    &block
  )
end

#http_options(url) ⇒ Hash

Returns a hash of HTTP options suitable for Net::HTTP or a compatible client.

The options include:

  • :ssl_verify_peer – a boolean that disables peer verification for hostnames listed in the configuration’s ssl_no_verify set.

  • :proxy – the proxy URL if one is configured.

Parameters:

  • url (String)

    the URL for which the options should be generated.

Returns:

  • (Hash)

    a hash containing the HTTP options.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ollama_chat/http_handling.rb', line 26

def http_options(url)
  options = {}
  if ssl_no_verify = config.ssl_no_verify?
    hostname = URI.parse(url).hostname
    options |= { ssl_verify_peer: !ssl_no_verify.include?(hostname) }
  end
  if proxy = config.proxy?
    options |= { proxy: }
  end
  options
end